关闭

【LeetCode OJ 228】Summary Ranges

标签: JavaLeetCode
366人阅读 评论(0) 收藏 举报
分类:

题目链接:https://leetcode.com/problems/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"].

示例代码:

public class Solution
{
	public static void main(String[] args)
	{
		
		int nums[]=new int[]{1,2,4,5,7};
		List<String> rList=new Solution().summaryRanges(nums);
		for(String str:rList)
		{
			System.out.println(str);
		}
	}
	public List<String> summaryRanges(int[] nums)
	{
        List<String> result=new ArrayList<String>();
        int startLine=0; //起始位置
        int endLine=0;	//结束位置
        int num=0;		//扫描计数
        for(int i=0;i<nums.length;i++)
        {
        	int j=i;
        	int k=j+1;
        	if(k<nums.length)
        	{
        		//后面的数>当前数+1,且只扫描了一个数,就将该数放入result中
        		if(nums[k]>(nums[j]+1)&&num==0)
        		{
        			result.add(String.valueOf(nums[j]));
        			startLine=k;
        			endLine=k;
        			num=0;
        		}
        		//后面的数>当前数+1,扫描了(endLine-start+1)个数,就将nums[startLine])+"->"+String.valueOf(nums[endLine])放入result中
        		else if(nums[k]>(nums[j]+1)&&num>0)
        		{
        			result.add(String.valueOf(nums[startLine])+"->"+String.valueOf(nums[endLine]));
        			startLine=k;
        			endLine=k;
        			num=0;
        		}
        		//后面的数=当前数+1,移动endLine,num++即可
        		else if(nums[k]-nums[j]==1)
        		{
        			endLine=k;
        			num++;
        		}
        	}
        	//扫描最后一个数
        	else
        	{
        		if(num==0)
        			result.add(String.valueOf(nums[k-1]));
        		else
        		{
        			result.add(String.valueOf(nums[startLine])+"->"+String.valueOf(nums[endLine]));
        			num=0;
        		}
        	}
        	
        }
        return result;
    }
}


0
0
查看评论

leetcode 228: Summary Ranges

leetcode 228: Summary Ranges python, java, c++
  • xudli
  • xudli
  • 2015-06-26 06:22
  • 10146

leetCode #228 Summary Ranges

题目:输出一组排好序的int数组的连续范围, 分析:找出连续的地方分段输出即可。 答案:class Solution { public: string formatRange(int startn,int endn){ char buffer[32]; i...
  • MarStarck
  • MarStarck
  • 2015-08-11 12:02
  • 158

[leetcode 228] Summary Ranges

Question: Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return...
  • Xiaohei00000
  • Xiaohei00000
  • 2015-12-12 17:48
  • 151

leetcode 228:Summary Ranges

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]...
  • tuobadon
  • tuobadon
  • 2015-07-04 11:29
  • 148

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","...
  • sunao2002002
  • sunao2002002
  • 2015-06-26 18:32
  • 3861

[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",&quo...
  • sbitswc
  • sbitswc
  • 2015-09-24 04:07
  • 361

Summary Ranges - LeetCode 228

题目描述: 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&quo...
  • bu_min
  • bu_min
  • 2015-06-28 09:11
  • 268

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","...
  • u010129448
  • u010129448
  • 2015-08-01 21:48
  • 266

【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”].p...
  • yuanzexi
  • yuanzexi
  • 2015-12-01 17:39
  • 115

[Leetcode]#228 Summary Ranges

//#228 Summary Ranges //0ms 100% class Solution { public: void int_string(string& s, int num) { if(num < 0) { ...
  • jerseyma
  • jerseyma
  • 2015-09-02 08:06
  • 154
    个人资料
    • 访问:91868次
    • 积分:1508
    • 等级:
    • 排名:千里之外
    • 原创:79篇
    • 转载:3篇
    • 译文:1篇
    • 评论:6条
    博客专栏
    最新评论