mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 16:36:41 +08:00
Merge pull request #117 from NovaHe/master
fix 167: clean up redundant code
This commit is contained in:
@ -6,13 +6,14 @@ func twoSum167(numbers []int, target int) []int {
|
|||||||
for i < j {
|
for i < j {
|
||||||
if numbers[i]+numbers[j] == target {
|
if numbers[i]+numbers[j] == target {
|
||||||
return []int{i + 1, j + 1}
|
return []int{i + 1, j + 1}
|
||||||
} else if numbers[i]+numbers[j] < target {
|
}
|
||||||
|
if numbers[i]+numbers[j] < target {
|
||||||
i++
|
i++
|
||||||
} else {
|
} else {
|
||||||
j--
|
j--
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return []int{-1, -1}
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 解法二 不管数组是否有序,空间复杂度比上一种解法要多 O(n)
|
// 解法二 不管数组是否有序,空间复杂度比上一种解法要多 O(n)
|
||||||
@ -20,8 +21,8 @@ func twoSum167_1(numbers []int, target int) []int {
|
|||||||
m := make(map[int]int)
|
m := make(map[int]int)
|
||||||
for i := 0; i < len(numbers); i++ {
|
for i := 0; i < len(numbers); i++ {
|
||||||
another := target - numbers[i]
|
another := target - numbers[i]
|
||||||
if _, ok := m[another]; ok {
|
if idx, ok := m[another]; ok {
|
||||||
return []int{m[another] + 1, i + 1}
|
return []int{idx + 1, i + 1}
|
||||||
}
|
}
|
||||||
m[numbers[i]] = i
|
m[numbers[i]] = i
|
||||||
}
|
}
|
||||||
|
@ -6,14 +6,13 @@ func majorityElement(nums []int) int {
|
|||||||
for i := 0; i < len(nums); i++ {
|
for i := 0; i < len(nums); i++ {
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
res, count = nums[i], 1
|
res, count = nums[i], 1
|
||||||
} else {
|
}
|
||||||
if nums[i] == res {
|
if nums[i] == res {
|
||||||
count++
|
count++
|
||||||
} else {
|
} else {
|
||||||
count--
|
count--
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user