#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 = 0, there are no leading zeroes in any binary representation.

The complement of a binary representation is the number in binary you get when changing every 1 to a 0 and 0 to a 1.  For example, the complement of "101" in binary is "010" in binary.

For a given number N in base-10, return the complement of it‘s binary representation as a base-10 integer.

Example 1:

Input: 5
Output: 2
Explanation: 5 is "101" in binary, with complement "010" in binary, which is 2 in base-10.

Example 2:

Input: 7
Output: 0
Explanation: 7 is "111" in binary, with complement "000" in binary, which is 0 in base-10.

Example 3:

Input: 10
Output: 5
Explanation: 10 is "1010" in binary, with complement "0101" in binary, which is 5 in base-10.

代码:

class Solution {
public:
    int bitwiseComplement(int N) {
        if(N == 0) return 1;
        vector<int> ans;
        while(N) {
            ans.push_back(!(N % 2));
            N /= 2;
        }
        int sum = 0;
        for(int i = 0; i < ans.size() / 2; i ++)
            swap(ans[i], ans[ans.size() - i - 1]);
        for(int i = 0; i < ans.size(); i ++)
            sum += ans[i] * pow(2, ans.size() - 1 - i);

        return sum;
    }
    int pow(int a, int b) {
        int ans = 1;
        while(b) {
            if(b % 2) {
                ans *= a;
                b --;
            } else {
                a *= a;
                b /= 2;
            }
        }
        return ans;
    }
};

  周末开始啦

原文地址:https://www.cnblogs.com/zlrrrr/p/10699770.html

时间: 2024-10-08 16:02:12

#Leetcode# 1009. Complement of Base 10 Integer的相关文章

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.

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.si

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.

ValueError: invalid literal for int() with base 10: &#39;&#39;

有时候需要用int()函数转换字符串为整型,但是切记int()只能转化由纯数字组成的字符串,如下例: Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. str1='214567' st

Leetcode: 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