Skip to content

Commit b56fa24

Browse files
Merge branch 'master' into remote
2 parents 8a35955 + f1b2279 commit b56fa24

File tree

54 files changed

+1357
-288
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1357
-288
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
👉 推荐 [在线阅读](http://programmercarl.com/) (Github在国内访问经常不稳定)
1+
👉 推荐 [在线阅读](http://programmercarl.com/) (Github在国内访问经常不稳定)
22
👉 推荐 [Gitee同步](https://gitee.com/programmercarl/leetcode-master)
33

44
> 1. **介绍**:本项目是一套完整的刷题计划,旨在帮助大家少走弯路,循序渐进学算法,[关注作者](#关于作者)
@@ -494,6 +494,7 @@
494494
## 图论
495495
* [463.岛屿的周长](./problems/0463.岛屿的周长.md) (模拟)
496496
* [841.钥匙和房间](./problems/0841.钥匙和房间.md) 【有向图】dfs,bfs都可以
497+
* [127.单词接龙](./problems/0127.单词接龙.md) 广搜
497498

498499
## 并查集
499500
* [684.冗余连接](./problems/0684.冗余连接.md) 【并查集基础题目】

problems/0001.两数之和.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,23 @@ function twoSum(array $nums, int $target): array
206206
}
207207
```
208208

209+
Swift:
210+
```swift
211+
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
212+
var res = [Int]()
213+
var dict = [Int : Int]()
214+
for i in 0 ..< nums.count {
215+
let other = target - nums[i]
216+
if dict.keys.contains(other) {
217+
res.append(i)
218+
res.append(dict[other]!)
219+
return res
220+
}
221+
dict[nums[i]] = i
222+
}
223+
return res
224+
}
225+
```
209226

210227
-----------------------
211228
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

problems/0027.移除元素.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,30 @@ func removeElement(_ nums: inout [Int], _ val: Int) -> Int {
246246
}
247247
```
248248

249+
PHP:
250+
```php
251+
class Solution {
252+
/**
253+
* @param Integer[] $nums
254+
* @param Integer $val
255+
* @return Integer
256+
*/
257+
function removeElement(&$nums, $val) {
258+
if (count($nums) == 0) {
259+
return 0;
260+
}
261+
// 快慢指针
262+
$slow = 0;
263+
for ($fast = 0; $fast < count($nums); $fast++) {
264+
if ($nums[$fast] != $val) {
265+
$nums[$slow] = $nums[$fast];
266+
$slow++;
267+
}
268+
}
269+
return $slow;
270+
}
271+
```
272+
249273
C:
250274
```c
251275
int removeElement(int* nums, int numsSize, int val){

problems/0046.全排列.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,32 @@ class Solution {
183183
}
184184
}
185185
```
186+
```java
187+
// 解法2:通过判断path中是否存在数字,排除已经选择的数字
188+
class Solution {
189+
List<List<Integer>> result = new ArrayList<>();
190+
LinkedList<Integer> path = new LinkedList<>();
191+
public List<List<Integer>> permute(int[] nums) {
192+
if (nums.length == 0) return result;
193+
backtrack(nums, path);
194+
return result;
195+
}
196+
public void backtrack(int[] nums, LinkedList<Integer> path) {
197+
if (path.size() == nums.length) {
198+
result.add(new ArrayList<>(path));
199+
}
200+
for (int i =0; i < nums.length; i++) {
201+
// 如果path中已有,则跳过
202+
if (path.contains(nums[i])) {
203+
continue;
204+
}
205+
path.add(nums[i]);
206+
backtrack(nums, path);
207+
path.removeLast();
208+
}
209+
}
210+
}
211+
```
186212

187213
Python:
188214
```python3

problems/0056.合并区间.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,28 @@ class Solution {
157157
}
158158
}
159159
```
160+
```java
161+
// 版本2
162+
class Solution {
163+
public int[][] merge(int[][] intervals) {
164+
LinkedList<int[]> res = new LinkedList<>();
165+
Arrays.sort(intervals, (o1, o2) -> Integer.compare(o1[0], o2[0]));
166+
res.add(intervals[0]);
167+
for (int i = 1; i < intervals.length; i++) {
168+
if (intervals[i][0] <= res.getLast()[1]) {
169+
int start = res.getLast()[0];
170+
int end = Math.max(intervals[i][1], res.getLast()[1]);
171+
res.removeLast();
172+
res.add(new int[]{start, end});
173+
}
174+
else {
175+
res.add(intervals[i]);
176+
}
177+
}
178+
return res.toArray(new int[res.size()][]);
179+
}
180+
}
181+
```
160182

161183
Python:
162184
```python

problems/0059.螺旋矩阵II.md

Lines changed: 132 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -191,33 +191,48 @@ class Solution {
191191

192192
python:
193193

194-
```python
194+
```python3
195195
class Solution:
196+
196197
def generateMatrix(self, n: int) -> List[List[int]]:
197-
left, right, up, down = 0, n-1, 0, n-1
198-
matrix = [ [0]*n for _ in range(n)]
199-
num = 1
200-
while left<=right and up<=down:
201-
# 填充左到右
202-
for i in range(left, right+1):
203-
matrix[up][i] = num
204-
num += 1
205-
up += 1
206-
# 填充上到下
207-
for i in range(up, down+1):
208-
matrix[i][right] = num
209-
num += 1
198+
# 初始化要填充的正方形
199+
matrix = [[0] * n for _ in range(n)]
200+
201+
left, right, up, down = 0, n - 1, 0, n - 1
202+
number = 1 # 要填充的数字
203+
204+
while left < right and up < down:
205+
206+
# 从左到右填充上边
207+
for x in range(left, right):
208+
matrix[up][x] = number
209+
number += 1
210+
211+
# 从上到下填充右边
212+
for y in range(up, down):
213+
matrix[y][right] = number
214+
number += 1
215+
216+
# 从右到左填充下边
217+
for x in range(right, left, -1):
218+
matrix[down][x] = number
219+
number += 1
220+
221+
# 从下到上填充左边
222+
for y in range(down, up, -1):
223+
matrix[y][left] = number
224+
number += 1
225+
226+
# 缩小要填充的范围
227+
left += 1
210228
right -= 1
211-
# 填充右到左
212-
for i in range(right, left-1, -1):
213-
matrix[down][i] = num
214-
num += 1
229+
up += 1
215230
down -= 1
216-
# 填充下到上
217-
for i in range(down, up-1, -1):
218-
matrix[i][left] = num
219-
num += 1
220-
left += 1
231+
232+
# 如果阶数为奇数,额外填充一次中心
233+
if n % 2:
234+
matrix[n // 2][n // 2] = number
235+
221236
return matrix
222237
```
223238

@@ -358,6 +373,100 @@ func generateMatrix(_ n: Int) -> [[Int]] {
358373
}
359374
```
360375

376+
Rust:
377+
378+
```rust
379+
impl Solution {
380+
pub fn generate_matrix(n: i32) -> Vec<Vec<i32>> {
381+
let mut res = vec![vec![0; n as usize]; n as usize];
382+
let (mut startX, mut startY, mut offset): (usize, usize, usize) = (0, 0, 1);
383+
let mut loopIdx = n/2;
384+
let mid: usize = loopIdx as usize;
385+
let mut count = 1;
386+
let (mut i, mut j): (usize, usize) = (0, 0);
387+
while loopIdx > 0 {
388+
i = startX;
389+
j = startY;
390+
391+
while j < (startY + (n as usize) - offset) {
392+
res[i][j] = count;
393+
count += 1;
394+
j += 1;
395+
}
396+
397+
while i < (startX + (n as usize) - offset) {
398+
res[i][j] = count;
399+
count += 1;
400+
i += 1;
401+
}
402+
403+
while j > startY {
404+
res[i][j] = count;
405+
count += 1;
406+
j -= 1;
407+
}
408+
409+
while i > startX {
410+
res[i][j] = count;
411+
count += 1;
412+
i -= 1;
413+
}
414+
415+
startX += 1;
416+
startY += 1;
417+
offset += 2;
418+
loopIdx -= 1;
419+
}
420+
421+
if(n % 2 == 1) {
422+
res[mid][mid] = count;
423+
}
424+
res
425+
}
426+
}
427+
```
428+
429+
PHP:
430+
```php
431+
class Solution {
432+
/**
433+
* @param Integer $n
434+
* @return Integer[][]
435+
*/
436+
function generateMatrix($n) {
437+
// 初始化数组
438+
$res = array_fill(0, $n, array_fill(0, $n, 0));
439+
$mid = $loop = floor($n / 2);
440+
$startX = $startY = 0;
441+
$offset = 1;
442+
$count = 1;
443+
while ($loop > 0) {
444+
$i = $startX;
445+
$j = $startY;
446+
for (; $j < $startY + $n - $offset; $j++) {
447+
$res[$i][$j] = $count++;
448+
}
449+
for (; $i < $startX + $n - $offset; $i++) {
450+
$res[$i][$j] = $count++;
451+
}
452+
for (; $j > $startY; $j--) {
453+
$res[$i][$j] = $count++;
454+
}
455+
for (; $i > $startX; $i--) {
456+
$res[$i][$j] = $count++;
457+
}
458+
$startX += 1;
459+
$startY += 1;
460+
$offset += 2;
461+
$loop--;
462+
}
463+
if ($n % 2 == 1) {
464+
$res[$mid][$mid] = $count;
465+
}
466+
return $res;
467+
}
468+
}
469+
```
361470

362471

363472
-----------------------

problems/0115.不同的子序列.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,30 @@ class SolutionDP2:
221221
```
222222

223223
Go:
224+
```go
225+
func numDistinct(s string, t string) int {
226+
dp:= make([][]int,len(s)+1)
227+
for i:=0;i<len(dp);i++{
228+
dp[i] = make([]int,len(t)+1)
229+
}
230+
// 初始化
231+
for i:=0;i<len(dp);i++{
232+
dp[i][0] = 1
233+
}
234+
// dp[0][j] 为 0,默认值,因此不需要初始化
235+
for i:=1;i<len(dp);i++{
236+
for j:=1;j<len(dp[i]);j++{
237+
if s[i-1] == t[j-1]{
238+
dp[i][j] = dp[i-1][j-1] + dp[i-1][j]
239+
}else{
240+
dp[i][j] = dp[i-1][j]
241+
}
242+
}
243+
}
244+
return dp[len(dp)-1][len(dp[0])-1]
245+
}
246+
```
247+
224248

225249
Javascript:
226250
```javascript

0 commit comments

Comments
 (0)