File tree 3 files changed +43
-9
lines changed
3 files changed +43
-9
lines changed Original file line number Diff line number Diff line change
1
+ package math ;
2
+
3
+ /**
4
+
5
+ * @since 2020/3/8 10:37 PM
6
+ */
7
+ public class LC_7_ReverseInteger {
8
+ /**
9
+ * 思路:利用 取余数的方式取最后一个个位,取出的个位参与结果组装
10
+ */
11
+ public int reverse (int x ) {
12
+ int rev = 0 ;
13
+ while (x != 0 ) {
14
+ int pop = x % 10 ;
15
+ x = x / 10 ;
16
+ if (rev > Integer .MAX_VALUE / 10 || (rev == Integer .MAX_VALUE / 10 && pop > 7 )) return 0 ;
17
+ if (rev < Integer .MIN_VALUE / 10 || (rev == Integer .MIN_VALUE / 10 && pop < -8 )) return 0 ;
18
+ rev = rev * 10 + pop ;
19
+ }
20
+ return rev ;
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ package math ;
2
+
3
+ import org .junit .Test ;
4
+
5
+ import static org .hamcrest .core .Is .is ;
6
+ import static org .junit .Assert .assertThat ;
7
+
8
+ /**
9
+
10
+ * @since 2020/3/8 10:36 PM
11
+ */
12
+ public class LC_7_ReverseIntegerTest {
13
+ @ Test
14
+ public void reverse_integer_test () {
15
+ LC_7_ReverseInteger ri = new LC_7_ReverseInteger ();
16
+ assertThat (ri .reverse (123 ), is (321 ));
17
+ }
18
+ }
Original file line number Diff line number Diff line change 11
11
<name >RicoNut</name >
12
12
<description >RicoNut</description >
13
13
14
- <parent >
15
- <groupId >org.springframework.boot</groupId >
16
- <artifactId >spring-boot-starter-parent</artifactId >
17
- <version >2.0.4.RELEASE</version >
18
- <relativePath /> <!-- lookup parent from repository -->
19
- </parent >
20
14
21
15
<properties >
22
16
<project .build.sourceEncoding>UTF-8</project .build.sourceEncoding>
25
19
</properties >
26
20
27
21
<dependencies >
28
-
29
22
<dependency >
30
- <groupId >org.springframework.boot</groupId >
31
- <artifactId >spring-boot-starter-test</artifactId >
23
+ <groupId >junit</groupId >
24
+ <artifactId >junit</artifactId >
25
+ <version >4.12</version >
32
26
<scope >test</scope >
33
27
</dependency >
34
28
</dependencies >
You can’t perform that action at this time.
0 commit comments