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

解析:这个题的意思就是求出给出数据num的反码的十进制数。
解题思路:num-->二进制num-->二进制num的反码-->二进制num反码的十进制表示。
首先把num转换为二进制字符串,然后遍历该字符串。按题目要求,应该从二进制最左边的1开始。如010就把左边的0去掉变为10。
我们可以设置一个flag,在遍历字符串时只要碰到1就把flag置为true,即开始取反码。
取反码的过程就比较简单了,只要1-->0 0-->1就可以了。
得到反码后再通过integer的内置方法转为十进制就可以了。
  
public class Solution {
public static int findComplement(int num) {
StringBuilder sbin=new StringBuilder(Integer.toBinaryString(num));
boolean flag = false;
for (int i = 0; i < sbin.length(); i++) {
if (sbin.charAt(i) == '1'){
flag = true;
}
if (flag) {
sbin.setCharAt(i, sbin.charAt(i) == '1' ? '0' : '1');
}
}
return Integer.valueOf(sbin.toString(), 2);
}
}

注意:更改字符串是不能用String类型的,因为String在实现的时候是被设计为final类型的。如果想改变应该用StringBuilder或者StringBuffer。

一个经典的面试题是问String、StringBuilder、StringBuffer的区别。很多人在准备面试的时候估计背过好几遍却不知道究竟有什么用处,毕竟平时写代码用String就够了呀。上述例子也许可以稍微打消一点这种疑问了,String的不可变的特性导致我们无法简单地实现需要的代码逻辑(当然,用String也是可以实现的)。此时StringBuilder和StringBuffer就派上用场了。

LeetCode(476): Number Complement的更多相关文章

  1. 【leetcode 476】. Number Complement

    给定一个正整数,输出其补码. 思路:利用mask掩码进行异或, 利用 temp >> 1 大于0 来决定mask长度,求出的mask 为二进制 1 0 0 0 0类型,           ...

  2. LeetCode算法题-Number Complement(Java实现-五种解法)

    这是悦乐书的第240次更新,第253篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第107题(顺位题号是476).给定正整数,输出其补码数.补充策略是翻转其二进制表示的位 ...

  3. LeetCode OJ:Number of Islands(孤岛计数)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  4. LeetCode OJ:Number of 1 Bits(比特1的位数)

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  5. LeetCode 191:number of one bits

    题目就是: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (als ...

  6. LeetCode 题解之Number Complement

    1.题目描述 2.题目分析 使用 C++的 bitset 库进行操作: 3.代码 int findComplement(int num) { bitset<> b(num); string ...

  7. LeetCode#476 Number Complement - in Swift

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

  8. 【leetcode】476. Number Complement

    problem 476. Number Complement solution1: class Solution { public: int findComplement(int num) { //正 ...

  9. LeetCode——Number Complement

    LeetCode--Number Complement Question Given a positive integer, output its complement number. The com ...

随机推荐

  1. git学习(5)分支管理(续)

    git学习(5)分支管理(续) 1.解决冲突 冲突的产生 如我们在新建分支和原来master分支上对同一文件做了修改并提交,在合并分支的时候就会遇到冲突 比如我新建了分支myBranch,在这个分支上 ...

  2. (转)android mock location

    android mock location 2014-01-26     我来说两句   来源:党玉涛   收藏 我要投稿 现在软件市场上有很多可以改变手机地理位置的软件,更改后打开微信就可以随意定位 ...

  3. Writing a Discard Server 写个抛弃服务器 世上最简单的协议

    Netty.docs: User guide for 4.x https://netty.io/wiki/user-guide-for-4.x.html The most simplistic pro ...

  4. 唯品会的Service Mesh三年进化史 2018 年 Service Mesh 元年,被誉为是下一代微服务架构

    2018 年 Service Mesh 元年,被誉为是下一代微服务架构 https://www.sohu.com/a/225324586_465914 唯品会的Service Mesh三年进化史 - ...

  5. angular(二)

    angularjs第二章 自定义指令 scope 控制器 AngularJS控制器控制AngularJS应用程序的数据,是常规的JavaScript对象. ng-controller指令就是用来定义应 ...

  6. VS调用python方法

    1.  安装python3.7 2.  Vs2010中配置python: 3.添加头文件:#include <Python.h> 4.问题:error LNK2001: 无法解析的外部符号 ...

  7. 前端开发 - JavaScript - 下

    12.数组 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  8. 原!linux脚本统计

    #! /bin/sh first=$ first2=$ input2=$ let second=`date -d "-1 days ago ${input2}" +%Y%m%d` ...

  9. CListCtrl控件使用方法总结

    今天第一次用CListCtrl控件,遇到不少问题,查了许多资料,现将用到的一些东西总结如下: 以下未经说明,listctrl默认view 风格为report 相关类及处理函数 MFC:CListCtr ...

  10. Python中字符串拼接的N种方法

    python拼接字符串一般有以下几种方法: ①直接通过(+)操作符拼接 s = 'Hello'+' '+'World'+'!'print(s) 输出结果:Hello World! 使用这种方式进行字符 ...