-琥珀-的专栏

格言:抱怨没有用,一切靠自己!!

Summary Ranges(数组中连续的数字段)

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”].

CODE

public class Solution {
    public List<String> summaryRanges(int[] nums) {
         List<String> list = new ArrayList<String>();
         quicksort(nums, 0, nums.length-1);
         int i=0;
         while(i<nums.length){
             int index=i;
             String temp=nums[i]+"";
             while(i+1<nums.length&&nums[i+1]-nums[i]==1) i++;
             if(i==index){
                 list.add(temp);
             }
             else{
                 temp+="->"+nums[i];
                  list.add(temp);
             }
             i++;
         }
         return list;
    }
    //进行快速排序
    public int part(int[] nums,int low,int high){
        int temp=nums[low];
        while(low<high){
            while(low<high&&nums[high]>=temp) high--;
            nums[low]=nums[high];
            while(low<high&&nums[low]<=temp) low++;
            nums[high]=nums[low];
        }
        nums[low]=temp;
        return low;
    }

    public void quicksort(int[] nums,int low,int high){
        if(low<high){
            int pos= part(nums,low,high);
            quicksort(nums, low, pos-1);
            quicksort(nums, pos+1, high);
        }
    }
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010032372/article/details/46648485
文章标签: summary sorted
个人分类: LeetCode
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Summary Ranges(数组中连续的数字段)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭