Leetcode 07 Reverse Integer

来源:互联网 发布:淘宝上怎么卖电视棒 编辑:程序博客网 时间:2024/06/11 05:35

Reverse digits of an integer.

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

分析:题目要求就是将整数进行翻转,是非常简单的。

public class Solution {    public int reverse(int x) {    int sign = x<0?-1:1;        x = x > 0? x:(-x);    long r=0;    while(x>0)    {    r= r*10+x%10;    x = x/10;    if (r>Integer.MAX_VALUE)        return 0;    }    return (int)r*sign;}}



0 0
原创粉丝点击