Skip to content

Update _693.java #42

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
merged 2 commits into from
Mar 12, 2019
Merged
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
19 changes: 13 additions & 6 deletions src/main/java/com/fishercoder/solutions/_693.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,20 @@
*/

public class _693 {
public boolean hasAlternatingBits(int n) {
String binaryStr = Integer.toBinaryString(n);
for (int i = 1; i < binaryStr.length(); i++) {
if (binaryStr.charAt(i - 1) == binaryStr.charAt(i)) {
return false;
public static class Solution1 {
public boolean hasAlternatingBits(int n) {
String binaryStr = Integer.toBinaryString(n);
for (int i = 1; i < binaryStr.length(); i++) {
if (binaryStr.charAt(i - 1) == binaryStr.charAt(i)) {
return false;
}
}
return true;
}
}
public static class Solution2 {
public boolean hasAlternatingBits_oneline(int n) {
return Integer.bitCount(((n >> 1) ^ n) + 1) == 1;
}
return true;
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy