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. [Java][Tomcat]在eclipse中运行tomcat报的一个错误

    2008-11-9 16:27:59 org.apache.tomcat.util.digester.SetPropertiesRule begin警告: [SetPropertiesRule]{Se ...

  2. Jwt在javaweb项目中的应用核心步骤解读

    1.引入jwt依赖 <!--引入JWT依赖,由于是基于Java,所以需要的是java-jwt--> <dependency> <groupId>io.jsonweb ...

  3. 刚开始学Python,坚持下去

    嗯,刚开始学Python没几天,挺好的一门语言,十分简洁,也很好上手. 坚持下去,每天至少看一个小时的Python.

  4. linux, sysrq,acpi,apci,uio,subsystem daemon

    linux, sysrq,acpi,apci Linux设备模型  一.sysfs文件系统: sysfs文件系统是Linux2.6内核引入的,它被看成是与proc.devfs和devpty等同类别的文 ...

  5. Flask使用日志记录到文件示例

    https://www.polarxiong.com/archives/Flask%E4%BD%BF%E7%94%A8%E6%97%A5%E5%BF%97%E8%AE%B0%E5%BD%95%E5%8 ...

  6. artTemplate模板使用补充

    1. 添加辅助方法 ``template.helper(name, callback)``辅助方法一般用来进行字符串替换,如 UBB 替换.脏话替换等. 例如扩展一个UBB替换方法: template ...

  7. var 声明变量的变量提升问题(let/const)

    在ES6之前,JavaScript没有块级作用域(一对花括号{}即为一个块级作用域),只有全局作用域和函数作用域.变量提升即将变量声明提升到它所在作用域的最开始的部分.既全局变量. <scrip ...

  8. 10张Gif动图让你弄懂递归等概念

    图像(包括动图)是传递信息的一种高效方式,往往能增强表象.记忆与思维等方面的反应强度.所谓一图胜千言,说的就是这个道理. 今天为大家整理了十张动图GIFS,有助于认识循环.递归.二分检索等概念的具体运 ...

  9. 感知器python

    感知器学习的目标是求得一个能够将训练集正实例点和负实例点·完全正确分开的分离超平面.即找到这超平面的参数w,b. 超平面定义 w*x+b=0 其中w是参数,x是数据.公式很好理解以二维平面为例,w有两 ...

  10. RE合同记账会计凭证

    *&---------------------------------------------------------------------* *& Title : 不动产转租合同自 ...