7. Reverse Integer&&190. Reverse Bits
2016-05-24 11:05
155 查看
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
思路:并没有什么特殊的……用一个long int来防止溢出并检验溢出
Reverse Bits是相关的题目
Reverse bits of a given 32 bits unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000).
Follow up:
If this function is called many times, how would you optimize it?
思路:化成二进制再传上去……可以看下discuss区,各种大神各种叼……感觉自己和人家有智商上的差距……
Example1: x = 123, return 321
Example2: x = -123, return -321
思路:并没有什么特殊的……用一个long int来防止溢出并检验溢出
class Solution { public: int reverse(int x) { int n=0; long int sum=0; if(x<0){x=x*(-1);n=1;} while(x!=0){ int r=x%10; sum=sum*10+r; x=x/10; } if(sum>2147483647||sum<-2147483647) return 0; if(n)return (int)-sum; return (int)sum; } };
Reverse Bits是相关的题目
Reverse bits of a given 32 bits unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000).
Follow up:
If this function is called many times, how would you optimize it?
思路:化成二进制再传上去……可以看下discuss区,各种大神各种叼……感觉自己和人家有智商上的差距……
相关文章推荐
- 实现多点跳跃登录Xshell使用教程
- Java transient关键字使用小记
- iOS上传照片:图片尺寸压缩,上传时大小压缩,QBImagePickerController多选照片出现的问题
- Android百度地图开发(2)
- ExtJs (3.2.0)文件目录介绍、文件删减、文件引用
- PowerPoint的几个快捷键
- 浏览器Uncaught QuotaExceededError错误(localStorage超出限额)
- redux在react-native中的使用
- 数据库-触发器
- JavaFX Scene Builder 8.2 下载地址
- PERL 使用IO::Socket::INET模块实现socket编程
- VC调用QT的UIDLL
- 数据库中的连接(JOIN)
- hdu 2390 Olympic Games【贪心】
- Django 执行migrate未成功创建表
- 百度地图开发(1)
- 林嘉文
- 故障恢复和恢复模式(Crash Recovery & Recovery Models)
- Oracle SQL树形结构查询
- 从头认识多线程-2.26 synchronized同步代码块在jvm是怎样执行的?