|
4 | 4 | import java.util.List;
|
5 | 5 |
|
6 | 6 | /**
|
| 7 | + * 228. Summary Ranges |
| 8 | + * |
7 | 9 | * Given a sorted integer array without duplicates, return the summary of its ranges.
|
8 |
| -
|
9 |
| - For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. |
| 10 | + * |
| 11 | + * For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. |
10 | 12 | */
|
11 | 13 | public class _228 {
|
12 | 14 |
|
13 |
| - public static List<String> summaryRanges(int[] nums) { |
14 |
| - List<String> result = new ArrayList<>(); |
15 |
| - for (int i = 0; i < nums.length; i++) { |
16 |
| - String start = String.valueOf(nums[i]); |
17 |
| - int tmpI = i; |
18 |
| - while ((i + 1) < nums.length && (nums[i] + 1) == nums[i + 1]) { |
19 |
| - i++; |
20 |
| - } |
21 |
| - if (tmpI == i) { |
22 |
| - result.add(start); |
23 |
| - } else { |
24 |
| - result.add(start + "->" + String.valueOf(nums[i])); |
| 15 | + public static class Solution1 { |
| 16 | + public List<String> summaryRanges(int[] nums) { |
| 17 | + List<String> result = new ArrayList<>(); |
| 18 | + for (int i = 0; i < nums.length; i++) { |
| 19 | + String start = String.valueOf(nums[i]); |
| 20 | + int tmpI = i; |
| 21 | + while ((i + 1) < nums.length && (nums[i] + 1) == nums[i + 1]) { |
| 22 | + i++; |
| 23 | + } |
| 24 | + if (tmpI == i) { |
| 25 | + result.add(start); |
| 26 | + } else { |
| 27 | + result.add(start + "->" + String.valueOf(nums[i])); |
| 28 | + } |
25 | 29 | }
|
26 |
| - } |
27 |
| - return result; |
28 |
| - } |
29 |
| - |
30 |
| - public static void main(String... args) { |
31 |
| - int[] nums = new int[]{0, 1, 2, 4, 5, 7}; |
32 |
| - List<String> result = summaryRanges(nums); |
33 |
| - for (String s : result) { |
34 |
| - System.out.println(s); |
| 30 | + return result; |
35 | 31 | }
|
36 | 32 | }
|
37 | 33 |
|
|
0 commit comments