Merge pull request #117 from NovaHe/master

fix 167: clean up redundant code
This commit is contained in:
halfrost
2021-04-28 09:12:20 +08:00
committed by GitHub
2 changed files with 9 additions and 9 deletions

View File

@ -6,13 +6,14 @@ func twoSum167(numbers []int, target int) []int {
for i < j {
if numbers[i]+numbers[j] == target {
return []int{i + 1, j + 1}
} else if numbers[i]+numbers[j] < target {
}
if numbers[i]+numbers[j] < target {
i++
} else {
j--
}
}
return []int{-1, -1}
return nil
}
// 解法二 不管数组是否有序,空间复杂度比上一种解法要多 O(n)
@ -20,8 +21,8 @@ func twoSum167_1(numbers []int, target int) []int {
m := make(map[int]int)
for i := 0; i < len(numbers); i++ {
another := target - numbers[i]
if _, ok := m[another]; ok {
return []int{m[another] + 1, i + 1}
if idx, ok := m[another]; ok {
return []int{idx + 1, i + 1}
}
m[numbers[i]] = i
}

View File

@ -6,12 +6,11 @@ func majorityElement(nums []int) int {
for i := 0; i < len(nums); i++ {
if count == 0 {
res, count = nums[i], 1
}
if nums[i] == res {
count++
} else {
if nums[i] == res {
count++
} else {
count--
}
count--
}
}
return res