Skip to content

Commit

Permalink
Time: 0 ms (100.00%), Space: 39.4 MB (34.97%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
tausiq2003 committed Sep 5, 2023
1 parent 4c385eb commit d32caef
Showing 1 changed file with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
class Solution {
public int numberOfSteps(int num) {
//if num is even divide by 2
//else subtract by 1
int count = 0;
while(num > 0){
if(num % 2 == 0){
num = num/2;
}
else{
num--;
}
count++;
}
// //if num is even divide by 2
// //else subtract by 1
// int count = 0;
// while(num > 0){
// if(num % 2 == 0){
// num = num/2;
// }
// else{
// num--;
// }
// count++;
// }

return count;
// return count;
//previous soln
// recursion soln
// at last 0 + 1 + 1 + 1 +... count
// base condition
if(num == 0){
return 0;
}
if (num % 2 == 0){
return 1 + numberOfSteps(num/2);

}
return 1 + numberOfSteps(num - 1);

}
}

0 comments on commit d32caef

Please sign in to comment.