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.

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.

Note:

  1. 0 <= N < 10^9

题意啥的去看看中文版的就好了。简单一点看看example

大佬的代码很短的

class Solution {
public:
    int bitwiseComplement(int N) {
        int num[33] = {0};
        int cnt = 0;
        int sum = 0;
        int flag = 1;
        if(N == 0){
            return 1;
        }
        while(N){
            int pos = N % 2;
            num[cnt++] = pos == 1 ? 0 : 1;
            N/=2;
        }
        for(int i = 0 ; i < cnt ; i++){
            //cout<<num[i]<<endl;
            if(num[i] == 0){
                sum += 0;
            }else{
                sum += flag;
            }
            flag *= 2;
        }
        //cout<<endl;
        return sum;
    }
};

原文地址:https://www.cnblogs.com/yinghualuowu/p/10549653.html

时间: 2024-08-28 03:08:08

128th LeetCode Weekly Contest 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,返回其二进制表示的反码所对应的

123th LeetCode Weekly Contest Add to Array-Form of Integer

For a non-negative integer X, the array-form of X is an array of its digits in left to right order.  For example, if X = 1231, then the array form is [1,2,3,1]. Given the array-form A of a non-negative integer X, return the array-form of the integer 

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

Leetcode Weekly Contest 86

Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个由整数组成的 N × N 矩阵,其中有多少个 3 × 3 的 "幻方" 子矩阵?(每个子矩阵都是连续的). 直接模拟即可,本来是签到题,由于粗心,浪费了时间. 1 class Solution { 2 public: 3 int numMagicSquaresInside(vector&l

Leetcode Weekly Contest 152

退役老人现在连leetcode都不会做了 = = 今天早上做了leetcode第三题题目看错了,加上比赛中间还在调投稿的实验,一心二用直接gg 总结下教训就是 本渣现在做题连题目都看不清就开始做.开始写题之前应当把样例过一遍,然后自己再造1-2个例子,然后再开始做 A题:统计素数的个数(素数筛或者sqrt(n)判断都可以),然后分别计算count! class Solution { public: int numPrimeArrangements(int n) { vector<int> ha

[Leetcode Weekly Contest]174

链接:LeetCode [Leetcode]5328. 方阵中战斗力最弱的 K 行 给你一个大小为?m?* n?的方阵?mat,方阵由若干军人和平民组成,分别用 0 和 1 表示. 请你返回方阵中战斗力最弱的?k?行的索引,按从最弱到最强排序. 如果第?i?行的军人数量少于第?j?行,或者两行军人数量相同但 i 小于 j,那么我们认为第 i 行的战斗力比第 j 行弱. 军人 总是 排在一行中的靠前位置,也就是说 1 总是出现在 0 之前. 输入:\(mat = [[1,1,0,0,0], [1,

108th LeetCode Weekly Contest Minimum Falling Path Sum

Given a square array of integers A, we want the minimum sum of a falling path through A. A falling path starts at any element in the first row, and chooses one element from each row.  The next row's choice must be in a column that is different from t

113th LeetCode Weekly Contest Reveal Cards In Increasing Order

In a deck of cards, every card has a unique integer.  You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck. Now, you do the following steps repeatedly, until all cards are revealed: Take the