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+ ...
随机推荐
- Gym 100247A The Power of the Dark Side
https://vjudge.net/problem/Gym-100247A 题意: 每个绝地武士有三个能力值a,b,c,两个武士决斗时谁有两个值大于对方谁就是胜者(a和a比,b和b比,c和c比,所有 ...
- 自定义标签TLD文件中,rtexprvalue子标签的意思
rtexprvalue的全称是 Run-time Expression Value, 它用于表示是否能够利用JSP表达式. 举例子: 1.定义一个TLD文件: <tag> <name ...
- 【Ruby】【高级编程】正则
#[[正则]]=beginsub 和 gsub 及它们的替代变量 sub! 和 gsub! 是使用正则表达式时重要的字符串方法.所有这些方法都是使用正则表达式模式执行搜索与替换操作.sub 和 sub ...
- 更新:在MAC上安装RN开发环境的步骤(全)
总共分为三部: 1:按照官网(中文)上的步骤去安装jdk和android studio 2:配置SDK 3:安装虚拟机和模拟器 所以这里提出的是注意事项: 1:~/.bash_profile 文件里面 ...
- Spring Security 中的加密BCryptPasswordEncoder
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler ...
- Python安装第三方库的安装技巧
电脑:Windows10 64位. Python IDE 软件:JetBrains PyCharm Community Edition 2018.1.3 x64 Python version : Py ...
- 3.git、TortoiseGit的安装、仓库的配置教程
参考:https://blog.csdn.net/hc_ttxs/article/details/79375788 引言: Git: 就是最原始的分布式版本控制系统,是开源的. GitHub:与Git ...
- 关于fstream、ifstream、ofstream读写文本文件、二进制文件详解
fstream.ifstream.ofstream是c++中关于文件操作的三个类 fstream类对文件进行读操作和写操作 打开文件 fstream fs("要打开的文件名",打开 ...
- 牛客小白月赛7 B 自杀游戏
自杀游戏 思路: sg函数 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include&l ...
- ZOJ 3965 Binary Tree Restoring
Binary Tree Restoring 思路: 递归 比较a序列和b序列中表示同一个子树的一段区间,不断递归 代码: #include<bits/stdc++.h> using nam ...