Java for LeetCode 007 Reverse Integer

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

解题思路:
将数字翻转并不难,可以转成String类型翻转,也可以逐位翻转,本题涉及到的主要是边界和溢出问题,使用Long或者BigInteger即可解决。

题目不难:

JAVA实现如下:

public class Solution {
    static public int reverse(int x) {
        if(x==0||x==-2147483648)return 0;

        boolean isNagetive=false;
        if(x<0){
            isNagetive=true;
            x=-x;
        }
        long result=0;
        while(x!=0){
            result*=10;
            result+=x%10;
            x/=10;
        }
        final int INT_MAX=0x7fffffff;
        if((result-INT_MAX)>0)
            return 0;

        if(isNagetive)result=-result;
        return (int)result;
    }
}
时间: 2024-10-10 02:41:51

Java for LeetCode 007 Reverse Integer的相关文章

LeetCode 007 Reverse Integer

[题目] Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 [题意] 反转int型整数,输出的也是int型的整数 [思路] 如要考虑两种特殊情况: 1. 类似100这样的整数翻转之后为1 2. 翻转之后的值溢出该如何处理, 本题的测试用例中似乎没有给出溢出的情况 在实际面试时需要跟面试官明确这种情况的处理方法. 基于这点事实,本题规定如果超出正边界返回INT_MA

[LeetCode] 007. Reverse Integer (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 007.Reverse_Integer (Easy) 链接: 题目:https://oj.leetcode.com/problems/Reverse-Integer/ 代码(github):https://github.com/illuz/leetcode 题意: 反转一个数. 分析: 注意读入和返回的数都是 in

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. 定位:简单题 将输入的数反转输出,注意的是负数符号保持在最前,反转后的

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) 一个比较好想到的方法,是先将输入的数字转换为字符串,再将字符串翻转后转换为数字.这个方

Java for LeetCode 092 Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

Leetcode 数 Reverse Integer

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Reverse Integer Total Accepted: 17472 Total Submissions: 43938 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought

【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-Algorithms #007 Reverse Integer, Database #182 Duplicate Emails

LeetCode-Algorithms #007 Reverse Integer 给定一个32位整数, 将其各位反转并返回, 如果结果超出取值范围就返回0 1 class Solution { 2 public int reverse(int x) { 3 //对原数取绝对值 4 int y = Math.abs(x); 5 //将原数转换为字符串 6 String s1 = Integer.toString(y); 7 //将字符串转换为字符数组 8 char[] arr = s1.toCha

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更大的数据类型存储我们转