Skip to content

Commit 3a43d0b

Browse files
solves binary numbr with alternatng bits
1 parent d7da0ac commit 3a43d0b

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# LeetCode Algorithms
22

3-
![problems-solved](https://img.shields.io/badge/Problems%20Solved-165/2081-1f425f.svg)
4-
![problems-solved-java](https://img.shields.io/badge/Java-165/2081-1abc9c.svg)
5-
![problems-solved-python](https://img.shields.io/badge/Python-165/2081-1abc9c.svg)
3+
![problems-solved](https://img.shields.io/badge/Problems%20Solved-167/2081-1f425f.svg)
4+
![problems-solved-java](https://img.shields.io/badge/Java-167/2081-1abc9c.svg)
5+
![problems-solved-python](https://img.shields.io/badge/Python-167/2081-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
77
[![cp](https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg)](https://github.com/anishLearnsToCode/competitive-programming)
88

@@ -187,7 +187,7 @@
187187
| 686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match) | |
188188
| 687 | [Longest Univalue Path](https://leetcode.com/problems/longest-univalue-path) | |
189189
| 690 | [Employee Importance](https://leetcode.com/problems/employee-importance) | |
190-
| 693 | [Binary Number with Alternating Bits](https://leetcode.com/problems/binary-number-with-alternating-bits) | |
190+
| 693 | [Binary Number with Alternating Bits](https://leetcode.com/problems/binary-number-with-alternating-bits) | [![Java](assets/java.png)](src/BinaryNumberWithAlternatingBits.java) [![Python](assets/python.png)](python/binary_number_with_alternating_bits.py) |
191191
| 696 | [Count Binary Substrings](https://leetcode.com/problems/count-binary-substrings) | |
192192
| 697 | [Degree of an Array](https://leetcode.com/problems/degree-of-an-array) | |
193193
| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree) | |
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def hasAlternatingBits(self, n: int) -> bool:
3+
lastDigit = n % 2
4+
while n > 0:
5+
n //= 2
6+
current = n % 2
7+
if lastDigit == current: return False
8+
lastDigit = current
9+
return True
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class BinaryNumberWithAlternatingBits {
2+
public boolean hasAlternatingBits(int number) {
3+
int lastDigit = number % 2, current;
4+
while (number > 0) {
5+
number >>= 1;
6+
current = number % 2;
7+
if (lastDigit == current) return false;
8+
lastDigit = current;
9+
}
10+
return true;
11+
}
12+
}

0 commit comments

Comments
 (0)