-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path228.summary-ranges.ts
More file actions
47 lines (44 loc) · 1.33 KB
/
Copy path228.summary-ranges.ts
File metadata and controls
47 lines (44 loc) · 1.33 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
/*
* @lc app=leetcode id=228 lang=typescript
*
* [228] Summary Ranges
*/
// @lc code=start
// function summaryRanges(nums: number[]): string[] {
// if (nums.length === 0) return []
// const arr = [nums[0] + ''];
// for(let i = 1; i < nums.length; i++) {
// const items = arr[arr.length - 1].split('->')
// const lastNum = Number(items[items.length - 1])
// // console.log(nums[i], lastNum)
// if (nums[i] === lastNum + 1) {
// arr[arr.length - 1] = `${items[0]}->${nums[i]}`
// } else {
// arr.push(nums[i] + '')
// }
// }
// return arr;
// };
function summaryRanges(nums: number[]): string[] {
if (nums.length === 0) return []
const arr = [nums[0] + ''];
for(let i = 0; i < nums.length; i++) {
// console.log(nums[i], nums[i + 1], nums[i + 1] === nums[i] + 1)
// 判斷與下一個值是否連續,是連續的話跳過
if (nums[i] + 1 === nums[i + 1]) {
continue
}
// 不連續的話有兩件要做的事
// 一種是要接在->後面
// 把下一個的值當作第一個放入
if (arr[arr.length - 1] !== nums[i] + '') {
arr[arr.length - 1] += '->' + nums[i]
}
if (nums[i + 1] !== undefined) {
arr.push(nums[i + 1] + '')
}
}
return arr;
};
// console.log(summaryRanges([0,2,3,6,8,9]))
// @lc code=end