Skip to content

Commit 372a76f

Browse files
Jian GUfishercoder1534
Jian GU
authored andcommitted
Update _693.java (fishercoder1534#42)
* Update _693.java * Update _693.java
1 parent 2a515c2 commit 372a76f

File tree

1 file changed

+13
-6
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+13
-6
lines changed

src/main/java/com/fishercoder/solutions/_693.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,20 @@
3131
*/
3232

3333
public class _693 {
34-
public boolean hasAlternatingBits(int n) {
35-
String binaryStr = Integer.toBinaryString(n);
36-
for (int i = 1; i < binaryStr.length(); i++) {
37-
if (binaryStr.charAt(i - 1) == binaryStr.charAt(i)) {
38-
return false;
34+
public static class Solution1 {
35+
public boolean hasAlternatingBits(int n) {
36+
String binaryStr = Integer.toBinaryString(n);
37+
for (int i = 1; i < binaryStr.length(); i++) {
38+
if (binaryStr.charAt(i - 1) == binaryStr.charAt(i)) {
39+
return false;
40+
}
3941
}
42+
return true;
43+
}
44+
}
45+
public static class Solution2 {
46+
public boolean hasAlternatingBits_oneline(int n) {
47+
return Integer.bitCount(((n >> 1) ^ n) + 1) == 1;
4048
}
41-
return true;
4249
}
4350
}

0 commit comments

Comments
 (0)