Skip to content

Commit 6f9c5b9

Browse files
refactor 556
1 parent 179e054 commit 6f9c5b9

File tree

2 files changed

+44
-45
lines changed

2 files changed

+44
-45
lines changed

src/main/java/com/fishercoder/solutions/_556.java

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,48 @@
1515
* Output: -1
1616
*/
1717
public class _556 {
18-
//credit: https://discuss.leetcode.com/topic/85759/this-problem-is-the-same-to-next-permutation-algorithm-only and https://discuss.leetcode.com/topic/85755/java-solution-like-next-permutation-problem-o-n
18+
public static class Solution1 {
19+
/**
20+
* credit: https://discuss.leetcode.com/topic/85759/this-problem-is-the-same-to-next-permutation-algorithm-only and https://discuss.leetcode.com/topic/85755/java-solution-like-next-permutation-problem-o-n
21+
*/
1922

20-
public int nextGreaterElement(int n) {
21-
char[] digits = String.valueOf(n).toCharArray();
22-
int i = digits.length - 2;
23-
while (i >= 0 && digits[i + 1] <= digits[i]) {
24-
i--;
23+
public int nextGreaterElement(int n) {
24+
char[] digits = String.valueOf(n).toCharArray();
25+
int i = digits.length - 2;
26+
while (i >= 0 && digits[i + 1] <= digits[i]) {
27+
i--;
28+
}
29+
if (i < 0) {
30+
return -1;
31+
}
32+
int j = digits.length - 1;
33+
while (j >= 0 && digits[j] <= digits[i]) {
34+
j--;
35+
}
36+
swap(digits, i, j);
37+
reverse(digits, i + 1);
38+
try {
39+
return Integer.parseInt(new String(digits));
40+
} catch (Exception e) {
41+
return -1;
42+
}
2543
}
26-
if (i < 0) {
27-
return -1;
28-
}
29-
int j = digits.length - 1;
30-
while (j >= 0 && digits[j] <= digits[i]) {
31-
j--;
32-
}
33-
swap(digits, i, j);
34-
reverse(digits, i + 1);
35-
try {
36-
return Integer.parseInt(new String(digits));
37-
} catch (Exception e) {
38-
return -1;
39-
}
40-
}
4144

42-
private void reverse(char[] a, int start) {
43-
int i = start;
44-
int j = a.length - 1;
45-
while (i < j) {
46-
swap(a, i, j);
47-
i++;
48-
j--;
45+
private void reverse(char[] a, int start) {
46+
int i = start;
47+
int j = a.length - 1;
48+
while (i < j) {
49+
swap(a, i, j);
50+
i++;
51+
j--;
52+
}
4953
}
50-
}
5154

52-
private void swap(char[] a, int i, int j) {
53-
char temp = a[i];
54-
a[i] = a[j];
55-
a[j] = temp;
55+
private void swap(char[] a, int i, int j) {
56+
char temp = a[i];
57+
a[i] = a[j];
58+
a[j] = temp;
59+
}
5660
}
5761

5862
}

src/test/java/com/fishercoder/_556Test.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,36 @@
11
package com.fishercoder;
22

33
import com.fishercoder.solutions._556;
4-
import org.junit.Before;
54
import org.junit.BeforeClass;
65
import org.junit.Test;
76

87
import static org.junit.Assert.assertEquals;
98
import static org.junit.Assert.assertTrue;
109

1110
public class _556Test {
12-
private static _556 test;
11+
private static _556.Solution1 solution1;
1312
private static int n;
1413
private static int expected;
1514
private static int actual;
1615

1716
@BeforeClass
1817
public static void setup() {
19-
test = new _556();
20-
}
21-
22-
@Before
23-
public void setupForEachTest() {
18+
solution1 = new _556.Solution1();
2419
}
2520

2621
@Test
2722
public void test1() {
2823
n = 12;
2924
expected = 21;
30-
actual = test.nextGreaterElement(n);
25+
actual = solution1.nextGreaterElement(n);
3126
assertEquals(expected, actual);
3227
}
3328

3429
@Test
3530
public void test2() {
3631
n = 21;
3732
expected = -1;
38-
actual = test.nextGreaterElement(n);
33+
actual = solution1.nextGreaterElement(n);
3934
assertEquals(expected, actual);
4035
assertTrue(Integer.MAX_VALUE > 1999999999);
4136
}
@@ -44,23 +39,23 @@ public void test2() {
4439
public void test3() {
4540
n = 1999999999;
4641
expected = -1;
47-
actual = test.nextGreaterElement(n);
42+
actual = solution1.nextGreaterElement(n);
4843
assertEquals(expected, actual);
4944
}
5045

5146
@Test
5247
public void test4() {
5348
n = 12222333;
5449
expected = 12223233;
55-
actual = test.nextGreaterElement(n);
50+
actual = solution1.nextGreaterElement(n);
5651
assertEquals(expected, actual);
5752
}
5853

5954
@Test
6055
public void test5() {
6156
n = 12443322;
6257
expected = 13222344;
63-
actual = test.nextGreaterElement(n);
58+
actual = solution1.nextGreaterElement(n);
6459
assertEquals(expected, actual);
6560
}
6661

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy