From 0a614a0e7971f3081f5b5f0de2119161993b5ac9 Mon Sep 17 00:00:00 2001 From: novahe Date: Tue, 27 Apr 2021 12:53:02 +0800 Subject: [PATCH 1/3] fix 167: clean up redundant code --- .../167. Two Sum II - Input array is sorted.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/leetcode/0167.Two-Sum-II---Input-array-is-sorted/167. Two Sum II - Input array is sorted.go b/leetcode/0167.Two-Sum-II---Input-array-is-sorted/167. Two Sum II - Input array is sorted.go index b73a32e1..65ec2d70 100644 --- a/leetcode/0167.Two-Sum-II---Input-array-is-sorted/167. Two Sum II - Input array is sorted.go +++ b/leetcode/0167.Two-Sum-II---Input-array-is-sorted/167. Two Sum II - Input array is sorted.go @@ -6,7 +6,8 @@ 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-- @@ -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 } From 263ca6e3b736f8d9347cb0be03036bdd94ea3132 Mon Sep 17 00:00:00 2001 From: novahe Date: Tue, 27 Apr 2021 12:57:45 +0800 Subject: [PATCH 2/3] fix 167: clean up redundant code --- .../167. Two Sum II - Input array is sorted.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/leetcode/0167.Two-Sum-II---Input-array-is-sorted/167. Two Sum II - Input array is sorted.go b/leetcode/0167.Two-Sum-II---Input-array-is-sorted/167. Two Sum II - Input array is sorted.go index 65ec2d70..e77da7c0 100644 --- a/leetcode/0167.Two-Sum-II---Input-array-is-sorted/167. Two Sum II - Input array is sorted.go +++ b/leetcode/0167.Two-Sum-II---Input-array-is-sorted/167. Two Sum II - Input array is sorted.go @@ -13,7 +13,7 @@ func twoSum167(numbers []int, target int) []int { j-- } } - return []int{-1, -1} + return nil } // 解法二 不管数组是否有序,空间复杂度比上一种解法要多 O(n) From 2d5005a24ac10d9c0f83af600861ecb3c824734f Mon Sep 17 00:00:00 2001 From: novahe Date: Tue, 27 Apr 2021 21:14:09 +0800 Subject: [PATCH 3/3] fix 169: clean up redundant code --- leetcode/0169.Majority-Element/169. Majority Element.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/leetcode/0169.Majority-Element/169. Majority Element.go b/leetcode/0169.Majority-Element/169. Majority Element.go index fed696f0..fcc9d507 100644 --- a/leetcode/0169.Majority-Element/169. Majority Element.go +++ b/leetcode/0169.Majority-Element/169. Majority Element.go @@ -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