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 648df89

Browse files
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
2 parents 1b4422d + f3ec7a5 commit 648df89

18 files changed

+331
-111
lines changed

‎README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,10 @@
523523

524524
[点此这里](https://github.com/youngyangyang04/leetcode-master/graphs/contributors)查看LeetCode-Master的所有贡献者。感谢他们补充了LeetCode-Master的其他语言版本,让更多的读者收益于此项目。
525525

526+
# Star 趋势
527+
528+
[![Star History Chart](https://api.star-history.com/svg?repos=youngyangyang04/leetcode-master&type=Date)](https://star-history.com/#youngyangyang04/leetcode-master&Date)
529+
526530
# 关于作者
527531

528532
大家好,我是程序员Carl,哈工大师兄,《代码随想录》作者,先后在腾讯和百度从事后端技术研发,CSDN博客专家。对算法和C++后端技术有一定的见解,利用工作之余重新刷leetcode。

‎problems/0027.移除元素.md

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -183,28 +183,24 @@ class Solution {
183183

184184
Python:
185185

186-
```python
186+
```python3
187187
class Solution:
188-
"""双指针法
189-
时间复杂度:O(n)
190-
空间复杂度:O(1)
191-
"""
192-
193-
@classmethod
194-
def removeElement(cls, nums: List[int], val: int) -> int:
195-
fast = slow = 0
196-
197-
while fast < len(nums):
198-
199-
if nums[fast] != val:
200-
nums[slow] = nums[fast]
201-
slow += 1
202-
203-
# 当 fast 指针遇到要删除的元素时停止赋值
204-
# slow 指针停止移动, fast 指针继续前进
205-
fast += 1
206-
207-
return slow
188+
def removeElement(self, nums: List[int], val: int) -> int:
189+
if nums is None or len(nums)==0:
190+
return 0
191+
l=0
192+
r=len(nums)-1
193+
while l<r:
194+
while(l<r and nums[l]!=val):
195+
l+=1
196+
while(l<r and nums[r]==val):
197+
r-=1
198+
nums[l], nums[r]=nums[r], nums[l]
199+
print(nums)
200+
if nums[l]==val:
201+
return l
202+
else:
203+
return l+1
208204
```
209205

210206

‎problems/0028.实现strStr.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,21 @@ class Solution {
685685
```
686686

687687
Python3:
688-
688+
```python
689+
//暴力解法:
690+
class Solution(object):
691+
def strStr(self, haystack, needle):
692+
"""
693+
:type haystack: str
694+
:type needle: str
695+
:rtype: int
696+
"""
697+
m,n=len(haystack),len(needle)
698+
for i in range(m):
699+
if haystack[i:i+n]==needle:
700+
return i
701+
return -1
702+
```
689703
```python
690704
// 方法一
691705
class Solution:

‎problems/0034.在排序数组中查找元素的第一个和最后一个位置.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
# 34. 在排序数组中查找元素的第一个和最后一个位置
99

10-
[题目链接](https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/)
10+
[力扣链接](https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/)
11+
1112

1213
给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。
1314

‎problems/0046.全排列.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,36 @@ function permute(nums: number[]): number[][] {
359359
};
360360
```
361361

362+
### Rust
363+
364+
```Rust
365+
impl Solution {
366+
fn backtracking(result: &mut Vec<Vec<i32>>, path: &mut Vec<i32>, nums: &Vec<i32>, used: &mut Vec<bool>) {
367+
let len = nums.len();
368+
if path.len() == len {
369+
result.push(path.clone());
370+
return;
371+
}
372+
for i in 0..len {
373+
if used[i] == true { continue; }
374+
used[i] = true;
375+
path.push(nums[i]);
376+
Self::backtracking(result, path, nums, used);
377+
path.pop();
378+
used[i] = false;
379+
}
380+
}
381+
382+
pub fn permute(nums: Vec<i32>) -> Vec<Vec<i32>> {
383+
let mut result: Vec<Vec<i32>> = Vec::new();
384+
let mut path: Vec<i32> = Vec::new();
385+
let mut used = vec![false; nums.len()];
386+
Self::backtracking(&mut result, &mut path, &nums, &mut used);
387+
result
388+
}
389+
}
390+
```
391+
362392
### C
363393

364394
```c

‎problems/0052.N皇后II.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,61 @@ var totalNQueens = function(n) {
144144
};
145145
```
146146

147+
TypeScript:
148+
149+
```typescript
150+
// 0-该格为空,1-该格有皇后
151+
type GridStatus = 0 | 1;
152+
function totalNQueens(n: number): number {
153+
let resCount: number = 0;
154+
const chess: GridStatus[][] = new Array(n).fill(0)
155+
.map(_ => new Array(n).fill(0));
156+
backTracking(chess, n, 0);
157+
return resCount;
158+
function backTracking(chess: GridStatus[][], n: number, startRowIndex: number): void {
159+
if (startRowIndex === n) {
160+
resCount++;
161+
return;
162+
}
163+
for (let j = 0; j < n; j++) {
164+
if (checkValid(chess, startRowIndex, j, n) === true) {
165+
chess[startRowIndex][j] = 1;
166+
backTracking(chess, n, startRowIndex + 1);
167+
chess[startRowIndex][j] = 0;
168+
}
169+
}
170+
}
171+
};
172+
function checkValid(chess: GridStatus[][], i: number, j: number, n: number): boolean {
173+
// 向上纵向检查
174+
let tempI: number = i - 1,
175+
tempJ: number = j;
176+
while (tempI >= 0) {
177+
if (chess[tempI][tempJ] === 1) return false;
178+
tempI--;
179+
}
180+
// 斜向左上检查
181+
tempI = i - 1;
182+
tempJ = j - 1;
183+
while (tempI >= 0 && tempJ >= 0) {
184+
if (chess[tempI][tempJ] === 1) return false;
185+
tempI--;
186+
tempJ--;
187+
}
188+
// 斜向右上检查
189+
tempI = i - 1;
190+
tempJ = j + 1;
191+
while (tempI >= 0 && tempJ < n) {
192+
if (chess[tempI][tempJ] === 1) return false;
193+
tempI--;
194+
tempJ++;
195+
}
196+
return true;
197+
}
198+
```
199+
147200
C
201+
148202
```c
149203
//path[i]为在i行,path[i]列上存在皇后
150204
int *path;

‎problems/0054.螺旋矩阵.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,30 @@ class Solution:
171171

172172
return res
173173
```
174-
174+
```python3
175+
class Solution:
176+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
177+
r=len(matrix)
178+
if r == 0 or len(matrix[0])==0:
179+
return []
180+
c=len(matrix[0])
181+
res=matrix[0]
182+
183+
if r>1:
184+
for i in range (1,r):
185+
res.append(matrix[i][c-1])
186+
for j in range(c-2, -1, -1):
187+
res.append(matrix[r-1][j])
188+
if c>1:
189+
for i in range(r-2, 0, -1):
190+
res.append(matrix[i][0])
191+
192+
M=[]
193+
for k in range(1, r-1):
194+
e=matrix[k][1:-1]
195+
M.append(e)
196+
197+
return res+self.spiralOrder(M)
198+
```
175199
-----------------------
176200
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

‎problems/0134.加油站.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,5 +471,73 @@ int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){
471471
}
472472
```
473473

474+
### Scala
475+
476+
暴力解法:
477+
478+
```scala
479+
object Solution {
480+
def canCompleteCircuit(gas: Array[Int], cost: Array[Int]): Int = {
481+
for (i <- cost.indices) {
482+
var rest = gas(i) - cost(i)
483+
var index = (i + 1) % cost.length // index为i的下一个节点
484+
while (rest > 0 && i != index) {
485+
rest += (gas(index) - cost(index))
486+
index = (index + 1) % cost.length
487+
}
488+
if (rest >= 0 && index == i) return i
489+
}
490+
-1
491+
}
492+
}
493+
```
494+
495+
贪心算法,方法一:
496+
497+
```scala
498+
object Solution {
499+
def canCompleteCircuit(gas: Array[Int], cost: Array[Int]): Int = {
500+
var curSum = 0
501+
var min = Int.MaxValue
502+
for (i <- gas.indices) {
503+
var rest = gas(i) - cost(i)
504+
curSum += rest
505+
min = math.min(min, curSum)
506+
}
507+
if (curSum < 0) return -1 // 情况1: gas的总和小于cost的总和,不可能到达终点
508+
if (min >= 0) return 0 // 情况2: 最小值>=0,从0号出发可以直接到达
509+
// 情况3: min为负值,从后向前看,能把min填平的节点就是出发节点
510+
for (i <- gas.length - 1 to 0 by -1) {
511+
var rest = gas(i) - cost(i)
512+
min += rest
513+
if (min >= 0) return i
514+
}
515+
-1
516+
}
517+
}
518+
```
519+
520+
贪心算法,方法二:
521+
522+
```scala
523+
object Solution {
524+
def canCompleteCircuit(gas: Array[Int], cost: Array[Int]): Int = {
525+
var curSum = 0
526+
var totalSum = 0
527+
var start = 0
528+
for (i <- gas.indices) {
529+
curSum += (gas(i) - cost(i))
530+
totalSum += (gas(i) - cost(i))
531+
if (curSum < 0) {
532+
start = i + 1 // 起始位置更新
533+
curSum = 0 // curSum从0开始
534+
}
535+
}
536+
if (totalSum < 0) return -1 // 说明怎么走不可能跑一圈
537+
start
538+
}
539+
}
540+
```
541+
474542
-----------------------
475543
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

‎problems/0209.长度最小的子数组.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,27 @@ class Solution:
179179
index += 1
180180
return 0 if res==float("inf") else res
181181
```
182-
183-
182+
```python3
183+
#滑动窗口
184+
class Solution:
185+
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
186+
if nums is None or len(nums)==0:
187+
return 0
188+
lenf=len(nums)+1
189+
total=0
190+
i=j=0
191+
while (j<len(nums)):
192+
total=total+nums[j]
193+
j+=1
194+
while (total>=target):
195+
lenf=min(lenf,j-i)
196+
total=total-nums[i]
197+
i+=1
198+
if lenf==len(nums)+1:
199+
return 0
200+
else:
201+
return lenf
202+
```
184203
Go:
185204
```go
186205
func minSubArrayLen(target int, nums []int) int {

0 commit comments

Comments
(0)

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