Skip to content

Commit 11953d0

Browse files
committedOct 8, 2017
[N-0] add 693
1 parent 6f30d04 commit 11953d0

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
25+
|693|[Binary Number with Alternating Bits](https://leetcode.com/problems/binary-number-with-alternating-bits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_693.java) | O(n) | O(1) | Easy |
2526
|690|[Employee Importance](https://leetcode.com/problems/employee-importance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_690.java) | O(n) | O(h) | Easy | DFS
2627
|688|[Knight Probability in Chessboard](https://leetcode.com/problems/knight-probability-in-chessboard/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_688.java) | O(n^2) | O(n^2) | Medium | DP
2728
|687|[Longest Univalue Path](https://leetcode.com/problems/longest-univalue-path/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_687.java) | O(n) | O(h) | Easy | DFS
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 693. Binary Number with Alternating Bits
5+
*
6+
* Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.
7+
8+
Example 1:
9+
Input: 5
10+
Output: True
11+
Explanation:
12+
The binary representation of 5 is: 101
13+
14+
Example 2:
15+
Input: 7
16+
Output: False
17+
Explanation:
18+
The binary representation of 7 is: 111.
19+
20+
Example 3:
21+
Input: 11
22+
Output: False
23+
Explanation:
24+
The binary representation of 11 is: 1011.
25+
26+
Example 4:
27+
Input: 10
28+
Output: True
29+
Explanation:
30+
The binary representation of 10 is: 1010.
31+
*/
32+
33+
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;
39+
}
40+
}
41+
return true;
42+
}
43+
}

0 commit comments

Comments
 (0)
Please sign in to comment.