-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincrementArray.java
More file actions
56 lines (44 loc) · 1.65 KB
/
incrementArray.java
File metadata and controls
56 lines (44 loc) · 1.65 KB
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
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
// NOTE: Please do not modify this function
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String inputLine = sc.nextLine();
Pattern pattern = Pattern.compile("\\[(.*?)\\]");
Matcher matcher = pattern.matcher(inputLine);
if (matcher.find()) {
String numbers = matcher.group(1);
String[] stringValues = numbers.split(",");
int[] intValues = new int[stringValues.length];
for (int i = 0; i < stringValues.length; i++) {
try {
intValues[i] = Integer.parseInt(stringValues[i].trim());
} catch (NumberFormatException e) {
System.out.println("Invalid integer input: " + stringValues[i].trim());
return;
}
}
int[] array = incrementArray(intValues);
System.out.println(Arrays.toString(array));
} else {
System.out.println("Invalid input format. Please use [1, 2, 3, 4, 5] format.");
}
}
public static int[] incrementArray(int[] digits) {
// Start from the end of the array and handle carry
int n = digits.length;
for (int i = n - 1; i >= 0; i--) {
if (digits[i] < 9) {
digits[i]++;
return digits;
}
digits[i] = 0;
}
// If we are here, it means all the digits were 9
int[] newDigits = new int[n + 1];
newDigits[0] = 1;
return newDigits;
}
}