Skip to content

Commit 4ebe08e

Browse files
authored
Create Minimum Bit Flips to Convert Number.java
1 parent 2b90fc1 commit 4ebe08e

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int minBitFlips(int start, int goal) {
3+
int numOfFlips = 0;
4+
while (start > 0 && goal > 0) {
5+
numOfFlips += start % 2 != goal % 2 ? 1 : 0;
6+
start /= 2;
7+
goal /= 2;
8+
}
9+
while (start > 0) {
10+
numOfFlips += start % 2;
11+
start /= 2;
12+
}
13+
while (goal > 0) {
14+
numOfFlips += goal % 2;
15+
goal /= 2;
16+
}
17+
return numOfFlips;
18+
}
19+
}

0 commit comments

Comments
 (0)