[LeetCode] 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"].解题思路略实现代码C++:// Runtim

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

解题思路

实现代码

C++:

// Runtime: 0 ms
class Solution {
public:
    vector<string> summaryRanges(vector<int>& nums) {
        vector<string> results;
        int start = 0;
        for (int i = 1; i <= nums.size(); i++)
        {
            if (i == nums.size() || nums[i - 1] + 1 < nums[i])
            {
                string temp = num2str(nums[start]);
                if (start + 1 < i)
                {
                    temp += "->";
                    temp += num2str(nums[i - 1]);
                }
                results.push_back(temp);
                start = i;
            }
        }

        return results;
    }

    string num2str(long long n)
    {
        if (n == 0)
        {
            return "0";
        }
        string temp = "";
        bool flag = false;
        if (n < 0)
        {
            flag = true;
            n = -n;
        }

        while (n)
        {
            temp = (char)(n % 10 + '0') + temp;
            n /= 10;
        }
        if(flag)
        {
            temp = '-' + temp;
        }

        return temp;
    }
};

Java:

// Runtime: 268 ms
public class Solution {
    public List<String> summaryRanges(int[] nums) {
        List<String> res = new ArrayList<String>();
        int begin = 0;
        for (int i = 1; i <= nums.length; i++){
            if (i == nums.length || nums[i] > nums[i-1] + 1){
                StringBuilder sb = new StringBuilder();
                sb.append(nums[begin]);
                if (i > begin + 1){
                    sb.append("->");
                    sb.append(nums[i-1]);
                }
                res.add(sb.toString());
                begin = i;
            }
        }

        return res;
    }
}

Python:

# Runtime: 36 ms
class Solution:
    # @param {integer[]} nums
    # @return {string[]}
    def summaryRanges(self, nums):
        x, size = 0, len(nums)
        ans = []
        while x < size:
            c, r = x, str(nums[x])
            while (x + 1) < size and nums[x+1] == nums[x] + 1:
                x += 1
            if x > c:
                r += '->' + str(nums[x])
            ans.append(r)
            x += 1
        return ans
目录
相关文章
LeetCode 228. Summary Ranges
给定一个无重复元素的有序整数数组,返回数组区间范围的汇总。
73 0
LeetCode 228. Summary Ranges
|
索引 Java
LeetCode 228 Summary Ranges(值域)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50611045 翻译 给定一个无重复的已排序整型数组,返回其中范围的集合。
731 0
[LeetCode] Summary Ranges
This problem is similar to Missing Ranges and easier than that one. The idea is to use two pointers to find the beginning and end of a range and then push it into the result.
891 0
[LeetCode] Missing Ranges
Problem Description: Given a sorted integer array where the range of elements are [lower, upper] inclusive, return its missing ranges.
801 0
|
2月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
3月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
57 6
|
3月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
114 2