Skip to content

Commit b29fd6c

Browse files
committed
feat: add solutions to lc problems: No.0457,1302
1 parent bd1bf9a commit b29fd6c

File tree

14 files changed

+773
-69
lines changed

14 files changed

+773
-69
lines changed

lcof/面试题32 - I. 从上到下打印二叉树/README.md

+19-19
Original file line numberDiff line numberDiff line change
@@ -128,25 +128,25 @@ var levelOrder = function (root) {
128128

129129
```go
130130
func levelOrder(root *TreeNode) []int {
131-
if root == nil {
132-
return []int{}
133-
}
134-
q := []*TreeNode{}
135-
q = append(q,root)
136-
//层序遍历,用队列,遍历到谁,就把谁的左右结点加入队列
137-
res := []int{}
138-
for len(q) != 0 {
139-
tmp := q[0]
140-
q = q[1:]
141-
res = append(res,tmp.Val)
142-
if tmp.Left != nil {
143-
q = append(q,tmp.Left)
144-
}
145-
if tmp.Right != nil {
146-
q = append(q,tmp.Right)
147-
}
148-
}
149-
return res
131+
if root == nil {
132+
return []int{}
133+
}
134+
q := []*TreeNode{}
135+
q = append(q, root)
136+
// 层序遍历,用队列,遍历到谁,就把谁的左右结点加入队列
137+
res := []int{}
138+
for len(q) != 0 {
139+
tmp := q[0]
140+
q = q[1:]
141+
res = append(res, tmp.Val)
142+
if tmp.Left != nil {
143+
q = append(q, tmp.Left)
144+
}
145+
if tmp.Right != nil {
146+
q = append(q, tmp.Right)
147+
}
148+
}
149+
return res
150150
}
151151
```
152152

Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
func levelOrder(root *TreeNode) []int {
2-
if root == nil {
3-
return []int{}
4-
}
5-
q := []*TreeNode{}
6-
q = append(q,root)
7-
//层序遍历,用队列,遍历到谁,就把谁的左右结点加入队列
8-
res := []int{}
9-
for len(q) != 0 {
10-
tmp := q[0]
11-
q = q[1:]
12-
res = append(res,tmp.Val)
13-
if tmp.Left != nil {
14-
q = append(q,tmp.Left)
15-
}
16-
if tmp.Right != nil {
17-
q = append(q,tmp.Right)
18-
}
19-
}
20-
return res
2+
if root == nil {
3+
return []int{}
4+
}
5+
q := []*TreeNode{}
6+
q = append(q, root)
7+
// 层序遍历,用队列,遍历到谁,就把谁的左右结点加入队列
8+
res := []int{}
9+
for len(q) != 0 {
10+
tmp := q[0]
11+
q = q[1:]
12+
res = append(res, tmp.Val)
13+
if tmp.Left != nil {
14+
q = append(q, tmp.Left)
15+
}
16+
if tmp.Right != nil {
17+
q = append(q, tmp.Right)
18+
}
19+
}
20+
return res
2121
}

solution/0400-0499/0457.Circular Array Loop/README.md

+125-2
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,150 @@
6565

6666
<p><strong>进阶:</strong>你能设计一个时间复杂度为 <code>O(n)</code> 且额外空间复杂度为 <code>O(1)</code> 的算法吗?</p>
6767

68-
6968
## 解法
7069

7170
<!-- 这里可写通用的实现逻辑 -->
7271

72+
快慢指针。
73+
7374
<!-- tabs:start -->
7475

7576
### **Python3**
7677

7778
<!-- 这里可写当前语言的特殊实现逻辑 -->
7879

7980
```python
80-
81+
class Solution:
82+
def circularArrayLoop(self, nums: List[int]) -> bool:
83+
n = len(nums)
84+
85+
def next(i):
86+
return (i + nums[i] % n + n) % n
87+
88+
for i in range(n):
89+
if nums[i] == 0:
90+
continue
91+
slow, fast = i, next(i)
92+
while nums[slow] * nums[fast] > 0 and nums[slow] * nums[next(fast)] > 0:
93+
if slow == fast:
94+
if slow != next(slow):
95+
return True
96+
break
97+
slow, fast = next(slow), next(next(fast))
98+
j = i
99+
while nums[j] * nums[next(j)] > 0:
100+
nums[j] = 0
101+
j = next(j)
102+
return False
81103
```
82104

83105
### **Java**
84106

85107
<!-- 这里可写当前语言的特殊实现逻辑 -->
86108

87109
```java
110+
class Solution {
111+
private int n;
112+
private int[] nums;
113+
114+
public boolean circularArrayLoop(int[] nums) {
115+
n = nums.length;
116+
this.nums = nums;
117+
for (int i = 0; i < n; ++i) {
118+
if (nums[i] == 0) {
119+
continue;
120+
}
121+
int slow = i, fast = next(i);
122+
while (nums[slow] * nums[fast] > 0 && nums[slow] * nums[next(fast)] > 0) {
123+
if (slow == fast) {
124+
if (slow != next(slow)) {
125+
return true;
126+
}
127+
break;
128+
}
129+
slow = next(slow);
130+
fast = next(next(fast));
131+
}
132+
int j = i;
133+
while (nums[j] * nums[next(j)] > 0) {
134+
nums[j] = 0;
135+
j = next(j);
136+
}
137+
}
138+
return false;
139+
}
140+
141+
private int next(int i) {
142+
return (i + nums[i] % n + n) % n;
143+
}
144+
}
145+
```
146+
147+
### **C++**
148+
149+
```cpp
150+
class Solution {
151+
public:
152+
bool circularArrayLoop(vector<int>& nums) {
153+
int n = nums.size();
154+
for (int i = 0; i < n; ++i) {
155+
if (!nums[i]) continue;
156+
int slow = i, fast = next(nums, i);
157+
while (nums[slow] * nums[fast] > 0 && nums[slow] * nums[next(nums, fast)] > 0) {
158+
if (slow == fast) {
159+
if (slow != next(nums, slow)) return true;
160+
break;
161+
}
162+
slow = next(nums, slow);
163+
fast = next(nums, next(nums, fast));
164+
}
165+
int j = i;
166+
while (nums[j] * nums[next(nums, j)] > 0) {
167+
nums[j] = 0;
168+
j = next(nums, j);
169+
}
170+
}
171+
return false;
172+
}
173+
174+
int next(vector<int>& nums, int i) {
175+
int n = nums.size();
176+
return (i + nums[i] % n + n) % n;
177+
}
178+
};
179+
```
88180

181+
### **Go**
182+
183+
```go
184+
func circularArrayLoop(nums []int) bool {
185+
for i, num := range nums {
186+
if num == 0 {
187+
continue
188+
}
189+
slow, fast := i, next(nums, i)
190+
for nums[slow]*nums[fast] > 0 && nums[slow]*nums[next(nums, fast)] > 0 {
191+
if slow == fast {
192+
if slow != next(nums, slow) {
193+
return true
194+
}
195+
break
196+
}
197+
slow, fast = next(nums, slow), next(nums, next(nums, fast))
198+
}
199+
j := i
200+
for nums[j]*nums[next(nums, j)] > 0 {
201+
nums[j] = 0
202+
j = next(nums, j)
203+
}
204+
}
205+
return false
206+
}
207+
208+
func next(nums []int, i int) int {
209+
n := len(nums)
210+
return (i + nums[i]%n + n) % n
211+
}
89212
```
90213

91214
### **...**

solution/0400-0499/0457.Circular Array Loop/README_EN.md

+123-2
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,142 @@ Every nums[seq[j]] must be either all positive or all negative.
6666
<p>&nbsp;</p>
6767
<p><strong>Follow up:</strong> Could you solve it in <code>O(n)</code> time complexity and <code>O(1)</code> extra space complexity?</p>
6868

69-
7069
## Solutions
7170

7271
<!-- tabs:start -->
7372

7473
### **Python3**
7574

7675
```python
77-
76+
class Solution:
77+
def circularArrayLoop(self, nums: List[int]) -> bool:
78+
n = len(nums)
79+
80+
def next(i):
81+
return (i + nums[i] % n + n) % n
82+
83+
for i in range(n):
84+
if nums[i] == 0:
85+
continue
86+
slow, fast = i, next(i)
87+
while nums[slow] * nums[fast] > 0 and nums[slow] * nums[next(fast)] > 0:
88+
if slow == fast:
89+
if slow != next(slow):
90+
return True
91+
break
92+
slow, fast = next(slow), next(next(fast))
93+
j = i
94+
while nums[j] * nums[next(j)] > 0:
95+
nums[j] = 0
96+
j = next(j)
97+
return False
7898
```
7999

80100
### **Java**
81101

82102
```java
103+
class Solution {
104+
private int n;
105+
private int[] nums;
106+
107+
public boolean circularArrayLoop(int[] nums) {
108+
n = nums.length;
109+
this.nums = nums;
110+
for (int i = 0; i < n; ++i) {
111+
if (nums[i] == 0) {
112+
continue;
113+
}
114+
int slow = i, fast = next(i);
115+
while (nums[slow] * nums[fast] > 0 && nums[slow] * nums[next(fast)] > 0) {
116+
if (slow == fast) {
117+
if (slow != next(slow)) {
118+
return true;
119+
}
120+
break;
121+
}
122+
slow = next(slow);
123+
fast = next(next(fast));
124+
}
125+
int j = i;
126+
while (nums[j] * nums[next(j)] > 0) {
127+
nums[j] = 0;
128+
j = next(j);
129+
}
130+
}
131+
return false;
132+
}
133+
134+
private int next(int i) {
135+
return (i + nums[i] % n + n) % n;
136+
}
137+
}
138+
```
139+
140+
### **C++**
141+
142+
```cpp
143+
class Solution {
144+
public:
145+
bool circularArrayLoop(vector<int>& nums) {
146+
int n = nums.size();
147+
for (int i = 0; i < n; ++i) {
148+
if (!nums[i]) continue;
149+
int slow = i, fast = next(nums, i);
150+
while (nums[slow] * nums[fast] > 0 && nums[slow] * nums[next(nums, fast)] > 0) {
151+
if (slow == fast) {
152+
if (slow != next(nums, slow)) return true;
153+
break;
154+
}
155+
slow = next(nums, slow);
156+
fast = next(nums, next(nums, fast));
157+
}
158+
int j = i;
159+
while (nums[j] * nums[next(nums, j)] > 0) {
160+
nums[j] = 0;
161+
j = next(nums, j);
162+
}
163+
}
164+
return false;
165+
}
166+
167+
int next(vector<int>& nums, int i) {
168+
int n = nums.size();
169+
return (i + nums[i] % n + n) % n;
170+
}
171+
};
172+
```
83173

174+
### **Go**
175+
176+
```go
177+
func circularArrayLoop(nums []int) bool {
178+
for i, num := range nums {
179+
if num == 0 {
180+
continue
181+
}
182+
slow, fast := i, next(nums, i)
183+
for nums[slow]*nums[fast] > 0 && nums[slow]*nums[next(nums, fast)] > 0 {
184+
if slow == fast {
185+
if slow != next(nums, slow) {
186+
return true
187+
}
188+
break
189+
}
190+
slow, fast = next(nums, slow), next(nums, next(nums, fast))
191+
}
192+
j := i
193+
for nums[j]*nums[next(nums, j)] > 0 {
194+
nums[j] = 0
195+
j = next(nums, j)
196+
}
197+
}
198+
return false
199+
}
200+
201+
func next(nums []int, i int) int {
202+
n := len(nums)
203+
return (i + nums[i]%n + n) % n
204+
}
84205
```
85206

86207
### **...**

0 commit comments

Comments
 (0)