-
-
Notifications
You must be signed in to change notification settings - Fork 241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
["0->2", "4->5", "7", "13", "15->16"] #103
Comments
给定一个升序整数数组[0,1,2,4,5,7,13,15,16],找出其中连续出现的数字区间如下: const arr = [0, 1, 2, 4, 5, 7, 13, 15, 16]
function getArr(arr) {
let j, ans = [], str = '';
for (let i = 0; i < arr.length; i++) {
j = i;
if (arr[i] + 1 === arr[i + 1]) {
while (arr[j] + 1 === arr[j + 1]) {
str = '->' + arr[j + 1];
j++;
}
str = arr[i] + str;
ans.push(str)
i = j
} else {
ans.push(arr[i].toString());
}
}
return ans;
}
console.log(getArr(arr)) |
// 双指针处理临界值问题
function format(arr){
const len = arr.length;
const res = [];
let pre = 0, cur = 0;
for(cur = 0; cur < len; ++cur){
if(cur === pre || arr[cur] === arr[cur - 1] + 1){continue};
if(cur - pre === 1){
res.push(Number(arr[pre]).toString());
}else{
res.push(Number(arr[pre]).toString() + "->" + Number(arr[cur - 1]).toString())
}
pre = cur;
}
// 处理临界值
if(pre === cur){
res.push(Number(arr[pre]).toString());
}else{
res.push(Number(arr[pre]).toString() + "->" + Number(arr[len - 1]).toString())
}
return res;
} |
function summaryRanges(arr) {
if (arr.length < 1) {
return [ ];
}
const ans = [];
let slow = 0;
let fast = 1;
while (fast !== arr.length + 1) {
if (arr[fast] - arr[fast - 1] !== 1 || arr.length === fast) {
if (arr[slow] === arr[fast - 1]) {
ans.push(arr[slow] + '');
} else {
ans.push(arr[slow] + '->' + arr[fast - 1]);
}
slow = fast;
}
fast++;
}
return ans;
} |
function convey(arr) {
let res = []
let i = 0
while(i < arr.length) {
let j = i
while(j < arr.length-1 && arr[j] === arr[j+1]-1) {
j ++
}
if(j === i) {
res.push(arr[i].toString())
i ++
} else {
res.push(`${arr[i]}->${arr[j]}`)
i = j+1
}
}
return res
} |
function findConsecutiveRanges(arr) {
var result = [];
for (var i = 0; i < arr.length; i++) {
var start = arr[i];
while (arr[i] + 1 === arr[i + 1]) { // 检查当前数字和下一个数字是否连续
i++;
}
var end = arr[i];
if (start !== end) {
result.push(start + "->" + end); // 如果序列有多个数字,则添加范围
} else {
result.push(start.toString()); // 如果序列只有一个数字,则直接添加该数字
}
}
return result;
}
console.log(findConsecutiveRanges([0,1,2,4,5,7,13,15,16])); |
构造一个二维数组,遍历2次即可
|
只需要遍历一次数组即可,时间复杂度为O(n)
|
const arr = [0, 1, 2, 4, 5, 7, 13, 15, 16] function getArr(arr) { |
function getArr(arr) {
let left = 0;
let res = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] + 1 === arr[i + 1]) {
continue;
} else {
if (left === i) {
res.push(String(arr[i]));
} else {
res.push(`${arr[left]} -> ${arr[i]}`);
}
left = i + 1;
}
}
return res;
} |
// 连续出现的数字区间
// 给定一个升序整数数组[0,1,2,4,5,7,13,15,16],找出其中连续出现的数字区间如下:
// ["0->2","4->5","7","13","15->16"]
let continuousNumbers = (nums) => {
let left = 0;
let right = 0;
let result = [];
let path = [];
while (left < nums.length) {
if (left > 0 && nums[left] - nums[left - 1] != 1) {
result.push(`${path[0]}->${path[path.length - 1]}`);
path = [];
right = left;
}
path.push(nums[left]);
left++;
}
result.push(`${path[0]}->${path[path.length - 1]}`);
return result;
};
console.log(continuousNumbers([0, 1, 2, 4, 5, 7, 13, 15, 16])); |
|
No description provided.
The text was updated successfully, but these errors were encountered: