C++ leetcode::Reverse Integer
第一天上课,数据库老师说对于计算机系的学生,凡是在课本上学到的专业知识都是过时的。深以为然,感觉大学两年半真的不知道学了什么,为未来感到担忧,C++也不敢说是精通,入门还差不多。最近丧的不行,不管怎么样,每天还是要进步一点点。
题目:
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
感觉不难,写了才发现自己没考虑溢出的问题,为判断溢出折腾了好久,心好累,提交之后23ms,击败20%。什么时候才能击败100%呢?
class Solution {
public:
int reverse(int x) {
if (x<= && x>=-)
return x;
int MAX = INT_MAX/;
int MIN = INT_MIN/;
int result = ;
while(x != ){
if ( result>MAX || result<MIN||(x > && INT_MAX-x%<result*)|| (x< && INT_MIN-x%>result*) )
return ;
result = result* + x%;
x=x/;
}
return result;
}
};
本来想把result设置成long的,但是我的编译器int和long占的字节一样,所以就只能用这样个笨方法来判断,就当是学习了。但是leetcode的编译器int和long不是占相同字节的。
C++ leetcode::Reverse Integer的更多相关文章
- LeetCode: Reverse Integer 解题报告
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- [LeetCode] Reverse Integer 翻转整数
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
- [Leetcode] reverse integer 反转整数
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...
- LeetCode——Reverse Integer(逆置一个整数)
问题: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return –321 Ha ...
- Leetcode: Reverse Integer 正确的思路下-要考虑代码简化
题目: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have ...
- leetcode:Reverse Integer【Python版】
1.在进入while之前,保证x是非负的: 2.符号还是专门用flag保存 =================== 3.另一思路:将integer转换成string,然后首位swap,直至中间: cl ...
- LeetCode——Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...
- [Leetcode]Reverse Integer
核心思想:原数对10取余数赋值给新数后降一位,再把新数升一位加上下一次原数取余值,直到原数降为0. 解法如下: int reverse(int x) { bool minus = false; ) ...
- leetcode reverse integer&&Palindrome Number
public class Solution { public int reverse(int x) { int ret=0; while(x!=0) { int t=x%10; ret=ret*10+ ...
随机推荐
- 正则解析json数据
http://tool.chinaz.com/regex http://tool.oschina.net/regex/
- 用R创建Word和PowerPoint文档--转载
https://www.jianshu.com/p/7df62865c3ed Rapp --简书 Microsoft的Office软件在办公软件领域占有绝对的主导地位,几乎每个职场人士都必须掌握Wor ...
- 【Selenium2】【selenium之 定位以及切换frame(iframe)】
参考:http://blog.csdn.net/huilan_same/article/details/52200586 总有人看不明白,以防万一,先在开头大写加粗说明一下: frameset不用切, ...
- django核心配置项
Django的默认配置文件中,包含上百条配置项目,其中很多是我们‘一辈子’都不碰到或者不需要单独配置的,这些项目在需要的时候再去查手册. 强调:配置的默认值不是在settings.py文件中!不要以为 ...
- C#连接数据库open函数失败
错误信息:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接. (provider ...
- eclipse导入项目文件以及 import项目文件后有个红色感叹号
eclipse导入项目文件 步骤:File —>Import—>General—>Existing Projects into Workspace 然后进去选择项目文件的具体路径即可 ...
- less 前端css利器 工程化、逻辑化 css 文件
less LESS 将 CSS 赋予了动态语言的特性,如 变量, 继承,运算, 函数. 1. 浏览器方式 1.1 html <!DOCTYPE html> <html lang=&q ...
- 使用pipeline减少与redis交互次数
1.redis_pipeline=redis_cli.pipeline() 2.redis_pipeline.setex()此语句可写多条 3.redis_pipeline.execute() # # ...
- python+opencv 运行环境搭建
1:安装pycharm,验证码你懂的 2:安装python3.5以上,或3.6,python2和3 的版本差异还蛮大 3:安装opencv,如下图 以上是方法一,还有之中方法是下载whl文件再手动安装 ...
- 昂达 v891 连接上adb 调试
这篇文章可以删除了. 可以看看有关v891root和分区的文章,里面介绍了驱动程序的工具了. USB线连上之后,可以直接访问文件,所以连接本身肯定没有问题的. 可是,总是Windows 那边MTP驱 ...