7. Reverse Integer java solutions

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?

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.

Subscribe to see which companies asked this question

按照题意,需要考虑末尾为0的情况,溢出的情况,为负数的情况等。

 1 public class Solution {
 2     public int reverse(int x) {
 3         boolean isnegative = false;
 4         if(x < 0) isnegative = true;
 5         long ans = 0;
 6         x = Math.abs(x);
 7         while(x > 0){
 8             ans = (ans*10) + (x%10);
 9             x /= 10;
10         }
11         if(ans > Integer.MAX_VALUE || ans < Integer.MIN_VALUE) return 0;
12         if(isnegative) ans = -ans;
13         return (int)ans;
14     }
15 }
时间: 2024-11-07 22:49:08

7. Reverse Integer java solutions的相关文章

【LeetCode】Reverse Integer (2 solutions)

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: 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 alread

LeetCode 007 Reverse Integer - Java

Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Note:The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. 定位:简单题 将输入的数反转输出,注意的是负数符号保持在最前,反转后的

Reverse Integer (JAVA)

Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 1 public class Solution { 2 public int reverse(int x) { 3 String str=x+""; 4 boolean isNeg=false; 5 if(str.charAt(0)=='-') 6 { 7 str=str.substring(1); 8 i

leetcode 7. Reverse Integer [java]

public int reverse(int x) { long res = 0; while (x != 0){ res = res* 10 + x % 10; x /= 10; } if(res >= Integer.MAX_VALUE || res < Integer.MIN_VALUE) return 0; return (int)res; } 原文地址:https://www.cnblogs.com/whyaza/p/10661942.html

344. Reverse String Java Solutions

Write a function that takes a string as input and returns the string reversed. Example: Given s = "hello", return "olleh". Subscribe to see which companies asked this question 1 public class Solution { 2 public String reverseString(Str

LeetCode第[7]题(Java):Reverse Integer 标签:数学

题目:Reverse Integer 难度:Easy 题目内容: Given a 32-bit signed integer, reverse digits of an integer. Note:Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assum

Reverse Integer - 反转一个int,溢出时返回0

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 若反转的数溢出,直接返回0 可以用计算结果来判断溢出,也可以用因数来判断 Java代码实现: 1 public class ReverseInteger { 2 public static int reverseInt(int x){ 3 if (x == 0) { 4 return

LeetCode:Reverse Integer - 翻转数字

1.题目名称 Reverse Integer(翻转数字) 2.题目地址 https://leetcode.com/problems/reverse-integer/ 3.题目内容 英文:Reverse digits of an integer. 中文:翻转一个正整数的各位,形成一个新数字 例如:x = 123, return 321:x = -123, return -321 4.一个有瑕疵的方法(不能AC) 一个比较好想到的方法,是先将输入的数字转换为字符串,再将字符串翻转后转换为数字.这个方

LeetCode 7 Reverse Integer(反转数字)

题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题思路: 其实这道题看起来非常简单,要实现也是几行代码的事.但是有个小问题容易被忽略,就是边界问题.什么意思呢?如果我们输入的整数超出了int的表达范围,这个问题要怎么解决呢? 用比int更大的数据类型存储我们转