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. JavaMath方法、服务器与Tomcat安装与配置步骤

    一.Math Math.PI 记录的圆周率  Math.E 记录e的常量  Math中还有一些类似的常量,都是一些工程数学常用量. Math.abs 求绝对值  Math.sin 正弦函数 Math. ...

  2. java nio探险

    区别于io: nio是基于通道和缓冲区的,io是基于字节流和字符流的,(千万别被这些破名词唬住).以读取文件为例,文件就是自来水厂,通道就是自来水管道,缓冲区就是你家的缸(或者盛水的xx容器,例如你的 ...

  3. R语言中将数据框(data.frame)中字符型数据转化为数值型

    as.data.frame(lapply(data,as.numeric))

  4. DHCP服务原理

    DHCP 工作原理 一.什么是DHCP? DHCP,动态主机配置协议,前身是BOOTP协议,是一个局域网的网络协议,使用UDP协议工作,常用的2个端口:67(DHCP server),68(DHCP ...

  5. Yii框架2.0的控制器

    控制器是继承[[yii\base\Controller]]类的对象,负责处理请求和生成响应. 具体来说,控制器从应用主体接管控制后会分析请求数据并传送到模型, 传送模型结果到视图,最后生成输出响应信息 ...

  6. 4.2 - MySQL

    一.表关系 请创建如下表,并创建相关约束 班级表:class 学生表:student cid caption grade_id sid sname gender class_id 1 一年一班 1 1 ...

  7. 剑指Offer——对称的二叉树

    题目描述: 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. 分析: 递归解法. 如果对称点一个有一边为空一边不为空,或者是对称点数值不一 ...

  8. 《Monitoring and Tuning the Linux Networking Stack: Receiving Data》翻译

    Overview 从宏观的角度来看,一个packet从网卡到socket接收缓冲区的路径如下所示: 驱动加载并初始化 packet到达网卡 packet通过DMA被拷贝到内核中的一个ring buff ...

  9. linux系统压缩\解压命令详解

    转自:http://www.cnblogs.com/qq78292959/archive/2011/07/06/2099427.html. tar -c: 建立压缩档案-x:解压-t:查看内容-r:向 ...

  10. NAND flash阵营ToggleDDR和ONFI

    NAND 闪存:目前闪存制造厂主要分为三星与东芝.海力士联合的ToggleDDR阵营和英特尔与美光为首的ONFI阵营 IM Flash Technologies(IMFT):由Intel和Micron ...