如何实现堆栈数据结构到范围提取(codewars 任务)?
How to implement stack data structure to range extraction (codewars task)?
我正在为称为范围提取的代码战争套路而苦苦挣扎 - 它采用递增顺序的整数列表和 returns 范围格式(重叠的单独间隔)中格式正确的字符串。
示例解决方案:
([-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]);
// returns "-6,-3-1,3-5,7-11,14,15,17-20"
好吧,在我的解决方案中,我没有得到 -6,-3-1,3-5,7-11,14,15,17-20
,而是得到了最后一项 -6,1,5,11,15,20
。
如何改进我的解决方案?代码:
function solution(list){
let result=[]
for(let i=0;i<list.length;i++){
let e2=list[i]
let e1 = result[result.length-1]
if(e2-e1==1){
result[result.length-1]=e2
}
else{
result.push(e2 )
}
}
return result
}
console.log(solution([-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]))
您没有采取任何措施以范围格式写入连续整数。相反,您只是将先前的结果替换为范围内的最终项目,这恰好反映在您的解决方案中:
-6: this number has no "neighbors" so is fine
1: the final item in the first range
5: the final item in the second range
...
问题出在循环的内部逻辑上。
总而言之,您需要一段时间而不是 if,并且您需要追加而不是替换:
function solution(list){
let result=[]
for(let i=0;i<list.length;i++){
//write first value in range to result
result.push(list[i].toString())
//if this is the last entry, we are done
if(i === list.length - 1){
break
}
//initialize variables
let e1 = list[i]
let e2 = list[i+1]
let isRange = false
//run thorugh array while we get consecutive numbers
while(e2-e1===1 && i < list.length-1){
//modify the OUTER LOOP index variable.
//This means when we return to the beginning of hte for loop,
// we will be at the beginning of the next range
i++
e1 = list[i]
e2 = list[i+1]
isRange = true
}
//if there were any consecutive numbers
if(isRange){
//rewrite the last entry in result as a range
result[result.length-1]+="-" + list[i].toString()
}
}
return result.toString()
}
console.log(solution([-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]))
现在,您的外循环遍历整个数组一次。内循环将确保外循环跳过列表中出现在某个范围内的任何项目。最后,如果内部循环找到任何范围,它会将条目重写为正确的范围。
我正在为称为范围提取的代码战争套路而苦苦挣扎 - 它采用递增顺序的整数列表和 returns 范围格式(重叠的单独间隔)中格式正确的字符串。
示例解决方案:
([-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]);
// returns "-6,-3-1,3-5,7-11,14,15,17-20"
好吧,在我的解决方案中,我没有得到 -6,-3-1,3-5,7-11,14,15,17-20
,而是得到了最后一项 -6,1,5,11,15,20
。
如何改进我的解决方案?代码:
function solution(list){
let result=[]
for(let i=0;i<list.length;i++){
let e2=list[i]
let e1 = result[result.length-1]
if(e2-e1==1){
result[result.length-1]=e2
}
else{
result.push(e2 )
}
}
return result
}
console.log(solution([-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]))
您没有采取任何措施以范围格式写入连续整数。相反,您只是将先前的结果替换为范围内的最终项目,这恰好反映在您的解决方案中:
-6: this number has no "neighbors" so is fine
1: the final item in the first range
5: the final item in the second range
...
问题出在循环的内部逻辑上。
总而言之,您需要一段时间而不是 if,并且您需要追加而不是替换:
function solution(list){
let result=[]
for(let i=0;i<list.length;i++){
//write first value in range to result
result.push(list[i].toString())
//if this is the last entry, we are done
if(i === list.length - 1){
break
}
//initialize variables
let e1 = list[i]
let e2 = list[i+1]
let isRange = false
//run thorugh array while we get consecutive numbers
while(e2-e1===1 && i < list.length-1){
//modify the OUTER LOOP index variable.
//This means when we return to the beginning of hte for loop,
// we will be at the beginning of the next range
i++
e1 = list[i]
e2 = list[i+1]
isRange = true
}
//if there were any consecutive numbers
if(isRange){
//rewrite the last entry in result as a range
result[result.length-1]+="-" + list[i].toString()
}
}
return result.toString()
}
console.log(solution([-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]))
现在,您的外循环遍历整个数组一次。内循环将确保外循环跳过列表中出现在某个范围内的任何项目。最后,如果内部循环找到任何范围,它会将条目重写为正确的范围。