476. Number Complement

来源:互联网 发布:中式现代服装 知乎 编辑:程序博客网 时间:2024/06/03 17:39


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: 5Output: 2Explanation: 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: 1Output: 0Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.


#include <iostream> #include <cmath>using namespace std;class Solution {public:    int findComplement(int num) {                int num2[32]={0};for(int i=-1,j=0;i!=0;j++)   {   i = num;   num2[j] = i%2;//   cout << num2[j];   num = num/2;int s = sizeof(num2)/sizeof(num2[0]);/*cout << s << endl;for(int i=0;i<s;i++)cout << num2[i];cout << endl;*/int result = 0;for(int i=s-1;i>=0;i--){if(num2[i-1] == 1){for(int j=i-1,k=0;k<i;j--,k++){///cout << j << " ";if(num2[j] == 1){num2[j] = 0;//cout << num2[j];//cout << " " <<k;}else if(num2[j] == 0){num2[j] = 1;//cout << num2[j];//cout << " " <<k;}//cout << endl;}for(int p=0;p<i-1;p++)result = result + num2[p]*pow(2,p);//cout << result << endl;break;}}         //       for(int i=0;i<s;i++)   //     cout << num2[i];               return result;    }};int main(){Solution r;// r.findComplement(5);cout << r.findComplement(5) << endl;cout << r.findComplement(1) << endl;cout << r.findComplement(16) << endl;}

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: 5Output: 2Explanation: 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: 1Output: 0Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.


#include <iostream> #include <cmath>using namespace std;class Solution {public:    int findComplement(int num) {                int num2[32]={0};for(int i=-1,j=0;i!=0;j++)   {   i = num;   num2[j] = i%2;//   cout << num2[j];   num = num/2;int s = sizeof(num2)/sizeof(num2[0]);/*cout << s << endl;for(int i=0;i<s;i++)cout << num2[i];cout << endl;*/int result = 0;for(int i=s-1;i>=0;i--){if(num2[i-1] == 1){for(int j=i-1,k=0;k<i;j--,k++){///cout << j << " ";if(num2[j] == 1){num2[j] = 0;//cout << num2[j];//cout << " " <<k;}else if(num2[j] == 0){num2[j] = 1;//cout << num2[j];//cout << " " <<k;}//cout << endl;}for(int p=0;p<i-1;p++)result = result + num2[p]*pow(2,p);//cout << result << endl;break;}}         //       for(int i=0;i<s;i++)   //     cout << num2[i];               return result;    }};int main(){Solution r;// r.findComplement(5);cout << r.findComplement(5) << endl;cout << r.findComplement(1) << endl;cout << r.findComplement(16) << endl;}

0 0
原创粉丝点击