-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: 0 ms (100.00%), Space: 39.4 MB (34.97%) - LeetHub
- Loading branch information
1 parent
4c385eb
commit d32caef
Showing
1 changed file
with
25 additions
and
13 deletions.
There are no files selected for viewing
38 changes: 25 additions & 13 deletions
38
...-of-steps-to-reduce-a-number-to-zero/1342-number-of-steps-to-reduce-a-number-to-zero.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} |