leetcode Reverse Integer

来源:互联网 发布:javascript 隐藏div 编辑:程序博客网 时间:2024/05/29 10:37

题目

Reverse digits of an integer.

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

取余运算

public class Solution {    public int reverse(int x) {        int flag=1;        if(x<0){            flag=-1;            x=-x;        }                int ret=0;        while(x>0){            ret=ret*10+x%10;            x/=10;        }        return ret*flag;    }}


0 0