[leetcode][javascript]Reverse Bits
2015-06-24 21:15
639 查看
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).
Follow up:
If this function is called many times, how would you optimize it?
之前对位操作不是很熟,经过这题有所理解。
js的符号位是挥之不去的,而当作位操作时,js的位操作限制在32位,除去一个符号位,其上限只能达到2^31 - 1,so,符号位上的数值需要单独计算
代码如下
然后顺便吐槽下,leetcode 的runtime在JS下没什么参考性,浮动好大。
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).
Follow up:
If this function is called many times, how would you optimize it?
之前对位操作不是很熟,经过这题有所理解。
js的符号位是挥之不去的,而当作位操作时,js的位操作限制在32位,除去一个符号位,其上限只能达到2^31 - 1,so,符号位上的数值需要单独计算
代码如下
/** * @param {number} n - a positive integer * @return {number} - a positive integer */ var reverseBits = function(n) { var tmp ,res = 0; var count = 1; var sign = n&1; n = n>>>1; while(n){ tmp = n&1; res = res|tmp<<(31-count); ++count; n = n>>>1; } if(sign){ res +=2147483648; } return res; };
然后顺便吐槽下,leetcode 的runtime在JS下没什么参考性,浮动好大。
相关文章推荐
- EL表达式在JSP界面中取不到值问题。
- IE11下请求JSON下载文件
- JSP页面EL表达式无效的问题
- 7种js延时加载执行的方法
- Intention.js:轻量级响应式开发工具
- Ember.js学习总结
- ArcGIS API for Javascript 应用开发入门
- Objective-C与Javascript相互通信的中间件
- js获取屏幕大小的方法
- 深入理解javascript之原型
- Js-动态控制table的tr、td增加及删除的具体实现
- js-form进行reset重置
- js--小结⑦---格式转换
- response.getWriter().write()与JspWriter的out.print()的区别
- js--小结⑥---typeof
- javascript 阻止浏览器弹窗
- js--小结⑤
- js数组去重
- QQ客服弹窗js
- BZOJ1030 [JSOI2007]文本生成器(AC自动机+dp)