Skip to content

Commit 69861d9

Browse files
authored
Merge branch 'youngyangyang04:master' into master
2 parents 186d4e4 + 6609258 commit 69861d9

File tree

83 files changed

+1797
-498
lines changed

Some content is hidden

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

83 files changed

+1797
-498
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: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -214,19 +214,17 @@ end
214214
```
215215
Rust:
216216
```rust
217-
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> &mut Vec<i32> {
218-
let mut start: usize = 0;
219-
while start < nums.len() {
220-
if nums[start] == val {
221-
nums.remove(start);
217+
impl Solution {
218+
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
219+
let mut slowIdx = 0;
220+
for pos in (0..nums.len()) {
221+
if nums[pos]!=val {
222+
nums[slowIdx] = nums[pos];
223+
slowIdx += 1;
224+
}
222225
}
223-
start += 1;
226+
return (slowIdx) as i32;
224227
}
225-
nums
226-
}
227-
fn main() {
228-
let mut nums = vec![5,1,3,5,2,3,4,1];
229-
println!("{:?}",remove_element(&mut nums, 5));
230228
}
231229
```
232230

@@ -248,6 +246,22 @@ func removeElement(_ nums: inout [Int], _ val: Int) -> Int {
248246
}
249247
```
250248

249+
C:
250+
```c
251+
int removeElement(int* nums, int numsSize, int val){
252+
int slow = 0;
253+
for(int fast = 0; fast < numsSize; fast++) {
254+
//若快指针位置的元素不等于要删除的元素
255+
if(nums[fast] != val) {
256+
//将其挪到慢指针指向的位置,慢指针+1
257+
nums[slow++] = nums[fast];
258+
}
259+
}
260+
//最后慢指针的大小就是新的数组的大小
261+
return slow;
262+
}
263+
```
264+
251265
-----------------------
252266
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
253267
* B站视频:[代码随想录](https://space.bilibili.com/525438321)

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/0051.N皇后.md

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

342342
public boolean isValid(int row, int col, int n, char[][] chessboard) {
343343
// 检查列
344-
for (int i=0; i<n; ++i) {
344+
for (int i=0; i<row; ++i) { // 相当于剪枝
345345
if (chessboard[i][col] == 'Q') {
346346
return false;
347347
}

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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,58 @@ func generateMatrix(_ n: Int) -> [[Int]] {
373373
}
374374
```
375375

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+
```
376428

377429

378430
-----------------------

problems/0077.组合.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,59 @@ func backtrack(n,k,start int,track []int){
435435
}
436436
```
437437

438+
C:
439+
```c
440+
int* path;
441+
int pathTop;
442+
int** ans;
443+
int ansTop;
444+
445+
void backtracking(int n, int k,int startIndex) {
446+
//当path中元素个数为k个时,我们需要将path数组放入ans二维数组中
447+
if(pathTop == k) {
448+
//path数组为我们动态申请,若直接将其地址放入二维数组,path数组中的值会随着我们回溯而逐渐变化
449+
//因此创建新的数组存储path中的值
450+
int* temp = (int*)malloc(sizeof(int) * k);
451+
int i;
452+
for(i = 0; i < k; i++) {
453+
temp[i] = path[i];
454+
}
455+
ans[ansTop++] = temp;
456+
return ;
457+
}
458+
459+
int j;
460+
for(j = startIndex; j <=n ;j++) {
461+
//将当前结点放入path数组
462+
path[pathTop++] = j;
463+
//进行递归
464+
backtracking(n, k, j + 1);
465+
//进行回溯,将数组最上层结点弹出
466+
pathTop--;
467+
}
468+
}
469+
470+
int** combine(int n, int k, int* returnSize, int** returnColumnSizes){
471+
//path数组存储符合条件的结果
472+
path = (int*)malloc(sizeof(int) * k);
473+
//ans二维数组存储符合条件的结果数组的集合。(数组足够大,避免极端情况)
474+
ans = (int**)malloc(sizeof(int*) * 10000);
475+
pathTop = ansTop = 0;
476+
477+
//回溯算法
478+
backtracking(n, k, 1);
479+
//最后的返回大小为ans数组大小
480+
*returnSize = ansTop;
481+
//returnColumnSizes数组存储ans二维数组对应下标中一维数组的长度(都为k)
482+
*returnColumnSizes = (int*)malloc(sizeof(int) *(*returnSize));
483+
int i;
484+
for(i = 0; i < *returnSize; i++) {
485+
(*returnColumnSizes)[i] = k;
486+
}
487+
//返回ans二维数组
488+
return ans;
489+
}
490+
```
438491
439492
-----------------------
440493
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

problems/0104.二叉树的最大深度.md

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,35 @@ class solution {
311311
}
312312
```
313313

314+
### 559.n叉树的最大深度
315+
```java
316+
class solution {
317+
/**
318+
* 迭代法,使用层序遍历
319+
*/
320+
public int maxDepth(Node root) {
321+
if (root == null) return 0;
322+
int depth = 0;
323+
Queue<Node> que = new LinkedList<>();
324+
que.offer(root);
325+
while (!que.isEmpty())
326+
{
327+
depth ++;
328+
int len = que.size();
329+
while (len > 0)
330+
{
331+
Node node = que.poll();
332+
for (int i = 0; i < node.children.size(); i++)
333+
if (node.children.get(i) != null)
334+
que.offer(node.children.get(i));
335+
len--;
336+
}
337+
}
338+
return depth;
339+
}
340+
}
341+
```
342+
314343
## python
315344

316345
### 104.二叉树的最大深度
@@ -505,21 +534,51 @@ var maxdepth = function(root) {
505534

506535
二叉树最大深度层级遍历
507536
```javascript
508-
var maxdepth = function(root) {
509-
//使用递归的方法 递归三部曲
510-
//1. 确定递归函数的参数和返回值
511-
const getdepth=function(node){
512-
//2. 确定终止条件
513-
if(node===null){
514-
return 0;
537+
var maxDepth = function(root) {
538+
if(!root) return 0
539+
let count = 0
540+
const queue = [root]
541+
while(queue.length) {
542+
let size = queue.length
543+
/* 层数+1 */
544+
count++
545+
while(size--) {
546+
let node = queue.shift();
547+
node.left && queue.push(node.left);
548+
node.right && queue.push(node.right);
549+
}
550+
}
551+
return count
552+
};
553+
```
554+
555+
N叉树的最大深度 递归写法
556+
```js
557+
var maxDepth = function(root) {
558+
if(!root) return 0
559+
let depth = 0
560+
for(let node of root.children) {
561+
depth = Math.max(depth, maxDepth(node))
562+
}
563+
return depth + 1
564+
}
565+
```
566+
567+
N叉树的最大深度 层序遍历
568+
```js
569+
var maxDepth = function(root) {
570+
if(!root) return 0
571+
let count = 0
572+
let queue = [root]
573+
while(queue.length) {
574+
let size = queue.length
575+
count++
576+
while(size--) {
577+
let node = queue.shift()
578+
node && (queue = [...queue, ...node.children])
515579
}
516-
//3. 确定单层逻辑
517-
let leftdepth=getdepth(node.left);
518-
let rightdepth=getdepth(node.right);
519-
let depth=1+math.max(leftdepth,rightdepth);
520-
return depth;
521580
}
522-
return getDepth(root);
581+
return count
523582
};
524583
```
525584

0 commit comments

Comments
 (0)