Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit31ca7d2

Browse files
author
JINGUIWANG
committed
Reverse Integer
Change-Id: I1e5f541b15c790bb6b21dd60c3678fe9f22744e6
1 parent170380d commit31ca7d2

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

‎ReverseInteger.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 14, 2014
4+
Problem: Reverse Integer
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/reverse-integer/
7+
Notes:
8+
Reverse digits of an integer.
9+
Example1: x = 123, return 321
10+
Example2: x = -123, return -321
11+
12+
Have you thought about this?
13+
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
14+
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
15+
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
16+
Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).
17+
18+
Solution: Use % and / iteratively.
19+
*/
20+
21+
publicclassSolution {
22+
publicintreverse(intx) {
23+
longres =0;
24+
while (x !=0) {
25+
res =res *10 +Long.valueOf(x %10 );
26+
x =x /10;
27+
}
28+
if (res <Integer.MIN_VALUE ||res >Integer.MAX_VALUE)return0;
29+
return (int)res;
30+
}
31+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp