2220. Minimum Bit Flips to Convert Number Easy

@problem@discussion
#Bit Manipulation



1/**
2 * [2220] Minimum Bit Flips to Convert Number
3 *
4 * A bit flip of a number x is choosing a bit in the binary representation of x and flipping it from either 0 to 1 or 1 to 0.
5 * 
6 * 	For example, for x = 7, the binary representation is 111 and we may choose any bit (including any leading zeros not shown) and flip it. We can flip the first bit from the right to get 110, flip the second bit from the right to get 101, flip the fifth bit from the right (a leading zero) to get 10111, etc.
7 * 
8 * Given two integers start and goal, return the minimum number of bit flips to convert start to goal.
9 *  
10 * Example 1:
11 * 
12 * Input: start = 10, goal = 7
13 * Output: 3
14 * Explanation: The binary representation of 10 and 7 are 1010 and 0111 respectively. We can convert 10 to 7 in 3 steps:
15 * - Flip the first bit from the right: 101<u>0</u> -> 101<u>1</u>.
16 * - Flip the third bit from the right: 1<u>0</u>11 -> 1<u>1</u>11.
17 * - Flip the fourth bit from the right: <u>1</u>111 -> <u>0</u>111.
18 * It can be shown we cannot convert 10 to 7 in less than 3 steps. Hence, we return 3.
19 * Example 2:
20 * 
21 * Input: start = 3, goal = 4
22 * Output: 3
23 * Explanation: The binary representation of 3 and 4 are 011 and 100 respectively. We can convert 3 to 4 in 3 steps:
24 * - Flip the first bit from the right: 01<u>1</u> -> 01<u>0</u>.
25 * - Flip the second bit from the right: 0<u>1</u>0 -> 0<u>0</u>0.
26 * - Flip the third bit from the right: <u>0</u>00 -> <u>1</u>00.
27 * It can be shown we cannot convert 3 to 4 in less than 3 steps. Hence, we return 3.
28 * 
29 *  
30 * Constraints:
31 * 
32 * 	0 <= start, goal <= 10^9
33 * 
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/minimum-bit-flips-to-convert-number/
38// discuss: https://leetcode.com/problems/minimum-bit-flips-to-convert-number/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43    pub fn min_bit_flips(start: i32, goal: i32) -> i32 {
44        0
45    }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_2220() {
56    }
57}
58


Back
© 2025 bowen.ge All Rights Reserved.