Skip to content

Commit 427bc88

Browse files
author
Yubin Bai
committed
new
1 parent 3fe6722 commit 427bc88

File tree

5 files changed

+63
-1
lines changed

5 files changed

+63
-1
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,7 @@ out/
6161

6262
# Maven
6363
log/
64-
target/
64+
target/
65+
66+
#sublime
67+
*.sublime-workspace

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Leetcode solutions in Java and Python
33

44
|#|Title|Solution|Difficulty|
55
|---|-----|--------|----------|
6+
|228|[Summary Ranges](https://leetcode.com/problems/summary-ranges/)|[Java](./src/summary_ranges)|Easy|
67
|227|[Basic Calculator II](https://leetcode.com/problems/basic-calculator-ii/)|[Java](./src/basic_calculator_ii)|Medium|
78
|226|[Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/)|[Java](./src/invert_binary_tree)|Easy|
89
|225|[Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues/)|[Java](./src/implement_stack_using_queues)|Medium|

leetcode.sublime-project

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"folders":
3+
[
4+
{
5+
"path": "."
6+
}
7+
],
8+
"build_systems":
9+
[
10+
{
11+
"name": "Run",
12+
"cmd": ["javac \"$file_name\" && java \"$file_base_name\""],
13+
"shell": true,
14+
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
15+
"selector": "source.java"
16+
}
17+
]
18+
}

src/summary_ranges/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Problem
2+
===
3+
4+
Given a sorted integer array without duplicates, return the summary of its ranges.
5+
6+
For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].

src/summary_ranges/Solution.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.*;
2+
3+
public class Solution {
4+
public List<String> summaryRanges(int[] nums) {
5+
List<String> list = new ArrayList<String>();
6+
if (nums.length == 0) return list;
7+
int prev = nums[0];
8+
int curr = prev;
9+
for (int i = 1; i < nums.length; i++) {
10+
if (nums[i] == curr + 1) {
11+
curr++;
12+
} else {
13+
if (prev == curr) {
14+
list.add(String.format("%d", prev));
15+
} else {
16+
list.add(String.format("%d->%d", prev, curr));
17+
}
18+
prev = nums[i];
19+
curr = nums[i];
20+
}
21+
}
22+
if (prev == curr) {
23+
list.add(String.format("%d", prev));
24+
} else {
25+
list.add(String.format("%d->%d", prev, curr));
26+
}
27+
return list;
28+
}
29+
public static void main(String[] args) {
30+
int[] nums = new int[] {0,1,2,4,5,7};
31+
Solution s = new Solution();
32+
s.summaryRanges(nums).forEach(System.out::println);
33+
}
34+
}

0 commit comments

Comments
 (0)