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

[pull] master from youngyangyang04:master #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 3 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions problems/0213.打家劫舍II.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -225,32 +225,38 @@ class Solution:
// 打家劫舍II 动态规划
// 时间复杂度O(n) 空间复杂度O(n)
func rob(nums []int) int {
if len(nums) == 1 {
return nums[0]
// 如果长度为0或1,那么有没有环的限制都一样
if len(nums) <= 1 {
return robWithoutCircle(nums)
}
if len(nums) == 2 {
return max(nums[0], nums[1])
}

result1 := robRange(nums, 0)
result2 := robRange(nums, 1)
return max(result1, result2)

// 否则,去头或去尾,取最大
res1 := robWithoutCircle(nums[:len(nums)-1])
res2 := robWithoutCircle(nums[1:])

return max(res1, res2)
}

// 偷盗指定的范围
func robRange(nums []int, start int) int {
// 原始的打家劫舍版
func robWithoutCircle(nums []int) int {
switch len(nums) {
case 0: return 0
case 1: return nums[0]
}
dp := make([]int, len(nums))
dp[1] = nums[start]

for i := 2; i < len(nums); i++ {
dp[i] = max(dp[i - 2] + nums[i - 1 + start], dp[i - 1])
dp[0]=nums[0]
dp[1] = max(nums[0], nums[1])

for i:=2; i<len(nums); i++ {
dp[i] = max(dp[i-1], dp[i-2]+nums[i])
}

return dp[len(nums) - 1]

return dp[len(nums)-1]

}

func max(a, b int) int {
if a > b {
func max(a, b int) int {
if a>b {
return a
}
return b
Expand Down
41 changes: 17 additions & 24 deletions problems/1356.根据数字二进制下1的数目排序.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -171,36 +171,29 @@ class Solution:

```go
func sortByBits(arr []int) []int {
var tmp int
for i := 0; i < len(arr); i++ {
for j := i+1; j < len(arr); j++ {
// 冒泡排序的手法,但是排序的规则从比大小变成了比位运算1的个数
if isCmp(arr[i], arr[j]) {
tmp = arr[i]
arr[i] = arr[j]
arr[j] = tmp
}
}
// 是否arr[i]<=arr[j]
// 先比较1的数量,后比较值本身
cmp := func(i, j int) bool {
c1, c2 := bitCount(arr[i]), bitCount(arr[j])
if c1 == c2 {
return arr[i] <= arr[j]
}
return c1 <= c2
}
return arr
}

func isCmp(a, b int) bool {
bitA := bitCount(a)
bitB := bitCount(b)
if bitA == bitB {
return a > b
} else {
return bitA > bitB
}
// 调用库函数
// 第一个参数是待排序切片,第二个是第i位是否小于第j位的函数
sort.Slice(arr, cmp)

return arr
}

func bitCount(n int) int {
count := 0
for n != 0 {
n &= (n-1) // 清除最低位的1
func bitCount(num int) (count int) {
for num != 0 {
num &= num-1 // 每次运算将最右侧的1变成0
count++
}

return count
}
```
Expand Down

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