这篇博文的代码有bug,新博文地址[leetcode]Reverse Integer
Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Have you thought about this?
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?
Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Have you thought about this?
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?
Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).
两个tips很有意思:
1. 10的倍数,怎么返回?(返回值类型是int,肯定要无视首位的0)
2. 溢出,怎么处理?(这个第一次没考虑到实在是太不应该了)但是就算不处理也AC了,难道是怕每个人处理溢出的做法不同,就没有设置那么强的数据?
不多说,没啥难度,代码如下
public int reverse(int x) { int result = 0; while(x!=0){ int temp = x % 10; x /= 10; if(10 * result + temp < Integer.MAX_VALUE || 10 * result + temp > Integer.MIN_VALUE){ result = 10 * result + temp; }else{ result = 10 * result + temp> Integer.MAX_VALUE? Integer.MAX_VALUE:Integer.MIN_VALUE; } } return result; }
相关推荐
Given a 32-bit signed integer, reverse digits of an integer. Java AC版本
第321题Reversed_Integer Java中的LeetCode逆整数问题解决方案 问题 - 给定一个有符号的 32 位整数 x,返回 x 其数字颠倒。 如果反转 x 导致值超出有符号的 32 位整数范围 [-231, 231 - 1],则返回 0。 假设环境不...
Leetcode\ReverseInteger\ReverseInteger.cs 问题: 业绩报告: 回文数 代码: Leetcode\PalindromeNumber\PalindromeNumber.cs 问题: 从排序数组中删除重复项 代码: Leetcode\RemoveDuplicates\RemoveDuplicates....
7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 13. Roman to Integer 15. 3Sum 16. 3Sum Closest 17. Letter Combinations of a Phone Number 18. 4Sum 19. Remove Nth Node From End of ...
java lru leetcode Fighting for a job! 记录找工作期间搬运的题,全部使用Java实现,本人C++鶸 1 ...LeetCode ...LeetCode ...LeetCode ...LeetCode ...LeetCode ...LeetCode ...LeetCode ...LeetCode ...Integer LeetCode 6 Zi
leetcode leetcode练习 twosum 问题 ;add two numbers问题;reverse integer问题;最大不重复子字符串长度问题;atoi问题;
Integer.toString(x); int reversed; int sign = 1; if( s.substring(0,1).equals("-")) { s = s.substring(1); sign = -1; } StringBuilder reservedString = new StringBuilder(); char[] charArray = s....
190 | [Reverse Bits](https://leetcode.com/problems/reverse-bits/) | [C++](./C++/reverse-bits.cpp) [Python](./Python/reverse-bits.py) | _O(1)_ | _O(1)_ | Easy ||| 191 |[Number of 1 Bits]...
leetcode 分类 数据结构和算法学习记录,结合LeetCode刷题 分类 DP 目录 题目 解法 分类 Time ...Reverse Integer/ri7.go) ...Reverse Integer/Solution.java) Math O(log(x)) O(1) JAVA Math JAVA Math
Reverse Integer JavaScript O(n) O(1) Easy 9 Palindrome Number JavaScript O(n) O(1) Easy 19 Remove Nth Node From End of List JavaScript O(n) O(1) Medium 21 Merge Two Sorted Lists JavaScript O(n)
字符串可能的余数LeetCode-7-Reverse-An-Integer 我对 LeetCode #7 的解决方案 我处理这个问题的目的是优化时间复杂度而不是空间复杂度。 因此,我避免了通用方法链接解决方案,在这种解决方案中,输入被转换为字符...
lru缓存leetcode leetcode 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 5. Longest Palindromic Substring 7. Reverse Integer 9. ...
Reverse Integer 整数反转 9 Palindrome Number 回文数 11 Container With Most Water 盛最多水的容器 13 Roman to Integer 罗马数字转整数 14 Longest Common Prefix 最长公共前缀 20 Valid Parentheses 有效的括号...
Reverse Integer.cpp 9 回文数 Palindrome Number.cpp 12 整数转罗马数字 Integer to Roman.cpp 13 罗马数字转整数 Roman to Integer.cpp 15 三数之和 3Sum.cpp 最接近的三数之和 3Sum Closest .cpp 20 有效的括号 ...
leetcode分类LeetCodeSourceCodes###LeetCode1TowSum如果数组是有序的,O(n)的时间复杂度就可以解决了,现在是...###LeetCode7ReverseInteger注意数字反正后可能超出Int32范围,此时应当抛异常###LeetCode8StringtoInt
leetcode 知乎 此项目介绍 此项目旨在是为leetcode里面的习题提供解析(数据结构相关知识),锻炼了自己,同时也给别人带来了方便。 网站习题链接: ** 作者知乎链接**: 关于习题 ...Integer Strin
Reverse Integer 简单 8 String to Integer (atoi) 中等 9 Palindrome Number 简单 11 Container With Most Water 中等 12 Integer to Roman 中等 13 Roman to Integer 简单 14 Longest Common Prefix 简单 15 3Sum ...
7.Reverse Integer 8.String To Integer 9.Palindrome Number 10.String To Integer 11.Container With Most Water 12.Integer To Roman 13.Roman To Integer 289 347 380 442 457 Circular Array Loop 535 Encode ...
LeetCode刷题总结 1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7....
leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 重要 Longest Substring Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 ...