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 113fec1

Browse files
Jian GUfishercoder1534
Jian GU
authored andcommitted
Update _693.java (#42)
* Update _693.java * Update _693.java
1 parent 919b7f6 commit 113fec1

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)

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