39 lines
1.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* https://leetcode.cn/problems/summary-ranges/?envType=study-plan-v2&envId=top-interview-150
* @param {number[]} nums
* @return {string[]}
*/
const summaryRanges = function (nums) {
};
/*
思路定义两个指针一个指针表示区间的开头一个表示区间的结尾用left和right表示right扫描连续区间如果发现不
再连续就把left和right表示的连续区间加入到结果集
*/
function f1(nums) {
const result = [];
let left = 0;
while (left < nums.length) {
let right = left;
// 扫描连续区间
while (right + 1 < nums.length && nums[right + 1] === nums[right] + 1) {
right++;
}
// 查看left和right是否相等如果相等则这个区间只有一个数否则是一个完整的区间需要用left->right表示
if (left === right) {
result.push(`${nums[left]}`);
} else {
result.push(`${nums[left]}->${nums[right]}`);
}
// 区间统计完毕,统计下一个区间
left = right + 1;
}
return result;
}