BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / acm-icpc / #91754同步于 2016/12/1
该镜像源已超过 30 天没有更新,可能在源站已被删除。
ACM_ICPC机器人发帖

已解决-Java For Leetcode 190. Reverse Bits

Crab0314
2016/12/1镜像同步16 回复
题目:Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000). 我的代码: public class Solution { // you need treat n as an unsigned value public int reverseBits(int n) { int res=0; for(int i=0;i<=31;i++){ if((n & 1) == 1){ res=res<<1+1; n=n>>1; }else{ res=res<<1; n=n>>1; } } return res; } } 求问一下Java语言该如何解这道题~[ema11]
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
w350053002机器人#1 · 2016/12/1
这个错我写也遇到了。。但是自己都不会怎么在本地环境下模拟出来这个错。。。。
Sluggard机器人#2 · 2016/12/2
public int reverseBits(int n) { int r = 0; for (int i = 0; i < 32; i++) { r <<= 1; r |= (n & 1); n >>= 1; } return r; } 我的答案。
nuanyangyang机器人#3 · 2016/12/2
res=res<<1+1; 这个语句的真正意思是 res=res<<(1+1); 但我想你不是这个意思。 【 在 Crab0314 的大作中提到: 】 : 题目:Reverse bits of a given 32 bits unsigned integer. : For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000). : 我的代码: : ...................
w350053002机器人#4 · 2016/12/2
暖神这个None可能是怎么输入的呢?我对java学的不全面。试了一些直接写null或者Integer都被编译器挡住了 【 在 nuanyangyang 的大作中提到: 】 : res=res<<1+1; : 这个语句的真正意思是 : res=res<<(1+1); : ...................
chy1机器人#5 · 2016/12/2
public int reverseBits(int n) { int result = 0; for(int i = 0;i < 32;i++){ result=result * 2 + (n & 1); n>>>=1; } return result; } 倒过来做二进制运算
nuanyangyang机器人#6 · 2016/12/2
只是服务器不愿意告诉你具体的输入而已。 【 在 w350053002 的大作中提到: 】 : 暖神这个None可能是怎么输入的呢?我对java学的不全面。试了一些直接写null或者Integer都被编译器挡住了
rancho机器人#7 · 2016/12/3
暖神正解 把res = res << 1 + 1 改成 res = (res << 1) + 1 应该可以AC 不通过的这个测例的输入应该就是1吧,然后不知道什么为什么没有显示出来。 (你的代码不改的话,输入1,结果就是0,而实际结果应该是 -2^31) 似乎也可以搜到其他人有这个bug https://discuss.leetcode.com/topic/23671/input-none-output-none-1-1-test-case-passed-wrong-answer/2?page=1
w350053002机器人#8 · 2016/12/3
好吧,谢谢暖神 【 在 nuanyangyang (暖羊羊) 的大作中提到: 】 : 只是服务器不愿意告诉你具体的输入而已。 通过『我邮2.0』发布
q1414998074机器人#9 · 2016/12/3
9楼 【 在 w350053002 的大作中提到: 】 : 好吧,谢谢暖神 【 在 nuanyangyang (暖羊羊) 的大作中提到: 】 : 只是服务器不愿意告诉你具体的输入而已。 通过『我邮2.0』发布 发自「贵邮」