Skip to content

Commit 8670833

Browse files
committed
feat: add solutions to lc problem: No.0228. Summary Ranges
1 parent accb7ad commit 8670833

File tree

7 files changed

+277
-85
lines changed

7 files changed

+277
-85
lines changed

solution/0200-0299/0228.Summary Ranges/README.md

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
<li><code>nums</code> 按升序排列</li>
7575
</ul>
7676

77-
7877
## 解法
7978

8079
<!-- 这里可写通用的实现逻辑 -->
@@ -86,15 +85,117 @@
8685
<!-- 这里可写当前语言的特殊实现逻辑 -->
8786

8887
```python
89-
88+
class Solution:
89+
def summaryRanges(self, nums: List[int]) -> List[str]:
90+
def make(nums, i, j):
91+
return str(nums[i]) if i == j else f'{nums[i]}->{nums[j]}'
92+
93+
i = j = 0
94+
n = len(nums)
95+
res = []
96+
while j < n:
97+
while j + 1 < n and nums[j] + 1 == nums[j + 1]:
98+
j += 1
99+
res.append(make(nums, i, j))
100+
i = j + 1
101+
j = i
102+
return res
90103
```
91104

92105
### **Java**
93106

94107
<!-- 这里可写当前语言的特殊实现逻辑 -->
95108

96109
```java
110+
class Solution {
111+
public List<String> summaryRanges(int[] nums) {
112+
List<String> res = new ArrayList<>();
113+
for (int i = 0, j = 0, n = nums.length; j < n;) {
114+
while (j + 1 < n && nums[j] + 1 == nums[j + 1]) {
115+
++j;
116+
}
117+
res.add(make(nums, i, j));
118+
i = j + 1;
119+
j = i;
120+
}
121+
return res;
122+
}
123+
124+
private String make(int[] nums, int i, int j) {
125+
return i == j ? String.valueOf(nums[i]) : String.format("%d->%d", nums[i], nums[j]);
126+
}
127+
}
128+
```
129+
130+
### **C++**
131+
132+
```cpp
133+
class Solution {
134+
public:
135+
vector<string> summaryRanges(vector<int>& nums) {
136+
vector<string> res;
137+
for (int i = 0, j = 0, n = nums.size(); j < n;) {
138+
while (j + 1 < n && nums[j] + 1 == nums[j + 1]) ++j;
139+
res.push_back(make(nums, i, j));
140+
i = j + 1;
141+
j = i;
142+
}
143+
return res;
144+
}
145+
146+
string make(vector<int>& nums, int i, int j) {
147+
return i == j ? to_string(nums[i]) : to_string(nums[i]) + "->" + to_string(nums[j]);
148+
}
149+
};
150+
```
151+
152+
### **Go**
153+
154+
```go
155+
func summaryRanges(nums []int) []string {
156+
var res []string
157+
for i, j, n := 0, 0, len(nums); j < n; {
158+
for j+1 < n && nums[j]+1 == nums[j+1] {
159+
j++
160+
}
161+
res = append(res, make(nums, i, j))
162+
i = j + 1
163+
j = i
164+
}
165+
return res
166+
}
167+
168+
func make(nums []int, i, j int) string {
169+
if i == j {
170+
return strconv.Itoa(nums[i])
171+
}
172+
return strconv.Itoa(nums[i]) + "->" + strconv.Itoa(nums[j])
173+
}
174+
```
97175

176+
### **C#**
177+
178+
```cs
179+
public class Solution {
180+
public IList<string> SummaryRanges(int[] nums) {
181+
var res = new List<string>();
182+
for (int i = 0, j = 0, n = nums.Length; j < n;)
183+
{
184+
while (j + 1 < n && nums[j] + 1 == nums[j + 1])
185+
{
186+
++j;
187+
}
188+
res.Add(make(nums, i, j));
189+
i = j + 1;
190+
j = i;
191+
}
192+
return res;
193+
}
194+
195+
public string make(int[] nums, int i, int j) {
196+
return i == j ? nums[i].ToString() : string.Format("{0}->{1}", nums[i], nums[j]);
197+
}
198+
}
98199
```
99200

100201
### **...**

solution/0200-0299/0228.Summary Ranges/README_EN.md

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,122 @@
7070
<li><code>nums</code> is sorted in ascending order.</li>
7171
</ul>
7272

73-
7473
## Solutions
7574

7675
<!-- tabs:start -->
7776

7877
### **Python3**
7978

8079
```python
81-
80+
class Solution:
81+
def summaryRanges(self, nums: List[int]) -> List[str]:
82+
def make(nums, i, j):
83+
return str(nums[i]) if i == j else f'{nums[i]}->{nums[j]}'
84+
85+
i = j = 0
86+
n = len(nums)
87+
res = []
88+
while j < n:
89+
while j + 1 < n and nums[j] + 1 == nums[j + 1]:
90+
j += 1
91+
res.append(make(nums, i, j))
92+
i = j + 1
93+
j = i
94+
return res
8295
```
8396

8497
### **Java**
8598

8699
```java
100+
class Solution {
101+
public List<String> summaryRanges(int[] nums) {
102+
List<String> res = new ArrayList<>();
103+
for (int i = 0, j = 0, n = nums.length; j < n;) {
104+
while (j + 1 < n && nums[j] + 1 == nums[j + 1]) {
105+
++j;
106+
}
107+
res.add(make(nums, i, j));
108+
i = j + 1;
109+
j = i;
110+
}
111+
return res;
112+
}
113+
114+
private String make(int[] nums, int i, int j) {
115+
return i == j ? String.valueOf(nums[i]) : String.format("%d->%d", nums[i], nums[j]);
116+
}
117+
}
118+
```
119+
120+
### **C++**
121+
122+
```cpp
123+
class Solution {
124+
public:
125+
vector<string> summaryRanges(vector<int>& nums) {
126+
vector<string> res;
127+
for (int i = 0, j = 0, n = nums.size(); j < n;) {
128+
while (j + 1 < n && nums[j] + 1 == nums[j + 1]) ++j;
129+
res.push_back(make(nums, i, j));
130+
i = j + 1;
131+
j = i;
132+
}
133+
return res;
134+
}
135+
136+
string make(vector<int>& nums, int i, int j) {
137+
return i == j ? to_string(nums[i]) : to_string(nums[i]) + "->" + to_string(nums[j]);
138+
}
139+
};
140+
```
141+
142+
### **Go**
143+
144+
```go
145+
func summaryRanges(nums []int) []string {
146+
var res []string
147+
for i, j, n := 0, 0, len(nums); j < n; {
148+
for j+1 < n && nums[j]+1 == nums[j+1] {
149+
j++
150+
}
151+
res = append(res, make(nums, i, j))
152+
i = j + 1
153+
j = i
154+
}
155+
return res
156+
}
157+
158+
func make(nums []int, i, j int) string {
159+
if i == j {
160+
return strconv.Itoa(nums[i])
161+
}
162+
return strconv.Itoa(nums[i]) + "->" + strconv.Itoa(nums[j])
163+
}
164+
```
87165

166+
### **C#**
167+
168+
```cs
169+
public class Solution {
170+
public IList<string> SummaryRanges(int[] nums) {
171+
var res = new List<string>();
172+
for (int i = 0, j = 0, n = nums.Length; j < n;)
173+
{
174+
while (j + 1 < n && nums[j] + 1 == nums[j + 1])
175+
{
176+
++j;
177+
}
178+
res.Add(make(nums, i, j));
179+
i = j + 1;
180+
j = i;
181+
}
182+
return res;
183+
}
184+
185+
public string make(int[] nums, int i, int j) {
186+
return i == j ? nums[i].ToString() : string.Format("{0}->{1}", nums[i], nums[j]);
187+
}
188+
}
88189
```
89190

90191
### **...**
Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,21 @@
11
class Solution {
22
public:
33
vector<string> summaryRanges(vector<int>& nums) {
4-
int len = nums.size();
5-
if(len == 0)return {};
6-
vector<string> ans;
7-
int count = 0;
8-
int idx = 0;
9-
while((idx + count) < len-1){
10-
if(nums[idx+count] == nums[idx+count+1]-1)count++;
11-
else{
12-
string str;
13-
if(count == 0){
14-
str = to_string(nums[idx]);
15-
}
16-
else{
17-
str = to_string(nums[idx])+"->"+to_string(nums[idx+count]);
18-
}
19-
ans.push_back(str);
20-
idx += (count+1);
21-
count = 0;
22-
}
4+
int n = nums.size();
5+
if (n == 0) {
6+
return {};
237
}
24-
25-
//末尾处理
26-
string str;
27-
if(count > 0)
28-
str = to_string(nums[idx])+"->"+to_string(nums[idx+count]);
29-
else
30-
str = to_string(nums[idx]);
31-
32-
ans.push_back(str);
33-
34-
return ans;
35-
8+
vector<string> res;
9+
for (int i = 0, j = 0; j < n;) {
10+
while (j + 1 < n && nums[j] + 1 == nums[j + 1]) ++j;
11+
res.push_back(make(nums, i, j));
12+
i = j + 1;
13+
j = i;
14+
}
15+
return res;
16+
}
17+
18+
string make(vector<int>& nums, int i, int j) {
19+
return i == j ? to_string(nums[i]) : to_string(nums[i]) + "->" + to_string(nums[j]);
3620
}
3721
};
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
using System.Collections.Generic;
2-
31
public class Solution {
42
public IList<string> SummaryRanges(int[] nums) {
5-
var result = new List<string>();
6-
int? start = null;
7-
for (var i = 0; i < nums.Length; ++i)
3+
var res = new List<string>();
4+
for (int i = 0, j = 0, n = nums.Length; j < n;)
85
{
9-
if (start == null) start = nums[i];
10-
if (i == nums.Length - 1 || nums[i] + 1 < nums[i + 1])
6+
while (j + 1 < n && nums[j] + 1 == nums[j + 1])
117
{
12-
result.Add(start == nums[i] ? start.ToString() : string.Format("{0}->{1}", start, nums[i]));
13-
start = null;
8+
++j;
149
}
10+
res.Add(make(nums, i, j));
11+
i = j + 1;
12+
j = i;
1513
}
16-
return result;
14+
return res;
15+
}
16+
17+
public string make(int[] nums, int i, int j) {
18+
return i == j ? nums[i].ToString() : string.Format("{0}->{1}", nums[i], nums[j]);
1719
}
1820
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func summaryRanges(nums []int) []string {
2+
var res []string
3+
for i, j, n := 0, 0, len(nums); j < n; {
4+
for j+1 < n && nums[j]+1 == nums[j+1] {
5+
j++
6+
}
7+
res = append(res, make(nums, i, j))
8+
i = j + 1
9+
j = i
10+
}
11+
return res
12+
}
13+
14+
func make(nums []int, i, j int) string {
15+
if i == j {
16+
return strconv.Itoa(nums[i])
17+
}
18+
return strconv.Itoa(nums[i]) + "->" + strconv.Itoa(nums[j])
19+
}
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
class Solution {
22
public List<String> summaryRanges(int[] nums) {
33
List<String> res = new ArrayList<>();
4-
for (int i = 0, j = 1; i < nums.length; i = j++) {
5-
while (j < nums.length && nums[j] - nums[j - 1] == 1) {
4+
for (int i = 0, j = 0, n = nums.length; j < n;) {
5+
while (j + 1 < n && nums[j] + 1 == nums[j + 1]) {
66
++j;
77
}
8-
if (j - i == 1) {
9-
res.add(String.valueOf(nums[i]));
10-
} else {
11-
res.add(nums[i] + "->" + nums[j - 1]);
12-
}
8+
res.add(make(nums, i, j));
9+
i = j + 1;
10+
j = i;
1311
}
1412
return res;
1513
}
16-
}
14+
15+
private String make(int[] nums, int i, int j) {
16+
return i == j ? String.valueOf(nums[i]) : String.format("%d->%d", nums[i], nums[j]);
17+
}
18+
}

0 commit comments

Comments
 (0)