-
Notifications
You must be signed in to change notification settings - Fork 5
/
NextBiggestInteger.java
86 lines (74 loc) · 2.45 KB
/
NextBiggestInteger.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
Given an integer, find the next biggest integer whose digits are in increasing order.
Example:
Input: 118
Output: 123
Input: 127
Output: 234
Input: 987
Output: 1234
*/
public class NextLargerNumber {
public static void main(String[] args) {
NextLargerNumber obj = new NextLargerNumber();
int result = obj.findNextLargetNum(91);
System.out.println(result);
}
public int findNextLargetNum(int num) {
if (num <= 0) {
return -1;
}
String str = String.valueOf(num);
int index = 0;
while (index < str.length() - 1) {
char curChar = str.charAt(index);
char nextChar = str.charAt(index + 1);
if (Character.getNumericValue(curChar) + 1 < Character.getNumericValue(nextChar)) {
String result = getResultWithChangeFirstChar(str);
int nextInt = Integer.parseInt(result);
return nextInt;
}
else if (Character.getNumericValue(curChar) + 1 > Character.getNumericValue(nextChar)) {
String result = getResultWithoutChangeFirstChar(str);
int nextInt = Integer.parseInt(result);
return nextInt;
}
index++;
}
String result = getResultWithChangeFirstChar(str);
int nextInt = Integer.parseInt(result);
return nextInt;
}
private String getResultWithoutChangeFirstChar(String str) {
if (Character.getNumericValue(str.charAt(0)) + str.length() > 9) {
return getResultWithChangeFirstChar(str);
}
StringBuilder sb = new StringBuilder();
char firstChar = str.charAt(0);
int length = str.length();
while (length > 0) {
sb.append(firstChar);
firstChar = (char)(firstChar + 1);
length--;
}
return sb.toString();
}
private String getResultWithChangeFirstChar(String str) {
int length = str.length();
char firstChar = str.charAt(0);
StringBuilder sb = new StringBuilder();
if (Character.getNumericValue(str.charAt(0)) + str.length() > 9) {
length = length + 1;
firstChar = '1';
}
else {
firstChar = (char)(firstChar + 1);
}
while (length > 0) {
sb.append(firstChar);
firstChar = (char)(firstChar + 1);
length--;
}
return sb.toString();
}
}