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 ...
随机推荐
- python str()与repr()
相同点: 将任意值转为字符串 不同点: str()致力于生成一个对象的可读性好的字符串表示,它的返回结果通常无法用于eval()求值,但很适合用于print语句输出 repr()出来的值是给pytho ...
- no ocijdbc11 in java.library.path linux
no ocijdbc11 in java.library.path linux vi /etc/profile export ORACLE_HOME=/oracle/database/oracle/p ...
- 【性能诊断】四、单功能场景的性能分析(RedGate,找到同一个客户端的并发请求被串行化问题)
问题描述: 客户端js连续发起两个异步http请求,请求地址相同,但参数不同:POST http://*.*.*.*/*****/webservice/RESTFulWebService/RESTFu ...
- C#操作xml SelectNodes,SelectSingleNode总是返回NULL 与 xPath 介绍
一. SelectNodes,SelectSingleNode总是返回NULL 下面以一个简单的xml为例: <?xml version="1.0"?> <mes ...
- .NET中的Timer类型用法详解
这篇文章主要介绍了.NET中的Timer类型用法,较为详细的分析了Timer类型在各种环境下的用法,需要的朋友可以参考下 在.NET FrameWork中有多个Timer,那么怎么根据实际情况进行 ...
- c#语音报时(含完整的声音资源文件).rar
private void btnBaoshi_Click(object sender, EventArgs e) { try { System.Threading.Thread thread = ne ...
- taglib例子
jsp中的taglib有点类似asp.net中的customer control.自定义标签. 一个最简单的taglib使用例子:检查用户是否已经被登陆. 新建一个class: CheckLoginT ...
- 安卓 NEXUS6 修改分辨率,density
NEXUS6原density数值: 2k屏 560 每一步: 使用RE文件管理器,编辑system/build.prop.将“ro.sif.lcd_density=”的参数改写成为需要修改的数值,保存 ...
- get utc+8 当时时间
/// <summary> /// get utc+8 当时时间 /// </summary> /// <returns></returns> publ ...
- Install Apache, PHP And MySQL On CentOS 7 (LAMP)
This tutorial shows how you can install an Apache2 webserver on a CentOS 7.0 server with PHP5 suppor ...