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 c366f26

Browse files
authored
Merge branch 'youngyangyang04:master' into master
2 parents aefb7aa + f16c640 commit c366f26

File tree

56 files changed

+3529
-923
lines changed

Some content is hidden

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

56 files changed

+3529
-923
lines changed

‎README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@
107107
5. [数组:209.长度最小的子数组](./problems/0209.长度最小的子数组.md)
108108
6. [数组:区间和](./problems/kamacoder/0058.区间和.md)
109109
6. [数组:59.螺旋矩阵II](./problems/0059.螺旋矩阵II.md)
110-
7. [数组:总结篇](./problems/数组总结篇.md)
110+
7. [数组:区间和](./problems/kamacoder/0058.区间和.md)
111+
8. [数组:开发商购买土地](./problems/kamacoder/0044.开发商购买土地.md)
112+
9. [数组:总结篇](./problems/数组总结篇.md)
111113

112114
## 链表
113115

‎problems/0018.四数之和.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ class Solution {
264264
265265
// nums[i]+nums[j] > target 直接返回, 剪枝操作
266266
if (nums[i]+nums[j] > 0 && nums[i]+nums[j] > target) {
267-
return result;
267+
break;
268268
}
269269
270270
if (j > i + 1 && nums[j - 1] == nums[j]) { // 对nums[j]去重

‎problems/0046.全排列.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242

4343
我以[1,2,3]为例,抽象成树形结构如下:
4444

45-
![46.全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20211027181706.png)
45+
46+
![全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20240803180318.png)
4647

4748
### 回溯三部曲
4849

@@ -54,7 +55,7 @@
5455

5556
但排列问题需要一个used数组,标记已经选择的元素,如图橘黄色部分所示:
5657

57-
![46.全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20211027181706.png)
58+
![全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20240803180318.png)
5859

5960
代码如下:
6061

@@ -66,7 +67,7 @@ void backtracking (vector<int>& nums, vector<bool>& used)
6667
6768
* 递归终止条件
6869
69-
![46.全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209174225145.png)
70+
![全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20240803180318.png)
7071
7172
可以看出叶子节点,就是收割结果的地方。
7273

‎problems/0062.不同路径.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ class Solution:
371371
```
372372
### Go
373373

374+
动态规划
374375
```Go
375376
func uniquePaths(m int, n int) int {
376377
dp := make([][]int, m)
@@ -390,6 +391,26 @@ func uniquePaths(m int, n int) int {
390391
}
391392
```
392393

394+
数论方法
395+
```Go
396+
func uniquePaths(m int, n int) int {
397+
numerator := 1
398+
denominator := m - 1
399+
count := m - 1
400+
t := m + n - 2
401+
for count > 0 {
402+
numerator *= t
403+
t--
404+
for denominator != 0 && numerator % denominator == 0 {
405+
numerator /= denominator
406+
denominator--
407+
}
408+
count--
409+
}
410+
return numerator
411+
}
412+
```
413+
393414
### Javascript
394415

395416
```Javascript

‎problems/0090.子集II.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,43 @@ class Solution:
310310
```
311311
### Go
312312

313+
使用used数组
314+
```Go
315+
var (
316+
result [][]int
317+
path []int
318+
)
319+
320+
func subsetsWithDup(nums []int) [][]int {
321+
result = make([][]int, 0)
322+
path = make([]int, 0)
323+
used := make([]bool, len(nums))
324+
sort.Ints(nums) // 去重需要排序
325+
backtracing(nums, 0, used)
326+
return result
327+
}
328+
329+
func backtracing(nums []int, startIndex int, used []bool) {
330+
tmp := make([]int, len(path))
331+
copy(tmp, path)
332+
result = append(result, tmp)
333+
for i := startIndex; i < len(nums); i++ {
334+
// used[i - 1] == true,说明同一树枝candidates[i - 1]使用过
335+
// used[i - 1] == false,说明同一树层candidates[i - 1]使用过
336+
// 而我们要对同一树层使用过的元素进行跳过
337+
if i > 0 && nums[i] == nums[i-1] && used[i-1] == false {
338+
continue
339+
}
340+
path = append(path, nums[i])
341+
used[i] = true
342+
backtracing(nums, i + 1, used)
343+
path = path[:len(path)-1]
344+
used[i] = false
345+
}
346+
}
347+
```
348+
349+
不使用used数组
313350
```Go
314351
var (
315352
path []int

‎problems/0093.复原IP地址.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ for (int i = startIndex; i < s.size(); i++) {
143143
代码如下:
144144

145145
```CPP
146-
// 判断字符串s在左闭又闭区间[start, end]所组成的数字是否合法
146+
// 判断字符串s在左闭右闭区间[start, end]所组成的数字是否合法
147147
bool isValid(const string& s, int start, int end) {
148148
if (start > end) {
149149
return false;
@@ -208,7 +208,7 @@ private:
208208
} else break; // 不合法,直接结束本层循环
209209
}
210210
}
211-
// 判断字符串s在左闭又闭区间[start, end]所组成的数字是否合法
211+
// 判断字符串s在左闭右闭区间[start, end]所组成的数字是否合法
212212
bool isValid(const string& s, int start, int end) {
213213
if (start > end) {
214214
return false;

‎problems/0110.平衡二叉树.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,8 @@ class Solution:
623623
```
624624
### Go:
625625

626+
递归法
627+
626628
```Go
627629
func isBalanced(root *TreeNode) bool {
628630
h := getHeight(root)
@@ -653,6 +655,64 @@ func max(a, b int) int {
653655
}
654656
```
655657

658+
迭代法
659+
660+
```Go
661+
func isBalanced(root *TreeNode) bool {
662+
st := make([]*TreeNode, 0)
663+
if root == nil {
664+
return true
665+
}
666+
st = append(st, root)
667+
for len(st) > 0 {
668+
node := st[len(st)-1]
669+
st = st[:len(st)-1]
670+
if math.Abs(float64(getDepth(node.Left)) - float64(getDepth(node.Right))) > 1 {
671+
return false
672+
}
673+
if node.Right != nil {
674+
st = append(st, node.Right)
675+
}
676+
if node.Left != nil {
677+
st = append(st, node.Left)
678+
}
679+
}
680+
return true
681+
}
682+
683+
func getDepth(cur *TreeNode) int {
684+
st := make([]*TreeNode, 0)
685+
if cur != nil {
686+
st = append(st, cur)
687+
}
688+
depth := 0
689+
result := 0
690+
for len(st) > 0 {
691+
node := st[len(st)-1]
692+
if node != nil {
693+
st = st[:len(st)-1]
694+
st = append(st, node, nil)
695+
depth++
696+
if node.Right != nil {
697+
st = append(st, node.Right)
698+
}
699+
if node.Left != nil {
700+
st = append(st, node.Left)
701+
}
702+
} else {
703+
st = st[:len(st)-1]
704+
node = st[len(st)-1]
705+
st = st[:len(st)-1]
706+
depth--
707+
}
708+
if result < depth {
709+
result = depth
710+
}
711+
}
712+
return result
713+
}
714+
```
715+
656716
### JavaScript:
657717

658718
递归法:

‎problems/0112.路径总和.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,48 @@ class Solution:
727727

728728
```go
729729
//递归法
730+
/**
731+
* Definition for a binary tree node.
732+
* type TreeNode struct {
733+
* Val int
734+
* Left *TreeNode
735+
* Right *TreeNode
736+
* }
737+
*/
738+
func hasPathSum(root *TreeNode, targetSum int) bool {
739+
if root == nil {
740+
return false
741+
}
742+
return traversal(root, targetSum - root.Val)
743+
}
744+
745+
func traversal(cur *TreeNode, count int) bool {
746+
if cur.Left == nil && cur.Right == nil && count == 0 {
747+
return true
748+
}
749+
if cur.Left == nil && cur.Right == nil {
750+
return false
751+
}
752+
if cur.Left != nil {
753+
count -= cur.Left.Val
754+
if traversal(cur.Left, count) {
755+
return true
756+
}
757+
count += cur.Left.Val
758+
}
759+
if cur.Right != nil {
760+
count -= cur.Right.Val
761+
if traversal(cur.Right, count) {
762+
return true
763+
}
764+
count += cur.Right.Val
765+
}
766+
return false
767+
}
768+
```
769+
770+
```go
771+
//递归法精简
730772
/**
731773
* Definition for a binary tree node.
732774
* type TreeNode struct {

‎problems/0121.买卖股票的最佳时机.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ class Solution:
308308
class Solution:
309309
def maxProfit(self, prices: List[int]) -> int:
310310
length = len(prices)
311-
if len == 0:
311+
if length == 0:
312312
return 0
313313
dp = [[0] * 2 for _ in range(length)]
314314
dp[0][0] = -prices[0]

‎problems/0131.分割回文串.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ class Solution:
527527

528528
```
529529
### Go
530+
回溯 基本版
530531
```go
531532
var (
532533
path []string // 放已经回文的子串
@@ -565,6 +566,63 @@ func isPalindrome(s string) bool {
565566
}
566567
```
567568

569+
回溯+动态规划优化回文串判断
570+
```go
571+
var (
572+
result [][]string
573+
path []string // 放已经回文的子串
574+
isPalindrome [][]bool // 放事先计算好的是否回文子串的结果
575+
)
576+
577+
func partition(s string) [][]string {
578+
result = make([][]string, 0)
579+
path = make([]string, 0)
580+
computePalindrome(s)
581+
backtracing(s, 0)
582+
return result
583+
}
584+
585+
func backtracing(s string, startIndex int) {
586+
// 如果起始位置已经大于s的大小,说明已经找到了一组分割方案了
587+
if startIndex >= len(s) {
588+
tmp := make([]string, len(path))
589+
copy(tmp, path)
590+
result = append(result, tmp)
591+
return
592+
}
593+
for i := startIndex; i < len(s); i++ {
594+
if isPalindrome[startIndex][i] { // 是回文子串
595+
// 获取[startIndex,i]在s中的子串
596+
path = append(path, s[startIndex:i+1])
597+
} else { // 不是回文,跳过
598+
continue
599+
}
600+
backtracing(s, i + 1) // 寻找i+1为起始位置的子串
601+
path = path[:len(path)-1] // 回溯过程,弹出本次已经添加的子串
602+
}
603+
}
604+
605+
func computePalindrome(s string) {
606+
// isPalindrome[i][j] 代表 s[i:j](双边包括)是否是回文字串
607+
isPalindrome = make([][]bool, len(s))
608+
for i := 0; i < len(isPalindrome); i++ {
609+
isPalindrome[i] = make([]bool, len(s))
610+
}
611+
for i := len(s)-1; i >= 0; i-- {
612+
// 需要倒序计算, 保证在i行时, i+1行已经计算好了
613+
for j := i; j < len(s); j++ {
614+
if j == i {
615+
isPalindrome[i][j] = true
616+
} else if j - i == 1 {
617+
isPalindrome[i][j] = s[i] == s[j]
618+
} else {
619+
isPalindrome[i][j] = s[i] == s[j] && isPalindrome[i+1][j-1]
620+
}
621+
}
622+
}
623+
}
624+
```
625+
568626
### JavaScript
569627

570628
```js

0 commit comments

Comments
(0)

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