Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
youngyangyang04 committed Jun 4, 2021
2 parents b503c62 + cc4275b commit 508a6e7
Show file tree
Hide file tree
Showing 23 changed files with 805 additions and 23 deletions.
15 changes: 15 additions & 0 deletions problems/0001.两数之和.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,21 @@ func twoSum(nums []int, target int) []int {
}
```

```go
// 使用map方式解题,降低时间复杂度
func twoSum(nums []int, target int) []int {
m := make(map[int]int)
for index, val := range nums {
if preIndex, ok := m[target-val]; ok {
return []int{preIndex, index}
} else {
m[val] = index
}
}
return []int{}
}
```

Rust

```rust
Expand Down
90 changes: 89 additions & 1 deletion problems/0028.实现strStr.md
Original file line number Diff line number Diff line change
Expand Up @@ -721,10 +721,98 @@ class Solution:

Go:

```go
// 方法一:前缀表使用减1实现

// getNext 构造前缀表next
// params:
// next 前缀表数组
// s 模式串
func getNext(next []int, s string) {
j := -1 // j表示 最长相等前后缀长度
next[0] = j

for i := 1; i < len(s); i++ {
for j >= 0 && s[i] != s[j+1] {
j = next[j] // 回退前一位
}
if s[i] == s[j+1] {
j++
}
next[i] = j // next[i]是i(包括i)之前的最长相等前后缀长度
}
}
func strStr(haystack string, needle string) int {
if len(needle) == 0 {
return 0
}
next := make([]int, len(needle))
getNext(next, needle)
j := -1 // 模式串的起始位置 next为-1 因此也为-1
for i := 0; i < len(haystack); i++ {
for j >= 0 && haystack[i] != needle[j+1] {
j = next[j] // 寻找下一个匹配点
}
if haystack[i] == needle[j+1] {
j++
}
if j == len(needle)-1 { // j指向了模式串的末尾
return i - len(needle) + 1
}
}
return -1
}
```

```go
// 方法二: 前缀表无减一或者右移

// getNext 构造前缀表next
// params:
// next 前缀表数组
// s 模式串
func getNext(next []int, s string) {
j := 0
next[0] = j
for i := 1; i < len(s); i++ {
for j > 0 && s[i] != s[j] {
j = next[j-1]
}
if s[i] == s[j] {
j++
}
next[i] = j
}
}
func strStr(haystack string, needle string) int {
n := len(needle)
if n == 0 {
return 0
}
j := 0
next := make([]int, n)
getNext(next, needle)
for i := 0; i < len(haystack); i++ {
for j > 0 && haystack[i] != needle[j] {
j = next[j-1] // 回退到j的前一位
}
if haystack[i] == needle[j] {
j++
}
if j == n {
return i - n + 1
}
}
return -1
}
```





-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
34 changes: 33 additions & 1 deletion problems/0037.解数独.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,39 @@ class Solution {
```

Python:

```python3
class Solution:
def solveSudoku(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
def backtrack(board):
for i in range(len(board)): #遍历行
for j in range(len(board[0])): #遍历列
if board[i][j] != ".": continue
for k in range(1,10): #(i, j) 这个位置放k是否合适
if isValid(i,j,k,board):
board[i][j] = str(k) #放置k
if backtrack(board): return True #如果找到合适一组立刻返回
board[i][j] = "." #回溯,撤销k
return False #9个数都试完了,都不行,那么就返回false
return True #遍历完没有返回false,说明找到了合适棋盘位置了
def isValid(row,col,val,board):
for i in range(9): #判断行里是否重复
if board[row][i] == str(val):
return False
for j in range(9): #判断列里是否重复
if board[j][col] == str(val):
return False
startRow = (row // 3) * 3
startcol = (col // 3) * 3
for i in range(startRow,startRow + 3): #判断9方格里是否重复
for j in range(startcol,startcol + 3):
if board[i][j] == str(val):
return False
return True
backtrack(board)
```

Go:

Expand Down
11 changes: 1 addition & 10 deletions problems/0056.合并区间.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,7 @@ Java:
class Solution {
public int[][] merge(int[][] intervals) {
List<int[]> res = new LinkedList<>();
Arrays.sort(intervals, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if (o1[0] != o2[0]) {
return Integer.compare(o1[0],o2[0]);
} else {
return Integer.compare(o1[1],o2[1]);
}
}
});
Arrays.sort(intervals, (o1, o2) -> Integer.compare(o1[0], o2[0]));

int start = intervals[0][0];
for (int i = 1; i < intervals.length; i++) {
Expand Down
48 changes: 48 additions & 0 deletions problems/0101.对称二叉树.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,54 @@ Python:

Go:

```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
// 递归
func defs(left *TreeNode, right *TreeNode) bool {
if left == nil && right == nil {
return true;
};
if left == nil || right == nil {
return false;
};
if left.Val != right.Val {
return false;
}
return defs(left.Left, right.Right) && defs(right.Left, left.Right);
}
func isSymmetric(root *TreeNode) bool {
return defs(root.Left, root.Right);
}

// 迭代
func isSymmetric(root *TreeNode) bool {
var queue []*TreeNode;
if root != nil {
queue = append(queue, root.Left, root.Right);
}
for len(queue) > 0 {
left := queue[0];
right := queue[1];
queue = queue[2:];
if left == nil && right == nil {
continue;
}
if left == nil || right == nil || left.Val != right.Val {
return false;
};
queue = append(queue, left.Left, right.Right, right.Left, left.Right);
}
return true;
}
```


JavaScript
```javascript
Expand Down
49 changes: 49 additions & 0 deletions problems/0104.二叉树的最大深度.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,55 @@ Python:

Go:

```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func max (a, b int) int {
if a > b {
return a;
}
return b;
}
// 递归
func maxDepth(root *TreeNode) int {
if root == nil {
return 0;
}
return max(maxDepth(root.Left), maxDepth(root.Right)) + 1;
}

// 遍历
func maxDepth(root *TreeNode) int {
levl := 0;
queue := make([]*TreeNode, 0);
if root != nil {
queue = append(queue, root);
}
for l := len(queue); l > 0; {
for ;l > 0;l-- {
node := queue[0];
if node.Left != nil {
queue = append(queue, node.Left);
}
if node.Right != nil {
queue = append(queue, node.Right);
}
queue = queue[1:];
}
levl++;
l = len(queue);
}
return levl;
}

```


JavaScript
```javascript
Expand Down
58 changes: 58 additions & 0 deletions problems/0111.二叉树的最小深度.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,64 @@ class Solution:

Go:

```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func min(a, b int) int {
if a < b {
return a;
}
return b;
}
// 递归
func minDepth(root *TreeNode) int {
if root == nil {
return 0;
}
if root.Left == nil && root.Right != nil {
return 1 + minDepth(root.Right);
}
if root.Right == nil && root.Left != nil {
return 1 + minDepth(root.Left);
}
return min(minDepth(root.Left), minDepth(root.Right)) + 1;
}

// 迭代

func minDepth(root *TreeNode) int {
dep := 0;
queue := make([]*TreeNode, 0);
if root != nil {
queue = append(queue, root);
}
for l := len(queue); l > 0; {
dep++;
for ; l > 0; l-- {
node := queue[0];
if node.Left == nil && node.Right == nil {
return dep;
}
if node.Left != nil {
queue = append(queue, node.Left);
}
if node.Right != nil {
queue = append(queue, node.Right);
}
queue = queue[1:];
}
l = len(queue);
}
return dep;
}
```


JavaScript:

Expand Down
30 changes: 30 additions & 0 deletions problems/0112.路径总和.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,36 @@ class Solution {
return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
}
}
```
迭代
```java
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if(root==null)return false;
Stack<TreeNode> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
stack1.push(root);stack2.push(root.val);
while(!stack1.isEmpty()){
int size = stack1.size();
for(int i=0;i<size;i++){
TreeNode node = stack1.pop();int sum=stack2.pop();
// 如果该节点是叶子节点了,同时该节点的路径数值等于sum,那么就返回true
if(node.left==null && node.right==null && sum==targetSum)return true;
// 右节点,压进去一个节点的时候,将该节点的路径数值也记录下来
if(node.right!=null){
stack1.push(node.right);stack2.push(sum+node.right.val);
}
// 左节点,压进去一个节点的时候,将该节点的路径数值也记录下来
if(node.left!=null){
stack1.push(node.left);stack2.push(sum+node.left.val);
}
}
}
return false;
}

}

```

0113.路径总和-ii
Expand Down
Loading

0 comments on commit 508a6e7

Please sign in to comment.