Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3871fca

Browse files
Merge pull request youngyangyang04#2289 from aFlyBird0/batch-1
改写两处Go代码,使其更符合Go习惯
2 parents 48692ef + 546d3b6 commit 3871fca

File tree

2 files changed

+42
-43
lines changed

2 files changed

+42
-43
lines changed

‎problems/0213.打家劫舍II.md‎

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -225,32 +225,38 @@ class Solution:
225225
// 打家劫舍II 动态规划
226226
// 时间复杂度O(n) 空间复杂度O(n)
227227
func rob(nums []int) int {
228-
if len(nums) == 1 {
229-
return nums[0]
228+
// 如果长度为0或1,那么有没有环的限制都一样
229+
if len(nums) <= 1 {
230+
return robWithoutCircle(nums)
230231
}
231-
if len(nums) == 2 {
232-
return max(nums[0], nums[1])
233-
}
234-
235-
result1 := robRange(nums, 0)
236-
result2 := robRange(nums, 1)
237-
return max(result1, result2)
232+
233+
// 否则,去头或去尾,取最大
234+
res1 := robWithoutCircle(nums[:len(nums)-1])
235+
res2 := robWithoutCircle(nums[1:])
236+
237+
return max(res1, res2)
238238
}
239239

240-
// 偷盗指定的范围
241-
func robRange(nums []int, start int) int {
240+
// 原始的打家劫舍版
241+
func robWithoutCircle(nums []int) int {
242+
switch len(nums) {
243+
case 0: return 0
244+
case 1: return nums[0]
245+
}
242246
dp := make([]int, len(nums))
243-
dp[1] = nums[start]
244-
245-
for i := 2; i < len(nums); i++ {
246-
dp[i] = max(dp[i - 2] + nums[i - 1 + start], dp[i - 1])
247+
dp[0]=nums[0]
248+
dp[1] = max(nums[0], nums[1])
249+
250+
for i:=2; i<len(nums); i++ {
251+
dp[i] = max(dp[i-1], dp[i-2]+nums[i])
247252
}
248-
249-
return dp[len(nums) - 1]
253+
254+
return dp[len(nums)-1]
255+
250256
}
251257

252-
func max(a, b int) int {
253-
if a > b {
258+
func max(a, b int) int {
259+
if a>b {
254260
return a
255261
}
256262
return b

‎problems/1356.根据数字二进制下1的数目排序.md‎

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -171,36 +171,29 @@ class Solution:
171171

172172
```go
173173
func sortByBits(arr []int) []int {
174-
var tmp int
175-
for i := 0; i < len(arr); i++ {
176-
for j := i+1; j < len(arr); j++ {
177-
// 冒泡排序的手法,但是排序的规则从比大小变成了比位运算1的个数
178-
if isCmp(arr[i], arr[j]) {
179-
tmp = arr[i]
180-
arr[i] = arr[j]
181-
arr[j] = tmp
182-
}
183-
}
174+
// 是否arr[i]<=arr[j]
175+
// 先比较1的数量,后比较值本身
176+
cmp := func(i, j int) bool {
177+
c1, c2 := bitCount(arr[i]), bitCount(arr[j])
178+
if c1 == c2 {
179+
return arr[i] <= arr[j]
180+
}
181+
return c1 <= c2
184182
}
185-
return arr
186-
}
187183

188-
func isCmp(a, b int) bool {
189-
bitA := bitCount(a)
190-
bitB := bitCount(b)
191-
if bitA == bitB {
192-
return a > b
193-
} else {
194-
return bitA > bitB
195-
}
184+
// 调用库函数
185+
// 第一个参数是待排序切片,第二个是第i位是否小于第j位的函数
186+
sort.Slice(arr, cmp)
187+
188+
return arr
196189
}
197190

198-
func bitCount(n int) int {
199-
count := 0
200-
for n != 0 {
201-
n &= (n-1) // 清除最低位的1
191+
func bitCount(num int) (count int) {
192+
for num != 0 {
193+
num &= num-1 // 每次运算将最右侧的1变成0
202194
count++
203195
}
196+
204197
return count
205198
}
206199
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /