Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ad30291

Browse files
Merge branch 'youngyangyang04:master' into master
2 parents 51481e1 + 611336a commit ad30291

File tree

5 files changed

+49
-25
lines changed

5 files changed

+49
-25
lines changed

‎problems/0059.螺旋矩阵II.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ public:
8787

8888
// 下面开始的四个for就是模拟转了一圈
8989
// 模拟填充上行从左到右(左闭右开)
90-
for (j = starty; j < n - offset; j++) {
91-
res[startx][j] = count++;
90+
for (j; j < n - offset; j++) {
91+
res[i][j] = count++;
9292
}
9393
// 模拟填充右列从上到下(左闭右开)
94-
for (i = startx; i < n - offset; i++) {
94+
for (i; i < n - offset; i++) {
9595
res[i][j] = count++;
9696
}
9797
// 模拟填充下行从右到左(左闭右开)

‎problems/0127.单词接龙.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ class Solution {
198198

199199
### Python
200200

201-
```
201+
```python
202202
class Solution:
203203
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
204204
wordSet = set(wordList)

‎problems/0349.两个数组的交集.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ class Solution:
231231

232232
### Go:
233233

234+
(版本一)使用字典和集合
235+
234236
```go
235237
func intersection(nums1 []int, nums2 []int) []int {
236238
set:=make(map[int]struct{},0) // 用map模拟set
@@ -251,6 +253,28 @@ func intersection(nums1 []int, nums2 []int) []int {
251253
}
252254
```
253255

256+
(版本二)使用数组
257+
258+
```go
259+
func intersection(nums1 []int, nums2 []int) []int {
260+
count1 := make([]int, 1001, 1001)
261+
count2 := make([]int, 1001, 1001)
262+
res := make([]int, 0)
263+
for _, v := range nums1 {
264+
count1[v] = 1
265+
}
266+
for _, v := range nums2 {
267+
count2[v] = 1
268+
}
269+
for i := 0; i <= 1000; i++ {
270+
if count1[i] + count2[i] == 2 {
271+
res = append(res, i)
272+
}
273+
}
274+
return res
275+
}
276+
```
277+
254278
### JavaScript:
255279

256280
```js

‎problems/0496.下一个更大元素I.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ class Solution {
256256
### Python3
257257

258258
```python
259+
# 版本一
259260
class Solution:
260261
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
261262
result = [-1]*len(nums1)
@@ -273,6 +274,26 @@ class Solution:
273274
stack.pop()
274275
stack.append(i)
275276
return result
277+
278+
# 版本二
279+
class Solution:
280+
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
281+
stack = []
282+
# 创建答案数组
283+
ans = [-1] * len(nums1)
284+
for i in range(len(nums2)):
285+
while len(stack) > 0 and nums2[i] > nums2[stack[-1]]:
286+
# 判断 num1 是否有 nums2[stack[-1]]。如果没有这个判断会出现指针异常
287+
if nums2[stack[-1]] in nums1:
288+
# 锁定 num1 检索的 index
289+
index = nums1.index(nums2[stack[-1]])
290+
# 更新答案数组
291+
ans[index] = nums2[i]
292+
# 弹出小元素
293+
# 这个代码一定要放在 if 外面。否则单调栈的逻辑就不成立了
294+
stack.pop()
295+
stack.append(i)
296+
return ans
276297
```
277298

278299
### Go

‎problems/0503.下一个更大元素II.md‎

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ class Solution {
170170
### Python:
171171

172172
```python
173-
# 方法 1:
174173
class Solution:
175174
def nextGreaterElements(self, nums: List[int]) -> List[int]:
176175
dp = [-1] * len(nums)
@@ -181,26 +180,6 @@ class Solution:
181180
stack.pop()
182181
stack.append(i%len(nums))
183182
return dp
184-
185-
# 方法 2:
186-
class Solution:
187-
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
188-
stack = []
189-
# 创建答案数组
190-
ans = [-1] * len(nums1)
191-
for i in range(len(nums2)):
192-
while len(stack) > 0 and nums2[i] > nums2[stack[-1]]:
193-
# 判断 num1 是否有 nums2[stack[-1]]。如果没有这个判断会出现指针异常
194-
if nums2[stack[-1]] in nums1:
195-
# 锁定 num1 检索的 index
196-
index = nums1.index(nums2[stack[-1]])
197-
# 更新答案数组
198-
ans[index] = nums2[i]
199-
# 弹出小元素
200-
# 这个代码一定要放在 if 外面。否则单调栈的逻辑就不成立了
201-
stack.pop()
202-
stack.append(i)
203-
return ans
204183
```
205184
### Go:
206185

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /