Merge pull request #60 from halfrost/code_quality_improvement

optimization code quality level from A to A+
This commit is contained in:
halfrost
2020-08-27 01:07:57 +08:00
committed by YDZ
543 changed files with 2449 additions and 2396 deletions

View File

@@ -71,9 +71,9 @@ func removeDuplicates80(nums []int) int {
if len(nums) == 0 {
return 0
}
last, finder, startFinder := 0, 0, -1
last, finder := 0, 0
for last < len(nums)-1 {
startFinder = -1
startFinder := -1
for nums[finder] == nums[last] {
if startFinder == -1 || startFinder > finder {
startFinder = finder
@@ -101,4 +101,5 @@ func removeDuplicates80(nums []int) int {
return last + 1
}
```

View File

@@ -62,10 +62,9 @@ func generateBSTree(start, end int) []*TreeNode {
tree = append(tree, nil)
return tree
}
left, right := []*TreeNode{}, []*TreeNode{}
for i := start; i <= end; i++ {
left = generateBSTree(start, i-1)
right = generateBSTree(i+1, end)
left := generateBSTree(start, i-1)
right := generateBSTree(i+1, end)
for _, l := range left {
for _, r := range right {
root := &TreeNode{Val: i, Left: l, Right: r}

View File

@@ -60,11 +60,9 @@ func insertionSortList(head *ListNode) *ListNode {
return head
}
newHead := &ListNode{Val: 0, Next: nil} // 这里初始化不要直接指向 head,为了下面循环可以统一处理
cur := head
pre := newHead
next := &ListNode{Val: 0, Next: nil}
cur, pre := head, newHead
for cur != nil {
next = cur.Next
next := cur.Next
for pre.Next != nil && pre.Next.Val < cur.Val {
pre = pre.Next
}

View File

@@ -97,7 +97,7 @@ func calculate1(s string) int {
break
}
}
tmp = string(stack[index+1 : len(stack)])
tmp = string(stack[index+1:])
stack = stack[:index]
res := strconv.Itoa(calculateStr(tmp))
for j := 0; j < len(res); j++ {
@@ -142,7 +142,6 @@ func calculateStr(str string) int {
if tmpStr != "" {
num, _ := strconv.Atoi(tmpStr)
nums = append(nums, num)
tmpStr = ""
}
res = nums[0]
for i := 0; i < len(s); i++ {
@@ -156,4 +155,11 @@ func calculateStr(str string) int {
return res
}
func isDigital(v byte) bool {
if v >= '0' && v <= '9' {
return true
}
return false
}
```

View File

@@ -46,9 +46,9 @@ package leetcode
// 解法一 O(n^2)
func isSubsequence(s string, t string) bool {
index, flag := 0, false
index := 0
for i := 0; i < len(s); i++ {
flag = false
flag := false
for ; index < len(t); index++ {
if s[i] == t[index] {
flag = true
@@ -76,4 +76,5 @@ func isSubsequence1(s string, t string) bool {
return len(s) == 0
}
```

View File

@@ -62,8 +62,8 @@ The substring "BBBB" has the longest repeating letters, which is 4.
package leetcode
func characterReplacement(s string, k int) int {
res, left, right, counter, freq := 0, 0, 1, 0, make([]int, 26)
for right = 0; right < len(s); right++ {
res, left, counter, freq := 0, 0, 0, make([]int, 26)
for right := 0; right < len(s); right++ {
freq[s[right]-'A']++
counter = max(counter, freq[s[right]-'A'])
for right-left+1-counter > k {
@@ -75,4 +75,11 @@ func characterReplacement(s string, k int) int {
return res
}
func max(a int, b int) int {
if a > b {
return a
}
return b
}
```

View File

@@ -57,12 +57,12 @@ func nextGreaterElement(nums1 []int, nums2 []int) []int {
if len(nums1) == 0 || len(nums2) == 0 {
return []int{}
}
res, reocrd, flag := []int{}, map[int]int{}, false
res, reocrd := []int{}, map[int]int{}
for i, v := range nums2 {
reocrd[v] = i
}
for i := 0; i < len(nums1); i++ {
flag = false
flag := false
for j := reocrd[nums1[i]]; j < len(nums2); j++ {
if nums2[j] > nums1[i] {
res = append(res, nums2[j])

View File

@@ -87,9 +87,8 @@ func splitListToParts(root *ListNode, k int) []*ListNode {
length := getLength(root)
splitNum := length / k
lengNum := length % k
cur := root
head := root
pre := root
cur, head := root, root
var pre *ListNode
fmt.Printf("总长度 %v, 分 %v 组, 前面 %v 组长度为 %v, 剩余 %v 组,每组 %v\n", length, k, lengNum, splitNum+1, k-lengNum, splitNum)
if splitNum == 0 {
for i := 0; i < k; i++ {
@@ -130,4 +129,14 @@ func splitListToParts(root *ListNode, k int) []*ListNode {
return res
}
func getLength(l *ListNode) int {
count := 0
cur := l
for cur != nil {
count++
cur = cur.Next
}
return count
}
```

View File

@@ -55,10 +55,9 @@ func uniqueLetterString1(S string) int {
if len(S) == 0 {
return 0
}
letterMap := map[byte]int{}
res, mod := 0, 1000000007
for i := 0; i < len(S); i++ {
letterMap = map[byte]int{}
letterMap := map[byte]int{}
for j := i; j < len(S); j++ {
letterMap[S[j]]++
tmp := 0
@@ -77,4 +76,5 @@ func uniqueLetterString1(S string) int {
return res % mod
}
```

View File

@@ -97,12 +97,12 @@ func recBST(root, tail *TreeNode) *TreeNode {
// 解法二 模拟
func increasingBST1(root *TreeNode) *TreeNode {
list, newRoot := []int{}, &TreeNode{}
list := []int{}
inorder(root, &list)
if len(list) == 0 {
return root
}
newRoot = &TreeNode{Val: list[0], Left: nil, Right: nil}
newRoot := &TreeNode{Val: list[0], Left: nil, Right: nil}
cur := newRoot
for index := 1; index < len(list); index++ {
tmp := &TreeNode{Val: list[index], Left: nil, Right: nil}
@@ -112,4 +112,12 @@ func increasingBST1(root *TreeNode) *TreeNode {
return newRoot
}
func inorder(root *TreeNode, output *[]int) {
if root != nil {
inorder(root.Left, output)
*output = append(*output, root.Val)
inorder(root.Right, output)
}
}
```

View File

@@ -69,6 +69,13 @@ Return the number of 4-directional walks from the starting square to the ending
package leetcode
var dir = [][]int{
{-1, 0},
{0, 1},
{1, 0},
{0, -1},
}
func uniquePathsIII(grid [][]int) int {
visited := make([][]bool, len(grid))
for i := 0; i < len(visited); i++ {
@@ -115,7 +122,7 @@ func findUniquePathIII(board [][]int, visited [][]bool, path []int, empty, start
}
}
visited[startx][starty] = false
empty++
//empty++ 这里虽然可以还原这个变量值,但是赋值没有意义,干脆不写了
path = path[:len(path)-2]
}
return