ReverseBits
eclipse没问题,leetcode 1不能通过,超出int最大值了,但是怎么转无符号?
/*Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).
For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.
*/
public static long reverseBits(int n) {
long sum=0;
List<Character> al=new ArrayList<Character>();
String str = Long.toBinaryString(n);
char[] ch = str.toCharArray();
for(int i=0;i<32-ch.length;i++)
al.add('0');
for (int i = 0; i < ch.length; i++)
al.add(ch[i]);
System.out.println(al);
for(int i=31;i>=0;i--) {
if(al.get(i)=='1')
sum+=Math.pow(2,i);}
return sum;
}
ReverseBits的更多相关文章
- [LeetCode] Reverse Bits 翻转位
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 全部leetcode题目解答(不含带锁)
(记忆线:当时一刷完是1-205. 二刷88道.下次更新记得标记不能bug-free的原因.) 88-------------Perfect Squares(完美平方数.给一个整数,求出用平方数来 ...
- Leetcode-190 Reverse Bits
#190. Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432615 ...
- Java位运算总结-leetcode题目
按位操作符只能用于整数基本数据类型中的单个bit中,操作符对应表格: Operator Description & 按位与(12345&1=1,可用于判断整数的奇偶性) | 按位或 ^ ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- leetcode刷题全纪录(持续更新)
2.Add Two Numbers 原题链接https://leetcode.com/problems/add-two-numbers/ AC解: public ListNode addTwoNumb ...
- leetcode 190
190. Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 4326159 ...
- 【leetcode】Reverse Bits(middle)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 【leetcode❤python】 190. Reverse Bits
#-*- coding: UTF-8 -*- class Solution: # @param n, an integer # @return an integer def reve ...
随机推荐
- jQuery 追加元素的方法如append、prepend、before
1.jQuery append() 方法 jQuery append() 方法在被选元素的结尾插入内容. 实例 复制代码代码如下: $("p").append("Some ...
- SQL2008安装自动退出
一同事安装sql 2008 然后删除过,后来再也装不上了,安装SQL2008操作执行到安装支持文件的时候就会出现安装过程自动退出的现象. 网上很多人说需要卸载,我看原因不一定是这个,因为根本没有安装成 ...
- 用c#读取文件内容中文是乱码的解决方法:
用c#读取文件内容中文是乱码的解决方法: //方法1: StreamReader din = new StreamReader(@"C:\1.txt", System.Text.E ...
- window7 下 安装 apache24(httpd-2.4.10-x86-r2)加 php5.6(php-5.6.4-Win32-VC11-x86)加yaf(php_yaf-2.3.3-5.6-ts-vc11-x86)整合
window7 下 安装 apache24(httpd-2.4.10-x86-r2)加 php5.6(php-5.6.4-Win32-VC11-x86)加yaf(php_yaf-2.3.3-5.6-t ...
- Jar mismatch! Fix your dependencies的问题
在开发Android项目的时候,有时需要引用多个项目作为library.在引用项目的时候,有时会出现“Jar mismatch! Fix your dependencies”错误. 这是因为两个项目的 ...
- [转]oracle for update和for update nowait的区别
1概念小结:(针对以下引用区域内容) 1.1 普通select语句不加锁. 1.2 for update和for update nowait都试图将符合条件的数据加上行级锁.用于排斥其他针对这个表的写 ...
- SQL SERVER 组内排序
取出每组的第一个 select *from (select * ,RANK ( ) OVER( PARTITION by org order by reportcode asc) PartionNum ...
- PHP析构函数与垃圾回收
析构函数:当某个对象成为垃圾或者当对象被显式销毁时执行. GC (Garbage Collector) 在PHP中,没有任何变量指向这个对象时,这个对象就成为垃圾.PHP会将其在内存中销毁.这是PHP ...
- android学习笔记29——Intent/IntentFilter
Intent/IntentFilter Intent封装android应用程序需要启动某个组件的“意图”,Intent还是应用程序组件之间通信的重要媒介. EG:Activity之间需要交换数据时,使 ...
- Eclipse:启动时提示"Failed to load the JNI shared library"的解决方案
今天打开Eclipse,弹出提示框"Failed to load the JNI shared library" 原因1:给定目录下jvm.dll不存在. 对策:(1)重新安装jr ...