File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
22
22
23
23
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
24
24
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
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 |
25
26
|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
26
27
|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
27
28
|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
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments