LeetCode(476): 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 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的更多相关文章
- 【leetcode 476】. Number Complement
给定一个正整数,输出其补码. 思路:利用mask掩码进行异或, 利用 temp >> 1 大于0 来决定mask长度,求出的mask 为二进制 1 0 0 0 0类型, ...
- LeetCode算法题-Number Complement(Java实现-五种解法)
这是悦乐书的第240次更新,第253篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第107题(顺位题号是476).给定正整数,输出其补码数.补充策略是翻转其二进制表示的位 ...
- 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 ...
- 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 ...
- LeetCode 191:number of one bits
题目就是: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (als ...
- LeetCode 题解之Number Complement
1.题目描述 2.题目分析 使用 C++的 bitset 库进行操作: 3.代码 int findComplement(int num) { bitset<> b(num); string ...
- LeetCode#476 Number Complement - in Swift
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- 【leetcode】476. Number Complement
problem 476. Number Complement solution1: class Solution { public: int findComplement(int num) { //正 ...
- LeetCode——Number Complement
LeetCode--Number Complement Question Given a positive integer, output its complement number. The com ...
随机推荐
- MyBatis基础入门
1.MyBatis概述 MyBatis是一个优秀的持久层框架,它对jdbc的操作数据库的过程进行封装,使开发者只需要关注 SQL 本身,而不需要花费精力去处理例如注册驱动.创建connection.创 ...
- js 跨域 之 修改服务器配置-XAMPP-Apache (nginx 拉到最后!)
js高程第21章提到了ajax 跨域技术,方法有很多,如图: 我主要讲这个: 其实代码就是这样就好了,当然只兼容 IE9 及之后的版本 ,IE9 之前的版本请去原书看吧,Page 600 var xh ...
- mysql 字符集研究
一.创建一个测试数据库 及一个测试用的表.均使用默认的编码方式. show variables like 'char%': mysql> show variables like 'char%'; ...
- Configuring Logging 配置日志
NGINX Docs | Configuring Logging https://docs.nginx.com/nginx/admin-guide/monitoring/logging/[ 在上层设置 ...
- Cookies, Security, and Privacy Client Identification
w HTTP The Definitive Guide Cookies themselves are not believed to be a tremendous security risk, be ...
- window下cmd的宽度调整
一直被cmd的宽度这么了好些年,刚才搜索了下,还真可以设置.标题栏右键属性,调整屏幕缓冲区宽度,只要足够长就不会换行了,然后调整窗口大小-宽度,不能超过屏幕缓冲区宽度,当小于屏幕缓冲区的时候,就会显示 ...
- CentOS7系统基本操作
查看网卡命令 [root@localhost ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state ...
- 【react表格组件】material-table 基本用法 & 组件override
教程: https://mbrn.github.io/material-table/#/ https://material-ui.com/api/table/ github: https://gith ...
- <Android 开源库> GreenDAO 使用方法具体解释<译文>
简单介绍 greenDAO是一个开源的Android ORM,使SQLite数据库的开发再次变得有趣. 它减轻了开发者处理底层的数据库需求,同一时候节省开发时间. SQLite是一个非常不错的关系型数 ...
- 【开发者笔记】按List中存放对象的某一字段计数的问题
如题,假设有如下表t_info: name date info a 20127-12-20 xxxx描述 b 20127-12-20 yyyyy描述 c 20127-12-21 zzz描述 d 201 ...