Skip to content

Commit 4b1ae25

Browse files
committed
feat: add solutions to lc problem: No.0228
No.0228.Summary Ranges
1 parent 9809e29 commit 4b1ae25

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363

6464
遍历数组,当 $j + 1 < n$ 且 $nums[j + 1] = nums[j] + 1$ 时,$j$ 向右移动,否则区间 $[i, j]$ 已经找到,将其加入答案,然后将 $i$ 移动到 $j + 1$ 的位置,继续寻找下一个区间。
6565

66-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度
66+
时间复杂度 $O(n)$,其中 $n$ 为数组长度。空间复杂度 $O(1)$。
6767

6868
<!-- tabs:start -->
6969

@@ -74,7 +74,7 @@
7474
```python
7575
class Solution:
7676
def summaryRanges(self, nums: List[int]) -> List[str]:
77-
def f(i, j):
77+
def f(i: int, j: int) -> str:
7878
return str(nums[i]) if i == j else f'{nums[i]}->{nums[j]}'
7979

8080
i = 0
@@ -156,6 +156,26 @@ func summaryRanges(nums []int) (ans []string) {
156156
}
157157
```
158158

159+
### **TypeScript**
160+
161+
```ts
162+
function summaryRanges(nums: number[]): string[] {
163+
const f = (i: number, j: number): string => {
164+
return i === j ? `${nums[i]}` : `${nums[i]}->${nums[j]}`;
165+
};
166+
const n = nums.length;
167+
const ans: string[] = [];
168+
for (let i = 0, j = 0; i < n; i = j + 1) {
169+
j = i;
170+
while (j + 1 < n && nums[j + 1] === nums[j] + 1) {
171+
++j;
172+
}
173+
ans.push(f(i, j));
174+
}
175+
return ans;
176+
}
177+
```
178+
159179
### **C#**
160180

161181
```cs

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,22 @@
5353

5454
## Solutions
5555

56+
**Approach 1: Two Pointers**
57+
58+
We can use two pointers $i$ and $j$ to find the left and right endpoints of each interval.
59+
60+
Traverse the array, when $j + 1 < n$ and $nums[j + 1] = nums[j] + 1$, move $j$ to the right, otherwise the interval $[i, j]$ has been found, add it to the answer, then move $i$ to the position of $j + 1$, and continue to find the next interval.
61+
62+
Time complexity $O(n)$, where $n$ is the length of the array. Space complexity $O(1)$.
63+
5664
<!-- tabs:start -->
5765

5866
### **Python3**
5967

6068
```python
6169
class Solution:
6270
def summaryRanges(self, nums: List[int]) -> List[str]:
63-
def f(i, j):
71+
def f(i: int, j: int) -> str:
6472
return str(nums[i]) if i == j else f'{nums[i]}->{nums[j]}'
6573

6674
i = 0
@@ -140,6 +148,26 @@ func summaryRanges(nums []int) (ans []string) {
140148
}
141149
```
142150

151+
### **TypeScript**
152+
153+
```ts
154+
function summaryRanges(nums: number[]): string[] {
155+
const f = (i: number, j: number): string => {
156+
return i === j ? `${nums[i]}` : `${nums[i]}->${nums[j]}`;
157+
};
158+
const n = nums.length;
159+
const ans: string[] = [];
160+
for (let i = 0, j = 0; i < n; i = j + 1) {
161+
j = i;
162+
while (j + 1 < n && nums[j + 1] === nums[j] + 1) {
163+
++j;
164+
}
165+
ans.push(f(i, j));
166+
}
167+
return ans;
168+
}
169+
```
170+
143171
### **C#**
144172

145173
```cs

solution/0200-0299/0228.Summary Ranges/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution:
22
def summaryRanges(self, nums: List[int]) -> List[str]:
3-
def f(i, j):
3+
def f(i: int, j: int) -> str:
44
return str(nums[i]) if i == j else f'{nums[i]}->{nums[j]}'
55

66
i = 0
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function summaryRanges(nums: number[]): string[] {
2+
const f = (i: number, j: number): string => {
3+
return i === j ? `${nums[i]}` : `${nums[i]}->${nums[j]}`;
4+
};
5+
const n = nums.length;
6+
const ans: string[] = [];
7+
for (let i = 0, j = 0; i < n; i = j + 1) {
8+
j = i;
9+
while (j + 1 < n && nums[j + 1] === nums[j] + 1) {
10+
++j;
11+
}
12+
ans.push(f(i, j));
13+
}
14+
return ans;
15+
}

0 commit comments

Comments
 (0)