LeetCode-228.Summary Ranges

原创 2016年06月01日 16:30:22

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 IList<string> SummaryRanges(int[] nums) 
    {
        int start, precur, cur = -1, n = nums.Length, left = 0;
        IList<string> res = new List<string>();
        while (cur != n - 1)
        {
            start = cur + 1;
            precur = n - 1;
            cur = precur;
            while (nums[cur] - nums[start] != cur - start)
            {
                precur = cur;
                cur = (start + cur) / 2;
            }
            if (cur == n - 1)
            {
                if (cur == start)
                    res.Add(nums[cur].ToString());
                else
                    res.Add(nums[start] + "->" + nums[cur]);
                break;
            }
            while (left + 1 != precur)
            {
                if (nums[cur] - nums[start] == cur - start)
                {
                    left = cur;
                    cur = (precur + cur) / 2;
                }
                else
                {
                    precur = cur;
                    cur = (left + cur) / 2;
                }
            }
            if (cur == start)
                res.Add(nums[cur].ToString());
            else
                res.Add(nums[start] + "->" + nums[cur]);
        }
        return res;
    }


不用二分法,直接搜索

public IList<string> SummaryRanges(int[] nums) 
    {
        int start = 0, end = 0, n = nums.Length;
        IList<string> res = new List<string>();
        while (end < n)
        {
            if (end + 1 < n && nums[end + 1] - nums[end] == 1)
                end++;
            else
            {
                if (start == end)
                    res.Add(nums[end].ToString());
                else
                    res.Add(nums[start] + "->" + nums[end]);
                start = end = end + 1;
            }
        }
        return res;
    }


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【LeetCode OJ 228】Summary Ranges

题目链接:https://leetcode.com/problems/summary-ranges/ 题目: Given a sorted integer array without dupl...

Leetcode_228_Summary Ranges

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/46762039 Given a sorted in...

Leetcode——228. Summary Ranges

题目Given a sorted integer array without duplicates, return the summary of its ranges.For example, giv...

LeetCode228——Summary Ranges

leetcode刷题日记——Summary Ranges 1.题目内容: Given a sorted integer array without duplicates, return the su...

228. Summary Ranges

题意: Given a sorted integer array without duplicates, return the summary of its ranges.For example, g...

228. Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges. For example, giv...
  • iFuMI
  • iFuMI
  • 2016-03-22 17:13
  • 181

228. Summary Ranges\59. Spiral Matrix II\64. Minimum Path Sum

Summary Ranges 题目描述 代码实现 Spiral Matrix II 题目描述 代码实现 Minimum Path Sum 题目描述 代码实现228. Summary Ranges题目描...

leetcode--Summary Ranges

题目: Given a sorted integer array without duplicates, return the summary of its ranges. For example...

Summary Ranges

题目名称 Summary Ranges—LeetCode链接描述 Given a sorted integer array without duplicates, return the summa...

Summary Ranges 问题解答

前段时间刷了几道leetcode上面的题,觉得比较有意思,但由于没有记录,所以过一段时间就忘记了。计划在这里对每刷的一道题都进行总结,本次的题如下:题目 Given a sorted integer...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)