LeetCode 7 Reverse Integer(翻转整数)
来源:互联网 发布:协议数据单元包两部分 编辑:程序博客网 时间:2024/06/03 03:36
翻译
翻转一个整型数例1:x = 123, 返回 321例2:x = -123, 返回 -321
原文
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321
Have you thought about this? (来自LeetCode官网)
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.Update (2014-11-10):Test cases had been added to test the overflow behavior.
C#
public class Solution{ public int Reverse(int x) { try { string str = Math.Abs(x).ToString(); string newStr = (x < 0) ? "-" : ""; for (int i = str.Length - 1; i >= 0; i--) { newStr += str[i]; } return int.Parse(newStr); } catch (Exception e) { return 0; } }}
C++(来源于网络)
class Solution {public: int reverse(int x) { int res = 0; while(x!=0){ if(res>INT_MAX/10||res<INT_MIN/10){ return 0; } res = res*10 + x%10; x = x/10; } return res; }};
2 0
- LeetCode 7 Reverse Integer(翻转整数)
- LeetCode | Reverse Integer(翻转整数)
- [C++]LeetCode 7:Reverse Integer(翻转整数)
- LeetCode #7 Reverse Integer 翻转整数 解题小节
- leetcode题集——reverse-integer(翻转十进制整数)
- 翻转int型整数(Reverse Integer<from LeetCode>)
- LeetCode | 7.Reverse integer(整数翻转)
- Reverse Integer 翻转整数
- 【007-Reverse Integer(翻转整数)】
- [LeetCode-7] Reverse Integer(反转整数)
- [C++]Reverse Integer整数翻转
- 每日一练之Reverse Integer[LeetCode No.7]-翻转整数
- 【LeetCode-面试算法经典-Java实现】【007-Reverse Integer(翻转整数)】
- leetcode 7. Reverse Integer(C语言,翻转一个整数,判断是否溢出)19
- Reverse Integer 翻转数字@LeetCode
- [LeetCode 7]Reverse Integer(处理整数溢出)
- LeetCode 7 :Reverse Integer ---- 反转int整数
- LeetCode 7. Reverse Integer(反转整数)
- TCP-socket发送结构体类型数据
- 【读书笔记】iOS-AppKit简介
- Android 5.1.1谷歌源生编译为Android Lollipop编译固件Nexus5 Nexus6 Nexus7
- LeetCode Add Digits
- 基于xml的springMVC请求与数据返回
- LeetCode 7 Reverse Integer(翻转整数)
- 第15章 进程间通信
- c3p0数据库连接池死锁问题
- 学习NotesList(Robotium自带的例子)
- hdu5452 离线最近公共祖先
- MySQL添加用户、删除用户与授权
- qt for mac 开发的程序怎么签名发布
- 精通正则表达式之元字符
- Ubuntu mysql 彻底卸载重装