Merge branch 'youngyangyang04:master' into master

This commit is contained in:
小夭
2023-04-19 15:17:57 +08:00
committed by GitHub
27 changed files with 282 additions and 50 deletions

View File

@ -76,11 +76,9 @@ public:
for (int i = 0; i < nums.size(); i++) { for (int i = 0; i < nums.size(); i++) {
nextDistance = max(nums[i] + i, nextDistance); // 更新下一步覆盖最远距离下标 nextDistance = max(nums[i] + i, nextDistance); // 更新下一步覆盖最远距离下标
if (i == curDistance) { // 遇到当前覆盖最远距离下标 if (i == curDistance) { // 遇到当前覆盖最远距离下标
if (curDistance < nums.size() - 1) { // 如果当前覆盖最远距离下标不是终点 ans++; // 需要走下一步
ans++; // 需要走下一步 curDistance = nextDistance; // 更新当前覆盖最远距离下标(相当于加油了)
curDistance = nextDistance; // 更新当前覆盖最远距离下标(相当于加油了) if (nextDistance >= nums.size() - 1) break; // 当前覆盖最远距到达集合终点不用做ans++操作了,直接结束
if (nextDistance >= nums.size() - 1) break; // 下一步的覆盖范围已经可以达到终点,结束循环
} else break; // 当前覆盖最远距到达集合终点不用做ans++操作了,直接结束
} }
} }
return ans; return ans;

View File

@ -312,24 +312,22 @@ object Solution {
```Rust ```Rust
impl Solution { impl Solution {
fn max(a: i32, b: i32) -> i32 { pub fn merge(mut intervals: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
if a > b { a } else { b } let mut res = vec![];
} if intervals.is_empty() {
return res;
pub fn merge(intervals: Vec<Vec<i32>>) -> Vec<Vec<i32>> { }
let mut intervals = intervals; intervals.sort_by_key(|a| a[0]);
let mut result = Vec::new(); res.push(intervals[0].clone());
if intervals.len() == 0 { return result; } for interval in intervals.into_iter().skip(1) {
intervals.sort_by(|a, b| a[0].cmp(&b[0])); let res_last_ele = res.last_mut().unwrap();
result.push(intervals[0].clone()); if res_last_ele[1] >= interval[0] {
for i in 1..intervals.len() { res_last_ele[1] = interval[1].max(res_last_ele[1]);
if result.last_mut().unwrap()[1] >= intervals[i][0] {
result.last_mut().unwrap()[1] = Self::max(result.last_mut().unwrap()[1], intervals[i][1]);
} else { } else {
result.push(intervals[i].clone()); res.push(interval);
} }
} }
result res
} }
} }
``` ```

View File

@ -101,6 +101,11 @@ public:
}; };
``` ```
* 时间复杂度: O(nm)
* 空间复杂度: O(n)
代码中m表示最多可以爬m个台阶代码中把m改成2就是本题70.爬楼梯可以AC的代码了。 代码中m表示最多可以爬m个台阶代码中把m改成2就是本题70.爬楼梯可以AC的代码了。
## 总结 ## 总结

View File

@ -218,6 +218,10 @@ public:
} }
}; };
``` ```
* 时间复杂度: O(n * m)
* 空间复杂度: O(n * m)
## 其他语言版本 ## 其他语言版本

View File

@ -50,7 +50,7 @@
代码如下: 代码如下:
```CPP ```CPP
int getdepth(treenode* node) int getdepth(TreeNode* node)
``` ```
2. 确定终止条件如果为空节点的话就返回0表示高度为0。 2. 确定终止条件如果为空节点的话就返回0表示高度为0。
@ -76,14 +76,14 @@ return depth;
```CPP ```CPP
class solution { class solution {
public: public:
int getdepth(treenode* node) { int getdepth(TreeNode* node) {
if (node == NULL) return 0; if (node == NULL) return 0;
int leftdepth = getdepth(node->left); // 左 int leftdepth = getdepth(node->left); // 左
int rightdepth = getdepth(node->right); // 右 int rightdepth = getdepth(node->right); // 右
int depth = 1 + max(leftdepth, rightdepth); // 中 int depth = 1 + max(leftdepth, rightdepth); // 中
return depth; return depth;
} }
int maxdepth(treenode* root) { int maxDepth(TreeNode* root) {
return getdepth(root); return getdepth(root);
} }
}; };
@ -93,9 +93,9 @@ public:
```CPP ```CPP
class solution { class solution {
public: public:
int maxdepth(treenode* root) { int maxDepth(TreeNode* root) {
if (root == null) return 0; if (root == null) return 0;
return 1 + max(maxdepth(root->left), maxdepth(root->right)); return 1 + max(maxDepth(root->left), maxDepth(root->right));
} }
}; };
@ -110,7 +110,7 @@ public:
class solution { class solution {
public: public:
int result; int result;
void getdepth(treenode* node, int depth) { void getdepth(TreeNode* node, int depth) {
result = depth > result ? depth : result; // 中 result = depth > result ? depth : result; // 中
if (node->left == NULL && node->right == NULL) return ; if (node->left == NULL && node->right == NULL) return ;
@ -127,7 +127,7 @@ public:
} }
return ; return ;
} }
int maxdepth(treenode* root) { int maxDepth(TreeNode* root) {
result = 0; result = 0;
if (root == NULL) return result; if (root == NULL) return result;
getdepth(root, 1); getdepth(root, 1);
@ -144,7 +144,7 @@ public:
class solution { class solution {
public: public:
int result; int result;
void getdepth(treenode* node, int depth) { void getdepth(TreeNode* node, int depth) {
result = depth > result ? depth : result; // 中 result = depth > result ? depth : result; // 中
if (node->left == NULL && node->right == NULL) return ; if (node->left == NULL && node->right == NULL) return ;
if (node->left) { // 左 if (node->left) { // 左
@ -155,7 +155,7 @@ public:
} }
return ; return ;
} }
int maxdepth(treenode* root) { int maxDepth(TreeNode* root) {
result = 0; result = 0;
if (root == 0) return result; if (root == 0) return result;
getdepth(root, 1); getdepth(root, 1);
@ -182,16 +182,16 @@ c++代码如下:
```CPP ```CPP
class solution { class solution {
public: public:
int maxdepth(treenode* root) { int maxDepth(TreeNode* root) {
if (root == NULL) return 0; if (root == NULL) return 0;
int depth = 0; int depth = 0;
queue<treenode*> que; queue<TreeNode*> que;
que.push(root); que.push(root);
while(!que.empty()) { while(!que.empty()) {
int size = que.size(); int size = que.size();
depth++; // 记录深度 depth++; // 记录深度
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
treenode* node = que.front(); TreeNode* node = que.front();
que.pop(); que.pop();
if (node->left) que.push(node->left); if (node->left) que.push(node->left);
if (node->right) que.push(node->right); if (node->right) que.push(node->right);
@ -230,11 +230,11 @@ c++代码:
```CPP ```CPP
class solution { class solution {
public: public:
int maxdepth(node* root) { int maxDepth(Node* root) {
if (root == 0) return 0; if (root == 0) return 0;
int depth = 0; int depth = 0;
for (int i = 0; i < root->children.size(); i++) { for (int i = 0; i < root->children.size(); i++) {
depth = max (depth, maxdepth(root->children[i])); depth = max (depth, maxDepth(root->children[i]));
} }
return depth + 1; return depth + 1;
} }
@ -247,15 +247,15 @@ public:
```CPP ```CPP
class solution { class solution {
public: public:
int maxdepth(node* root) { int maxDepth(Node* root) {
queue<node*> que; queue<Node*> que;
if (root != NULL) que.push(root); if (root != NULL) que.push(root);
int depth = 0; int depth = 0;
while (!que.empty()) { while (!que.empty()) {
int size = que.size(); int size = que.size();
depth++; // 记录深度 depth++; // 记录深度
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
node* node = que.front(); Node* node = que.front();
que.pop(); que.pop();
for (int j = 0; j < node->children.size(); j++) { for (int j = 0; j < node->children.size(); j++) {
if (node->children[j]) que.push(node->children[j]); if (node->children[j]) que.push(node->children[j]);

View File

@ -250,7 +250,7 @@ private:
vector<vector<int>> result; vector<vector<int>> result;
vector<int> path; vector<int> path;
// 递归函数不需要返回值,因为我们要遍历整个树 // 递归函数不需要返回值,因为我们要遍历整个树
void traversal(treenode* cur, int count) { void traversal(TreeNode* cur, int count) {
if (!cur->left && !cur->right && count == 0) { // 遇到了叶子节点且找到了和为sum的路径 if (!cur->left && !cur->right && count == 0) { // 遇到了叶子节点且找到了和为sum的路径
result.push_back(path); result.push_back(path);
return; return;
@ -276,10 +276,10 @@ private:
} }
public: public:
vector<vector<int>> pathsum(treenode* root, int sum) { vector<vector<int>> pathSum(TreeNode* root, int sum) {
result.clear(); result.clear();
path.clear(); path.clear();
if (root == null) return result; if (root == NULL) return result;
path.push_back(root->val); // 把根节点放进路径 path.push_back(root->val); // 把根节点放进路径
traversal(root, sum - root->val); traversal(root, sum - root->val);
return result; return result;

View File

@ -149,6 +149,11 @@ public:
}; };
``` ```
* 时间复杂度: O(n * m)
* 空间复杂度: O(n * m)
## 其他语言版本 ## 其他语言版本

View File

@ -970,6 +970,49 @@ pub fn remove_extra_spaces(s: &mut Vec<char>) {
} }
} }
``` ```
C:
```C
// 翻转字符串中指定范围的字符
void reverse(char* s, int start, int end) {
for (int i = start, j = end; i < j; i++, j--) {
int tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
}
// 删除字符串两端和中间多余的空格
void removeExtraSpace(char* s) {
int start = 0; // 指向字符串开头的指针
int end = strlen(s) - 1; // 指向字符串结尾的指针
while (s[start] == ' ') start++; // 移动指针 start直到找到第一个非空格字符
while (s[end] == ' ') end--; // 移动指针 end直到找到第一个非空格字符
int slow = 0; // 指向新字符串的下一个写入位置的指针
for (int i = start; i <= end; i++) { // 遍历整个字符串
if (s[i] == ' ' && s[i+1] == ' ') { // 如果当前字符是空格,并且下一个字符也是空格,则跳过
continue;
}
s[slow] = s[i]; // 否则,将当前字符复制到新字符串的 slow 位置
slow++; // 将 slow 指针向后移动
}
s[slow] = '\0'; // 在新字符串的末尾添加一个空字符
}
// 翻转字符串中的单词
char * reverseWords(char * s){
removeExtraSpace(s); // 先删除字符串两端和中间的多余空格
reverse(s, 0, strlen(s) - 1); // 翻转整个字符串
int slow = 0; // 指向每个单词的开头位置的指针
for (int i = 0; i <= strlen(s); i++) { // 遍历整个字符串
if (s[i] ==' ' || s[i] == '\0') { // 如果当前字符是空格或空字符,说明一个单词结束了
reverse(s, slow, i-1); // 翻转单词
slow = i + 1; // 将 slow 指针指向下一个单词的开头位置
}
}
return s; // 返回处理后的字符串
}
```
<p align="center"> <p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -156,6 +156,11 @@ public:
}; };
``` ```
* 时间复杂度: O(n * k),其中 n 为 prices 的长度
* 空间复杂度: O(n * k)
当然有的解法是定义一个三维数组dp[i][j][k]第i天第j次买卖k表示买还是卖的状态从定义上来讲是比较直观。 当然有的解法是定义一个三维数组dp[i][j][k]第i天第j次买卖k表示买还是卖的状态从定义上来讲是比较直观。
但感觉三维数组操作起来有些麻烦,我是直接用二维数组来模拟三维数组的情况,代码看起来也清爽一些。 但感觉三维数组操作起来有些麻烦,我是直接用二维数组来模拟三维数组的情况,代码看起来也清爽一些。

View File

@ -108,6 +108,9 @@ public:
}; };
``` ```
* 时间复杂度: O(n)
* 空间复杂度: O(n)
## 总结 ## 总结
打家劫舍是DP解决的经典题目这道题也是打家劫舍入门级题目后面我们还会变种方式来打劫的。 打家劫舍是DP解决的经典题目这道题也是打家劫舍入门级题目后面我们还会变种方式来打劫的。

View File

@ -316,8 +316,8 @@ class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode: def removeElements(self, head: ListNode, val: int) -> ListNode:
dummy_head = ListNode(next=head) #添加一个虚拟节点 dummy_head = ListNode(next=head) #添加一个虚拟节点
cur = dummy_head cur = dummy_head
while(cur.next!=None): while cur.next:
if(cur.next.val == val): if cur.next.val == val:
cur.next = cur.next.next #删除cur.next节点 cur.next = cur.next.next #删除cur.next节点
else: else:
cur = cur.next cur = cur.next

View File

@ -82,6 +82,11 @@ public:
}; };
``` ```
* 时间复杂度: O(n)
* 空间复杂度: O(n)
## 总结 ## 总结
成环之后还是难了一些的, 不少题解没有把“考虑房间”和“偷房间”说清楚。 成环之后还是难了一些的, 不少题解没有把“考虑房间”和“偷房间”说清楚。

View File

@ -127,6 +127,10 @@ public:
}; };
``` ```
* 时间复杂度: O(n * √n)
* 空间复杂度: O(n)
同样我在给出先遍历物品在遍历背包的代码一样的可以AC的。 同样我在给出先遍历物品在遍历背包的代码一样的可以AC的。
```CPP ```CPP
@ -145,6 +149,8 @@ public:
} }
}; };
``` ```
* 同上
## 总结 ## 总结

View File

@ -106,6 +106,10 @@ public:
} }
}; };
``` ```
* 时间复杂度: O(n^2)
* 空间复杂度: O(n)
## 总结 ## 总结

View File

@ -133,6 +133,11 @@ public:
}; };
``` ```
* 时间复杂度: O(n * amount),其中 n 为 coins 的长度
* 空间复杂度: O(amount)
对于遍历方式遍历背包放在外循环,遍历物品放在内循环也是可以的,我就直接给出代码了 对于遍历方式遍历背包放在外循环,遍历物品放在内循环也是可以的,我就直接给出代码了
```CPP ```CPP
@ -154,6 +159,8 @@ public:
} }
}; };
``` ```
* 同上
## 总结 ## 总结

View File

@ -381,6 +381,32 @@ class Solution:
return path return path
``` ```
python - 使用used数组 - 神似之前几题写法
```python
class Solution:
def findItinerary(self, tickets: List[List[str]]) -> List[str]:
global used,path,results
used = [0]*len(tickets)
path = ['JFK']
results = []
tickets.sort() # 先排序,这样一旦找到第一个可行路径,一定是字母排序最小的
self.backtracking(tickets,'JFK')
return results[0]
def backtracking(self,tickets,cur):
if sum(used) == len(tickets):
results.append(path[:])
return True # 只要找到就返回
for i in range(len(tickets)):
if tickets[i][0]==cur and used[i]==0:
used[i]=1
path.append(tickets[i][1])
state = self.backtracking(tickets,tickets[i][1])
path.pop()
used[i]=0
if state: return True # 只要找到就返回,不继续搜索了
```
### GO ### GO
```go ```go
type pair struct { type pair struct {

View File

@ -130,6 +130,11 @@ public:
``` ```
* 时间复杂度: O(target * n),其中 n 为 nums 的长度
* 空间复杂度: O(target)
C++测试用例有两个数相加超过int的数据所以需要在if里加上dp[i] < INT_MAX - dp[i - num]。 C++测试用例有两个数相加超过int的数据所以需要在if里加上dp[i] < INT_MAX - dp[i - num]。
但java就不用考虑这个限制java里的int也是四个字节吧也有可能leetcode后台对不同语言的测试数据不一样 但java就不用考虑这个限制java里的int也是四个字节吧也有可能leetcode后台对不同语言的测试数据不一样

View File

@ -156,6 +156,11 @@ public:
}; };
``` ```
* 时间复杂度: O(kmn)k 为strs的长度
* 空间复杂度: O(mn)
## 总结 ## 总结
不少同学刷过这道题,可能没有总结这究竟是什么背包。 不少同学刷过这道题,可能没有总结这究竟是什么背包。

View File

@ -144,6 +144,11 @@ public:
} }
}; };
``` ```
* 时间复杂度: O(n^2)
* 空间复杂度: O(n^2)
## 其他语言版本 ## 其他语言版本

View File

@ -179,6 +179,11 @@ public:
} }
}; };
``` ```
* 时间复杂度: O(mn),其中 m 是amountn 是 coins 的长度
* 空间复杂度: O(m)
是不是发现代码如此精简,哈哈 是不是发现代码如此精简,哈哈
## 总结 ## 总结

View File

@ -234,6 +234,26 @@ class Solution:
return root return root
``` ```
**迭代**
```python
class Solution:
def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if not root: return root
stack = []
result = []
cur = root
pre = 0
while cur or stack:
if cur:
stack.append(cur)
cur = cur.right
else:
cur = stack.pop()
cur.val+= pre
pre = cur.val
cur =cur.left
return root
```
## Go ## Go

View File

@ -104,6 +104,10 @@ public:
}; };
``` ```
* 时间复杂度: O(n * m)
* 空间复杂度: O(n * m)
### 动态规划二 ### 动态规划二
@ -127,6 +131,10 @@ public:
}; };
``` ```
* 时间复杂度: O(n * m)
* 空间复杂度: O(n * m)
## 其他语言版本 ## 其他语言版本

View File

@ -280,18 +280,20 @@ object Solution {
```Rust ```Rust
impl Solution { impl Solution {
pub fn monotone_increasing_digits(n: i32) -> i32 { pub fn monotone_increasing_digits(n: i32) -> i32 {
let mut str_num = n.to_string().chars().map(|x| x.to_digit(10).unwrap() as i32).collect::<Vec<i32>>(); let mut n_bytes = n.to_string().into_bytes();
let mut flag = str_num.len(); let mut flag = n_bytes.len();
for i in (1..str_num.len()).rev() { for i in (1..n_bytes.len()).rev() {
if str_num[i - 1] > str_num[i] { if n_bytes[i - 1] > n_bytes[i] {
flag = i; flag = i;
str_num[i - 1] -= 1; n_bytes[i - 1] -= 1;
} }
} }
for i in flag..str_num.len() { for v in n_bytes.iter_mut().skip(flag) {
str_num[i] = 9; *v = 57;
} }
str_num.iter().fold(0, |acc, x| acc * 10 + x) n_bytes
.into_iter()
.fold(0, |acc, x| acc * 10 + x as i32 - 48)
} }
} }
``` ```

View File

@ -271,6 +271,7 @@ class Solution {
``` ```
Python Python
> 未精简版本
```python ```python
class Solution: class Solution:
@ -291,6 +292,21 @@ class Solution:
return answer return answer
``` ```
> 精简版本
```python
class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
answer = [0]*len(temperatures)
stack = []
for i in range(len(temperatures)):
while len(stack)>0 and temperatures[i] > temperatures[stack[-1]]:
answer[stack[-1]] = i - stack[-1]
stack.pop()
stack.append(i)
return answer
```
Go Go
> 暴力法 > 暴力法

View File

@ -535,8 +535,57 @@ function getIndexAfterDel(s: string, startIndex: number): number {
} }
``` ```
### Rust
> 双指针
```rust
impl Solution {
pub fn backspace_compare(s: String, t: String) -> bool {
let (s, t) = (
s.chars().collect::<Vec<char>>(),
t.chars().collect::<Vec<char>>(),
);
Self::get_string(s) == Self::get_string(t)
}
pub fn get_string(mut chars: Vec<char>) -> Vec<char> {
let mut slow = 0;
for i in 0..chars.len() {
if chars[i] == '#' {
slow = (slow as u32).saturating_sub(1) as usize;
} else {
chars[slow] = chars[i];
slow += 1;
}
}
chars.truncate(slow);
chars
}
}
```
> 双栈法
```rust
impl Solution {
pub fn backspace_compare(s: String, t: String) -> bool {
Self::get_string(s) == Self::get_string(t)
}
pub fn get_string(string: String) -> String {
let mut s = String::new();
for c in string.chars() {
if c != '#' {
s.push(c);
} else if !s.is_empty() {
s.pop();
}
}
s
}
}
```
<p align="center"> <p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -62,6 +62,10 @@ public:
} }
}; };
``` ```
* 时间复杂度: O(n * m)
* 空间复杂度: O(n * m)
## 总结 ## 总结

View File

@ -124,6 +124,10 @@ public:
} }
}; };
``` ```
* 时间复杂度: O(n * m),其中 n 和 m 分别为 text1 和 text2 的长度
* 空间复杂度: O(n * m)
## 其他语言版本 ## 其他语言版本