1342. Number of Steps to Reduce a Number to Zero Easy
1/**
2 * [1342] Number of Steps to Reduce a Number to Zero
3 *
4 * Given an integer num, return the number of steps to reduce it to zero.
5 * In one step, if the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it.
6 *
7 * Example 1:
8 *
9 * Input: num = 14
10 * Output: 6
11 * Explanation:
12 * Step 1) 14 is even; divide by 2 and obtain 7.
13 * Step 2) 7 is odd; subtract 1 and obtain 6.
14 * Step 3) 6 is even; divide by 2 and obtain 3.
15 * Step 4) 3 is odd; subtract 1 and obtain 2.
16 * Step 5) 2 is even; divide by 2 and obtain 1.
17 * Step 6) 1 is odd; subtract 1 and obtain 0.
18 *
19 * Example 2:
20 *
21 * Input: num = 8
22 * Output: 4
23 * Explanation:
24 * Step 1) 8 is even; divide by 2 and obtain 4.
25 * Step 2) 4 is even; divide by 2 and obtain 2.
26 * Step 3) 2 is even; divide by 2 and obtain 1.
27 * Step 4) 1 is odd; subtract 1 and obtain 0.
28 *
29 * Example 3:
30 *
31 * Input: num = 123
32 * Output: 12
33 *
34 *
35 * Constraints:
36 *
37 * 0 <= num <= 10^6
38 *
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/
43// discuss: https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48 pub fn number_of_steps(num: i32) -> i32 {
49 0
50 }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn test_1342() {
61 }
62}
63
Back
© 2025 bowen.ge All Rights Reserved.