Leetcode-1012 Complement of Base 10 Integer(十进制整数的补码)

 1 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
 2 class Solution
 3 {
 4     public:
 5         int bitwiseComplement(int N)
 6         {
 7             if(N==0)
 8                 return 1;
 9             string s;
10             while(N)
11             {
12                 s+= N%2+‘0‘;
13                 N/=2;
14             }
15             reverse(s.begin(),s.end());
16
17             _for(i,0,s.size())
18             {
19                 if(s[i]==‘0‘)
20                 {
21                     s[i] = ‘1‘;
22                 }
23                 else
24                     s[i] = ‘0‘;
25             }
26
27             int rnt = 0;
28             for(int i = 0 ;i < s.size();i ++)
29             {
30                 rnt += (s[i]-‘0‘)*pow(2,s.size()-i-1);
31               //  cout << rnt << endl;
32             }
33             return rnt;
34         }
35 };

原文地址:https://www.cnblogs.com/Asurudo/p/10546531.html

时间: 2024-08-29 08:45:41

Leetcode-1012 Complement of Base 10 Integer(十进制整数的补码)的相关文章

#Leetcode# 1009. Complement of Base 10 Integer

https://leetcode.com/problems/complement-of-base-10-integer/ Every non-negative integer N has a binary representation.  For example, 5 can be represented as "101" in binary, 11 as "1011" in binary, and so on.  Note that except for N =

leetcode 1009. 十进制整数的反码(Complement of Base 10 Integer)

目录 题目描述: 示例 1: 示例 2: 示例 3: 解法: 题目描述: 每个非负整数 N 都有其二进制表示.例如, 5 可以被表示为二进制 "101",11 可以用二进制 "1011" 表示,依此类推.注意,除 N = 0 外,任何二进制表示中都不含前导零. 二进制的反码表示是将每个 1 改为 0 且每个 0 变为 1.例如,二进制数 "101" 的二进制反码为 "010". 给定十进制数 N,返回其二进制表示的反码所对应的

128th LeetCode Weekly Contest Complement of Base 10 Integer

Every non-negative integer N has a binary representation.  For example, 5 can be represented as "101" in binary, 11 as "1011" in binary, and so on.  Note that except for N = 0, there are no leading zeroes in any binary representation.

1012.十进制整数的补码

自己的方法不适用性太差了 因为要另外处理0: 所以不建议采用 import java.util.Scanner; class Solution { public int bitwiseComplement(int N) { String str = ""; int sum = 0 ; if(N == 0) { return 1; } while( N >0 ) { if(N % 2 == 0) { str = 1 + str; } else { str = 0 + str ; }

leetcode——Reverse Integer 反转整数数字(AC)

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 这个题比较简单,考虑特殊情况如12000,注意检查反转后数字是否会越界溢出.代码如下: class Solution { public: int reverse(int x) { bool minus = false; short int splitNum[10]; int i = 0, j = 0; unsign

python 小数相加报错 invalid literal for int() with base 10

for i in column1: x = int(i) s += xprint "sum:",s,"count:",len(column1)# round (s / len(column1), 3)print "avg",round (s / len(column1), 3) Traceback (most recent call last): File "C:/3/csv测试.py", line 26, in <mo

Python中ValueError: invalid literal for int() with base 10 的实用解决办法

今天在写爬虫程序的时候由于要翻页,做除法分页的时候出现了 1 2 totalCount = '100' totalPage = int(totalCount)/20 ValueError: invalid literal for int() with base 10的错误 网上同样的错误有人建议用round(float(“1.0″)),但是解决不了我这个问题,round(float(“1.0″))是用于解决浮点数转换为整形数的, 而我这个则是因为原字符串转换为整形后做除法,虽然一段时间内可能不报

convert from base 10 to base 2

COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION Hence, we convert from base 10 to base 2 by repeated divisions by 2. The remainders and the final quotient, 1, give us, in order of increas-ing significance, the binary di

celery中配置redis密码时的ValueError: invalid literal for int() with base 10: &#39;xxxx&#39;

原配置: celery_broker = 'redis://:xxxx#[email protected]:6379/0' # docker0 错误原因: 密码中不能有 # https://blog.csdn.net/liushaochan123/article/details/8885116 celery中配置redis密码时的ValueError: invalid literal for int() with base 10: 'xxxx' 原文地址:https://www.cnblogs.