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

Commit353ce78

Browse files
reverse integer
1 parentd9b7804 commit353ce78

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

‎EASY/src/easy/ReverseInteger.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
packageeasy;
2+
/**7. Reverse Integer QuestionEditorial Solution My Submissions
3+
Total Accepted: 155938
4+
Total Submissions: 655150
5+
Difficulty: Easy
6+
Reverse digits of an integer.
7+
8+
Example1: x = 123, return 321
9+
Example2: x = -123, return -321*/
10+
publicclassReverseInteger {
11+
//lastly, after making it AC'ed on my own, I turned to Discuss, and find this very short solution,
12+
//it turns out we don't need to do any handling for the sign
13+
publicintreverse_short_version(intx){
14+
longrev =0;
15+
while(x !=0){
16+
rev =rev*10 +x%10;
17+
x /=10;
18+
if(rev >Integer.MAX_VALUE ||rev <Integer.MIN_VALUE)return0;
19+
}
20+
return (int)rev;
21+
}
22+
23+
//made it AC'ed on my own, cheers!
24+
publicintreverse(intx) {
25+
if(x ==0)return0;
26+
//save the first bit if it's a negative sign
27+
StringBuildersb =newStringBuilder();
28+
sb.append(x);
29+
booleannegative =sb.toString().charAt(0) =='-' ?true :false;
30+
//use modulor and division and use long as the result type to avoid overflow
31+
longlongX = (long)x;
32+
if(negative){
33+
//get rid of the first '-' bit
34+
StringwithoutNegativeSign =sb.substring(1).toString();
35+
longX =Long.parseLong(withoutNegativeSign);
36+
}
37+
sb.setLength(0);
38+
longresult =0;
39+
if(negative){
40+
sb.append('-');
41+
}
42+
while(longX !=0){
43+
sb.append(longX%10);
44+
longX /=10;
45+
}
46+
result =Long.parseLong(sb.toString());
47+
System.out.println(result);//it's right here, but after converting it into an int, it overflowed to become a wrong number, how to handle this?
48+
//it turns out depending on the question requirement, on this OJ, it's expecting 0, if it's beyond the range of (Integer.MIN_VALUE, Integer.MAX_VALUE)
49+
return (result >Integer.MAX_VALUE ||result <Integer.MIN_VALUE) ?0 : (int)result;
50+
}
51+
52+
publicstaticvoidmain(String...strings){
53+
ReverseIntegertest =newReverseInteger();
54+
//when the input is 1534236469, it's expecting 0 as the correct answer, this is due to its reversed number is greater than Integer.MAX_VALUE, thus return 0
55+
System.out.println(1534236469 >Integer.MAX_VALUE);
56+
System.out.println("1534236469\n" +Integer.MAX_VALUE);
57+
// System.out.println(test.reverse(-2147483648));
58+
}
59+
60+
// this is not going to work when the input number's reverse version is greater than
61+
// Integer.MAX_VALUE, it'll throw NumberFormatException as Java cannot handle it, overflowed.
62+
publicintreverse_overflowed(intx) {
63+
//save the first bit if it's a negative sign
64+
StringBuildersb =newStringBuilder();
65+
sb.append(x);
66+
booleannegative =sb.toString().charAt(0) =='-' ?true :false;
67+
char[]bits =sb.toString().toCharArray();
68+
sb.setLength(0);
69+
if(negative){
70+
sb.append('-');
71+
//until i > 0
72+
for(inti =bits.length-1;i >0;i--){
73+
sb.append(bits[i]);
74+
}
75+
}else {
76+
//until i >= 0
77+
for(inti =bits.length-1;i >=0;i--){
78+
sb.append(bits[i]);
79+
}
80+
}
81+
returnInteger.parseInt(sb.toString());
82+
}
83+
84+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp