Skip to content

Commit f3b0b39

Browse files
committed
📝 update alg lc
1 parent 48cfc72 commit f3b0b39

26 files changed

Lines changed: 272 additions & 287 deletions

Java/alg/lc/101.对称二叉树.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,23 @@ let isSymmertric = root => {
5555

5656
```go
5757
func isSymmetric(root *TreeNode) bool {
58-
if root == nil {
59-
return true
60-
}
61-
return isSymmetric1(root.Left, root.Right)
62-
}
63-
64-
func isSymmetric1(t1, t2 *TreeNode) bool {
65-
if t1 == nil && t2 == nil {
58+
var isSymmetric1 func (t1, t2 *TreeNode) bool
59+
isSymmetric1 = func (t1, t2 *TreeNode) bool {
60+
if t1 == nil && t2 == nil {
6661
return true
6762
}
68-
if t1 == nil || t2 == nil {
63+
if t1 == nil || t2 == nil {
6964
return false
7065
}
71-
if t1.Val != t2.Val {
66+
if t1.Val != t2.Val {
7267
return false
7368
}
74-
return isSymmetric1(t1.Left, t2.Right) && isSymmetric1(t1.Right, t2.Left)
69+
return isSymmetric1(t1.Left, t2.Right) && isSymmetric1(t1.Right, t2.Left)
70+
}
71+
if root == nil {
72+
return true
73+
}
74+
return isSymmetric1(root.Left, root.Right)
7575
}
7676
```
7777

Java/alg/lc/1071.字符串的最大公因子.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,18 @@ console.log(gcdOfStrings("LEET", "CODE"));
4848

4949
```go
5050
func gcdOfStrings(str1 string, str2 string) string {
51-
if str1+str2 != str2+str1 {
52-
return ""
53-
}
54-
return str2[0:gcd2(len(str1), len(str2))]
55-
}
56-
func gcd2(a int, b int) int {
57-
if b == 0 {
51+
var gcd func(a int, b int) int
52+
gcd = func (a int, b int) int {
53+
if b == 0 {
5854
return a
5955
} else {
60-
return gcd2(b, a % b)
56+
return gcd(b, a % b)
57+
}
58+
}
59+
if str1+str2 != str2+str1 {
60+
return ""
6161
}
62+
return str2[0:gcd(len(str1), len(str2))]
6263
}
6364
```
6465

Java/alg/lc/108.将有序数组转换为二叉搜索树.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,21 @@ let buildTree = (nums, l, r) => {
5050

5151
```go
5252
func sortedArrayToBST(nums []int) *TreeNode {
53-
if nums == nil || len(nums) == 0 {
53+
var buildTree func (nums[] int, l, r int) *TreeNode
54+
buildTree = func (nums[] int, l, r int) *TreeNode {
55+
if l > r {
5456
return nil
5557
}
56-
return buildTree(nums, 0, len(nums) - 1)
57-
}
58-
59-
func buildTree(nums[] int, l, r int) *TreeNode {
60-
if l > r {
58+
m := l + (r - l) / 2
59+
root := &TreeNode{Val: nums[m]}
60+
root.Left = buildTree(nums, l, m - 1)
61+
root.Right = buildTree(nums, m + 1, l)
62+
return root
63+
}
64+
if nums == nil || len(nums) == 0 {
6165
return nil
6266
}
63-
m := l + (r - l) / 2
64-
root := &TreeNode{Val: nums[m]}
65-
root.Left = buildTree(nums, l, m - 1)
66-
root.Right = buildTree(nums, m + 1, l)
67-
return root
67+
return buildTree(nums, 0, len(nums) - 1)
6868
}
6969
```
7070

Java/alg/lc/11.盛最多水的容器.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ let maxArea = height => {
4949
func maxArea(height []int) int {
5050
max := 0
5151
l, r := 0, len(height) - 1
52+
Max := func(a int, b int) int {
53+
if a < b {
54+
return b
55+
} else {
56+
return a
57+
}
58+
}
5259
for l < r {
5360
min := 0
5461
if height[l] < height[r] {
@@ -58,17 +65,10 @@ func maxArea(height []int) int {
5865
min = height[r]
5966
r--
6067
}
61-
max = max9(max, (r - l + 1) * min)
68+
max = Max(max, (r - l + 1) * min)
6269
}
6370
return max
6471
}
65-
func max9(a int, b int) int {
66-
if a < b {
67-
return b
68-
} else {
69-
return a
70-
}
71-
}
7272
```
7373

7474
### java

Java/alg/lc/110.平衡二叉树.md

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,29 @@ let depth = root => {
4848
### go
4949

5050
```go
51-
var res = true
5251
func isBalanced(root *TreeNode) bool {
53-
depth(root)
54-
return res
55-
}
56-
func depth(root *TreeNode) int {
57-
if root == nil {
52+
var res = true
53+
var depth func (root *TreeNode) int
54+
Max := func(a int, b int) int {
55+
if a < b {
56+
return b
57+
} else {
58+
return a
59+
}
60+
}
61+
depth = func (root *TreeNode) int {
62+
if root == nil {
5863
return 0
5964
}
60-
l := depth(root.Left)
61-
r := depth(root.Right)
62-
if Abs(l - r) > 1{
65+
l := depth(root.Left)
66+
r := depth(root.Right)
67+
if Abs(l - r) > 1{
6368
res = false
6469
}
65-
return 1 + Max(l, r)
70+
return 1 + Max(l, r)
71+
}
72+
depth(root)
73+
return res
6674
}
6775
```
6876

Java/alg/lc/111.二叉树的最小深度.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,18 @@ var minDepth = function(root) {
5656
```go
5757
// 递归
5858
func minDepth(root *TreeNode) int {
59+
Min := func(a int, b int) int {
60+
if a < b {
61+
return a
62+
} else {
63+
return b
64+
}
65+
}
5966
if root == nil {
6067
return 0
6168
}
6269
l := minDepth(root.Left)
63-
r := minDepth(root.Right)
64-
// 剪枝
70+
r := minDepth(root.Right)
6571
if l == 0 || r == 0 {
6672
return l + r + 1
6773
}

Java/alg/lc/125.验证回文串.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ console.log(isPalindrome("race a car"));
6464

6565
```go
6666
func isPalindrome1(s string) bool {
67+
isNormalChar := func (a uint8) bool {
68+
if a >= 48 && a <= 57 {
69+
return true
70+
} else if a >= 65 && a <= 97 {
71+
return true
72+
} else if a >= 90 && a <= 122{
73+
return true
74+
}
75+
return false
76+
}
6777
if s == "" {
6878
return true
6979
}
@@ -83,17 +93,6 @@ func isPalindrome1(s string) bool {
8393
}
8494
return true
8595
}
86-
87-
func isNormalChar(a uint8) bool {
88-
if a >= 48 && a <= 57 {
89-
return true
90-
} else if a >= 65 && a <= 97 {
91-
return true
92-
} else if a >= 90 && a <= 122{
93-
return true
94-
}
95-
return false
96-
}
9796
```
9897

9998
### java

Java/alg/lc/17.电话号码的字母组合.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,28 @@ let letterCombinations = digits => {
5555
### go
5656

5757
```go
58-
var KEYS = []string{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}
59-
var res1 []string
6058
func letterCombinations(digits string) []string {
59+
var KEYS = []string{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}
60+
var res []string
61+
var dfs func(digits string, sb string)
6162
if len(digits) == 0 {
62-
return res1
63+
return res
6364
}
64-
dfs(digits, "")
65-
return res1
66-
}
67-
func dfs(digits string, sb string) {
68-
if len(sb) == len(digits) {
69-
res1 = append(res1, sb)
70-
return
71-
}
72-
c := digits[len(sb)] - byte('0')
73-
cur := KEYS[c]
74-
for _, v := range cur {
75-
sb += string(v)
76-
dfs(digits, sb)
77-
sb = sb[0:len(sb) - 1]
65+
dfs = func (digits string, sb string) {
66+
if len(sb) == len(digits) {
67+
res = append(res, sb)
68+
return
69+
}
70+
c := digits[len(sb)] - byte('0')
71+
cur := KEYS[c]
72+
for _, v := range cur {
73+
sb += string(v)
74+
dfs(digits, sb)
75+
sb = sb[0:len(sb) - 1]
76+
}
7877
}
78+
dfs(digits, "")
79+
return res
7980
}
8081
```
8182

Java/alg/lc/20.有效的括号.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,12 @@ let isValid = s => {
6969
### go
7070

7171
```go
72-
func isValid(s string) bool {
72+
func isValid(s string, i int, j int) bool {
7373
//
7474
var stack []string
75+
isSym := func (c1, c2 string) bool {
76+
return (c1 == "(" && c2 == ")") || (c1 == "{" && c2 == "}") || (c1 == "[" && c2 == "]")
77+
}
7578
for _, c := range s {
7679
if stack == nil || len(stack) == 0 || !isSym(stack[len(stack)-1], string(c)) {
7780
stack = append(stack, string(c))
@@ -81,10 +84,6 @@ func isValid(s string) bool {
8184
}
8285
return len(stack) == 0
8386
}
84-
85-
func isSym(c1, c2 string) bool {
86-
return (c1 == "(" && c2 == ")") || (c1 == "{" && c2 == "}") || (c1 == "[" && c2 == "]")
87-
}
8887
```
8988

9089
### java

Java/alg/lc/206.反转链表.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,16 @@ let reverseList = head => {
5454
```go
5555
// 尾递归
5656
func reverseList1(head *ListNode) *ListNode {
57-
return reverse1(nil, head)
58-
}
59-
func reverse1(pre, cur *ListNode) *ListNode {
60-
if cur == nil {
57+
var reverse func (pre, cur *ListNode) *ListNode
58+
reverse = func (pre, cur *ListNode) *ListNode {
59+
if cur == nil {
6160
return pre
6261
}
63-
next := cur.Next
64-
cur.Next = pre
65-
return reverse1(cur, next)
62+
next := cur.Next
63+
cur.Next = pre
64+
return reverse(cur, next)
65+
}
66+
return reverse(nil, head)
6667
}
6768
// 迭代头插
6869
func reverseList(head *ListNode) *ListNode {

0 commit comments

Comments
 (0)