文章目录
  1. 1. Question:Reverse Integer
  2. 2. SourceCode:
    1. 2.1. s1
    2. 2.2. s2

Question:Reverse Integer

Reverse digits of an integer.

Example1: x = 123, return 321

Example2: x = -123, return -321

SourceCode:

s1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//笔者提交版本;耗时:6ms;
public class Solution {
public int reverse(int x) {
int tmpNum = Math.abs(x);
StringBuilder tmpStr = new StringBuilder(String.valueOf(tmpNum));
String result = (String)(x > 0 ? tmpStr.reverse().toString() : "-"+tmpStr.reverse().toString());
int re =0;
//边界控制
try {
re =Integer.parseInt(result);
} catch (Exception e) {
// TODO: handle exception
}
return re;
}
}

s2

1
2
//该版本参考了Discuss,还没看Discuss;耗时:ms;
//待写
文章目录
  1. 1. Question:Reverse Integer
  2. 2. SourceCode:
    1. 2.1. s1
    2. 2.2. s2