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:
- The given integer is guaranteed to fit within the range of a 32-bit signed integer.
- 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:
- The given integer is guaranteed to fit within the range of a 32-bit signed integer.
- 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;}
- LeetCode 476. Number Complement
- leetcode 476. Number Complement
- 【Leetcode】476. Number Complement
- 【Leetcode】476. Number Complement
- 476. Number Complement*
- LeetCode 476. Number Complement
- LeetCode 476. Number Complement
- LeetCode--476. Number Complement
- LeetCode 476. Number Complement
- 476. Number Complement
- 476. Number Complement [LeetCode]
- 476. Number Complement
- [LeetCode]476.Number Complement
- 476. Number Complement
- 【LeetCode】 476. Number Complement
- LeetCode 476. Number Complement
- 476. Number Complement
- 476. Number Complement (E)
- Leetcode 4 Median of Two Sorted Arrays(二分)
- [连载]嵌入式实时操作系统AIOS设计与实现
- 获取字符串高度或宽度
- u盘安装linux操作系统So Easy
- sqlserver2008r2查找非中文字母数字出现的第一个位置
- 476. Number Complement
- 【Ubuntu】自动加载第三方ko模块
- java基础知识总结
- Java反射的作用
- js--作用域链
- iOS 将字符串复制到粘贴板
- 配置Tomcat的数据源
- Base 7
- c++中的内存对齐