请求修复JavaScript Range extraction(范围提取)实现代码
JavaScript Range Extraction 问题修复
我需要解决JavaScript中的Range extraction(范围提取)问题:将递增有序整数列表转换为指定格式的字符串,规则为:单个整数直接保留,连续至少3个整数用'起始-结束'表示(包含两端)。示例:输入[-10, -9, -8, -6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]应返回"-10--8,-6,-3-1,3-5,7-11,14,15,17-20"。
我设计了三个函数实现:newRangeStart创建存储范围的数组并加入起始值;olSupp删除已处理元素;RangeArr构建范围并调用olSupp更新列表。但olSupp无法正确删除已使用的元素,导致代码功能异常,当前代码如下:
function solution(list){ // TODO: complete solution let ol = [...list]; let results = []; /*This adds a new array for a range by adding the first number of the range to an array (2D array) and stores it in the resuts array */ function newRangeStart(orderedlist,result){ result.push([orderedlist[0]]); return result; } /*This functions takes the ol and deletes elements that are found in the results so that the next time we run the newRangeStart function it creates an other array for another range with a different start number*/ function olSupp(orderedlist,result){ let toRemove = result.flat(); let newList = []; for (let i = 0; i < orderedlist.length; i++) { if(!toRemove.includes(orderedlist[i])){ newList.push(orderedlist[i]); } } orderedlist = [...newList]; return orderedlist; } /*Finally RangeArr function creates a range from the ol (ordered list) starting by the first element of the results array and then uses olSupp to delete the used numbers from the ol */ function RangeArr (orderedlist,result){ newRangeStart(orderedlist,result); let i = 0; while(orderedlist[i+1]- orderedlist[i] == 1 && orderedlist[i+2]- orderedlist[i+1]== 1) { result[i].push(orderedlist[i+1],orderedlist[i+2]); i = i+1; } olSupp(orderedlist,result); return result; } /*we execute the RangeArr function until ol becomes emepty and this will give us multiple arrays in the result array containing the elements of each range found in the ol */ //PS: i didnt put the code beacuse it causes an infinte loop using while RangeArr(ol,results); console.log(ol,results); } solution([-10, -9, -8, -6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]);
问题分析
你的代码存在几个核心问题:
olSupp函数中,内部的orderedlist是局部变量,赋值操作不会影响外部的ol变量,导致列表无法正确更新。RangeArr的循环逻辑错误,仅检测连续三个元素的情况,无法处理更长的连续序列,且元素添加方式会造成重复。- 缺少循环处理所有元素的逻辑,原计划的
while循环因逻辑问题会导致无限循环。
修复后的完整代码
function solution(list) { let ol = [...list]; let results = []; // 初始化一个新的范围数组,加入起始值 function newRangeStart(startNum) { return [startNum]; } // 从有序列表中移除已经处理过的范围元素 function olSupp(processedRange) { // 找到当前范围的结束位置,直接截断列表更高效 const endIndex = ol.indexOf(processedRange[processedRange.length - 1]); ol = ol.slice(endIndex + 1); } // 构建单个连续范围 function buildRange() { if (ol.length === 0) return null; const range = newRangeStart(ol[0]); let currentIndex = 1; // 遍历直到元素不连续 while (currentIndex < ol.length && ol[currentIndex] === ol[currentIndex - 1] + 1) { range.push(ol[currentIndex]); currentIndex++; } olSupp(range); return range; } // 循环处理所有元素 while (ol.length > 0) { results.push(buildRange()); } // 将结果数组转换为指定格式的字符串 return results.map(range => { if (range.length === 1) { return String(range[0]); } else if (range.length >= 3) { return `${range[0]}-${range[range.length - 1]}`; } else { // 长度为2的情况,直接用逗号分隔 return range.join(','); } }).join(','); } // 测试示例 console.log(solution([-10, -9, -8, -6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20])); // 输出:"-10--8,-6,-3-1,3-5,7-11,14,15,17-20"
关键修复点
- 重构
olSupp函数:不再低效遍历判断元素是否存在,而是通过找到范围最后一个元素的索引,直接截断列表,确保已处理元素被完全移除,同时提升效率。 - 重写范围构建逻辑:
buildRange函数完整遍历连续序列,收集所有连续元素,避免原逻辑的重复添加问题。 - 添加完整循环处理:使用
while循环直到列表为空,确保所有元素都被处理。 - 完善格式转换:针对范围长度1、2、≥3的不同情况,按照规则生成对应的字符串,最终拼接成结果。
内容的提问来源于stack exchange,提问作者Mehenna Boufassa
相关产品推荐
相关产品推荐

