Leetcode OJ平台上的Reverse Integer题目用Java实现

上一篇 / 下一篇  2014-06-03 09:24:40 / 个人分类:学习

Reverse digits of an integer.

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


原题如上,这个题目十分简单,源代码如下:

public class Solution {
    public static int reverse(int x) {
        int y = Math.abs(x);
        int[] a = new int[11];
        int i = 0 ;
        int reverse = 0;
        for(; y > 0; ++ i){
            a[i] = y % 10;
            y = y / 10;
        }
        for(int j = i-1; j >= 0; -- j){
            //System.out.println(a[j]);
            reverse += a[j] * Math.pow(10,i-j-1) ;
        }
        if(x < 0)
            return -1*reverse;
        else
            return reverse;
    }
}


TAG:

 

评分:0

我来说两句

我的栏目

日历

« 2024-04-26  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 17724
  • 日志数: 12
  • 建立时间: 2014-05-21
  • 更新时间: 2014-06-03

RSS订阅

Open Toolbar