File tree Expand file tree Collapse file tree 1 file changed +13
-6
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +13
-6
lines changed Original file line number Diff line number Diff line change 3131 */
3232
3333public 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}
You can’t perform that action at this time.
0 commit comments