476. Number Complement Easy

@problem@discussion
#Bit Manipulation



1/**
2 * [476] Number Complement
3 *
4 * The complement of an integer is the integer you get when you flip all the 0's to 1's and all the 1's to 0's in its binary representation.
5 * 
6 * 	For example, The integer 5 is "101" in binary and its complement is "010" which is the integer 2.
7 * 
8 * Given an integer num, return its complement.
9 *  
10 * Example 1:
11 * 
12 * Input: num = 5
13 * Output: 2
14 * Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
15 * 
16 * Example 2:
17 * 
18 * Input: num = 1
19 * Output: 0
20 * Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
21 * 
22 *  
23 * Constraints:
24 * 
25 * 	1 <= num < 2^31
26 * 
27 *  
28 * Note: This question is the same as 1009: <a href="https://leetcode.com/problems/complement-of-base-10-integer/" target="_blank">https://leetcode.com/problems/complement-of-base-10-integer/</a>
29 * 
30 */
31pub struct Solution {}
32
33// problem: https://leetcode.com/problems/number-complement/
34// discuss: https://leetcode.com/problems/number-complement/discuss/?currentPage=1&orderBy=most_votes&query=
35
36// submission codes start here
37
38impl Solution {
39    pub fn find_complement(num: i32) -> i32 {
40        0
41    }
42}
43
44// submission codes end
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn test_476() {
52    }
53}
54


Back
© 2025 bowen.ge All Rights Reserved.