문제해석
num이 주어졌을 때, 특정 작업을 반복해서 num이 0이하일 때 몇번의 반복인지 반환
각 작업은 num이 2로 나누어 떨어지는경우(짝수) 2로 나누거나, 그 외에 2로 나누어지지 않는 경우(홀수)는 1을 뺀다.
답 예
public int numberOfSteps(int num) {
int step = 0;
while (num > 0) {
if (num % 2 == 0) {
num = num /2;
} else {
num = num -1;
}
step++;
}
return step;
}
'Algorithm' 카테고리의 다른 글
[leetcode 75] 1480. Running Sum of 1d Array (0) | 2023.02.15 |
---|---|
[leetcode] 1672. Richest Customer Wealth (0) | 2023.02.15 |
[leetcode] 1337. The K Weakest Rows in a Matrix (0) | 2023.02.14 |
[leetcode] 876. Middle of the Linked List (0) | 2023.02.09 |
[leetcode] 412. Fizz Buzz (0) | 2023.02.09 |