LeetCode解题思路:476. Number Complement

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

Note:

  1. The given integer is guaranteed to fit within the range of a 32-bit signed integer.
  2. You could assume no leading zero bit in the integer’s binary representation.

Example 1:

Input: 5
Output: 2
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.

Example 2:

Input: 1
Output: 0
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.

题意:给定一个整数,输出另一个整数。对这个数的要求是原数转换成二进制后,从高向低第一个1开始按位取反所得到的数。

基本思路:

1.最常规想法:定义1个长度为32的int(bool/char/type) 数组,把原数二进制展开,然后数0,并在数组中相应的位置上写1,到最后一个1为止。然后再把数组变成一个二进制数,然后就能得到想要的数。代码如下(找最后一个1的方式有点蠢,不仅适合正数也适合负数):

 1 int findComplement(int num) {
 2     int a[32] = {0};//定义数组
 3     int cnt = 0;
 4     while(num)
 5     {
 6         a[cnt++] = (num%2)^1;//保存元素
 7         num/=2;
 8     }
 9     while(a[cnt] == 0 && cnt > 0)//找数组最后的位置
10         cnt--;
11     cnt ++;
12     int i = 0;
13     int ret = 0;
14     while(i<=cnt)
15     {
16         ret += (1<<i)*a[i];//把数组转化成数
17         ++i;
18     }
19     return ret;
20 }

2. 用上面的方法是3ms并不慢。当然并不是没有简单的方法,这题看上去就像按位取反,然而按位取反并不太可行,因为5取反等于-6,仍然要去掉高位的1,才能得到想要的数。最后仍然要去统计高位有多少个1,保存在一个数组或数中再进行操作。

3. 不过保存在1个数中,从占用的空间上看多少能让我们觉得更加欣慰一点,多少有一点优化好过没有优化,既然都要把这个数转化成二进制,那我们可以顺手构造一个新的数,让他的每一位都为1,并且位数和原来的数相等,然后用这个数和原来的数直接异或并返回。这种查位的方法只适合正数(题中也说的是正数)

1 int findComplement(int num) {
2     int i = 0,ret = 0,tmp = num;
3     while((tmp/=2)!=0)
4        ret += 1<<i++;
5     ret+=1<<i;//因为/=2先进行后判断,所以要后加一次
6     return ret^num;//这里也可以改成ret-num,当然这并不是位操作实现减法的原理
7 }

当然结果仍然是3ms 代码看上去倒是简洁了不少。

更简洁的方法就是思路2,我自己想了半天觉得没有特别简单的方法就没写

以下来自LeetCode lzl124631x137大神,适合正数和负数

1 class Solution {
2 public:
3     int findComplement(int num) {
4         unsigned mask = ~0;//将掩码各位全设1
5         while (num & mask) mask <<= 1;//掩码左移确定num位数
6         return ~mask & ~num;//取反再取与
7     }
8 };

还有一个比较奇特的方法,由于给的是正整数所以这种方法是可行的的,如果包含负数这种将不可行

1 int findComplement(int num) {
2     int mask = num;
3     mask |= mask >> 1;
4     mask |= mask >> 2;
5     mask |= mask >> 4;
6     mask |= mask >> 8;
7     mask |= mask >> 16;
8     return num ^ mask;
9 }

有兴趣的可以去看下他的解释:https://discuss.leetcode.com/topic/74897/maybe-fewest-operations。侵删。

时间: 2024-10-24 20:42:52

LeetCode解题思路:476. Number Complement的相关文章

LeetCode#476 Number Complement - in Swift

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. 給一個正整數,算出他的二補數 (2's complement). 二補數就是將該數字的二進制碼全部翻轉過來. Note: The given integer is guaranteed to fit within the range of

LeetCode解题思路:595. Big Countries

There is a table World +-----------------+------------+------------+--------------+---------------+ | name | continent | area | population | gdp | +-----------------+------------+------------+--------------+---------------+ | Afghanistan | Asia | 652

LeetCode 476. Number Complement(easy难度c++)

题目: Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the range of a 32-bit signed integer. You could assume no leadin

LeetCode 476. Number Complement (数的补数)

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the range of a 32-bit signed integer. You could assume no leading ze

476. Number Complement【位运算】

2017/3/14 15:36:44 Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the range of a 32-bit signed integer. You could a

LeetCode 解题思路:535.Encode and Decode TinyURL

TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk. Design the encode and decode methods for the TinyURL service. There is no res

LeetCode解题思路:575. Distribute Candies

Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister.

LeetCode解题思路:627. Swap Salary

Given a table salary, such as the one below, that has m=male and f=female values. Swap all f and m values (i.e., change all f values to m and vice versa) with a single update query and no intermediate temp table. For example: | id | name | sex | sala

476. Number Complement

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the range of a 32-bit signed integer. You could assume no leading ze