Leetcode-228 Summary Ranges
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode-228 Summary Ranges相关的知识,希望对你有一定的参考价值。
Given a sorted integer array without duplicates, return the summary of its ranges.
For example, given [0,1,2,4,5,7]
, return ["0->2","4->5","7"].
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
题解:两个指针 start, end. 如果nums[end+1] = nums[end]+1, 就移动end指针, 否则, 插入字符串nums[start]->nums[end].
/** * @param {number[]} nums * @return {string[]} */ var summaryRanges = function(nums) { var str=new Array(); var start,end,s,e; var len = nums.length; if(len<1 || nums===null){ return str; } str[0]=""; start=0; var k=0; end=0; while(end<len){ str[k]=""; if(end+1<len && nums[end+1]===nums[end]+1){ end++; } else{ if(start === end){ str[k++]+=nums[start]; } else{ str[k++]+=nums[start]+"->"+nums[end]; } end++; start=end; } } return str; };
以上是关于Leetcode-228 Summary Ranges的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode --- 228. Summary Ranges 解题报告