规范格式

This commit is contained in:
YDZ
2020-08-07 15:50:06 +08:00
parent 854a339abc
commit 4e11f4028a
1438 changed files with 907 additions and 924 deletions

View File

@ -0,0 +1,29 @@
package leetcode
// 解法一
func hasAlternatingBits(n int) bool {
/*
n = 1 0 1 0 1 0 1 0
n >> 1 0 1 0 1 0 1 0 1
n ^ n>>1 1 1 1 1 1 1 1 1
n 1 1 1 1 1 1 1 1
n + 1 1 0 0 0 0 0 0 0 0
n & (n+1) 0 0 0 0 0 0 0 0
*/
n = n ^ (n >> 1)
return (n & (n + 1)) == 0
}
// 解法二
func hasAlternatingBits1(n int) bool {
last, current := 0, 0
for n > 0 {
last = n & 1
n = n / 2
current = n & 1
if last == current {
return false
}
}
return true
}