mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -129,10 +129,10 @@ class Solution:
|
||||
head_dummy.next = head
|
||||
|
||||
slow, fast = head_dummy, head_dummy
|
||||
while(n!=0): #fast先往前走n步
|
||||
while(n>=0): #fast先往前走n+1步
|
||||
fast = fast.next
|
||||
n -= 1
|
||||
while(fast.next!=None):
|
||||
while(fast!=None):
|
||||
slow = slow.next
|
||||
fast = fast.next
|
||||
#fast 走到结尾后,slow的下一个节点为倒数第N个节点
|
||||
|
@ -117,6 +117,9 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
* 时间复杂度 O(n^2): 模拟遍历二维矩阵的时间
|
||||
* 空间复杂度 O(1)
|
||||
|
||||
## 类似题目
|
||||
|
||||
* 54.螺旋矩阵
|
||||
|
@ -219,19 +219,17 @@ func findContentChildren(g []int, s []int) int {
|
||||
|
||||
### Rust
|
||||
```rust
|
||||
pub fn find_content_children(children: Vec<i32>, cookie: Vec<i32>) -> i32 {
|
||||
let mut children = children;
|
||||
let mut cookies = cookie;
|
||||
pub fn find_content_children(mut children: Vec<i32>, mut cookie: Vec<i32>) -> i32 {
|
||||
children.sort();
|
||||
cookies.sort();
|
||||
|
||||
let (mut child, mut cookie) = (0usize, 0usize);
|
||||
let (mut child, mut cookie) = (0, 0);
|
||||
while child < children.len() && cookie < cookies.len() {
|
||||
// 优先选择最小饼干喂饱孩子
|
||||
if children[child] <= cookies[cookie] {
|
||||
child += 1;
|
||||
}
|
||||
cookie += 1
|
||||
cookie += 1;
|
||||
}
|
||||
child as i32
|
||||
}
|
||||
|
@ -86,6 +86,9 @@ public:
|
||||
};
|
||||
|
||||
```
|
||||
* 时间复杂度:O(log n)
|
||||
* 空间复杂度:O(1)
|
||||
|
||||
|
||||
### 二分法第二种写法
|
||||
|
||||
@ -124,6 +127,9 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
* 时间复杂度:O(log n)
|
||||
* 空间复杂度:O(1)
|
||||
|
||||
|
||||
## 总结
|
||||
|
||||
|
@ -305,11 +305,12 @@ def construct_binary_tree(nums: []) -> TreeNode:
|
||||
Tree.append(node)
|
||||
if i == 0:
|
||||
root = node
|
||||
# 直接判断2*i+2<len(Tree)会漏掉2*i+1=len(Tree)-1的情况
|
||||
for i in range(len(Tree)):
|
||||
node = Tree[i]
|
||||
if node and (2 * i + 2) < len(Tree):
|
||||
node.left = Tree[i * 2 + 1]
|
||||
node.right = Tree[i * 2 + 2]
|
||||
if Tree[i] and 2 * i + 1 < len(Tree):
|
||||
Tree[i].left = Tree[2 * i + 1]
|
||||
if 2 * i + 2 < len(Tree):
|
||||
Tree[i].right = Tree[2 * i + 2]
|
||||
return root
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user