`

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 List<String> summaryRanges(int[] nums) {
        List<String> list = new ArrayList<String>();
        if(nums == null || nums.length == 0) return list;
        for(int i = 0; i < nums.length; i++) {
            int num = nums[i];
            while(i < nums.length - 1 && nums[i] + 1 == nums[i + 1]) {
                i ++;
            }
            if(num != nums[i]) {
                list.add(num + "->" + nums[i]);
            } else {
                list.add(String.valueOf(num));
            }
        }
        return list;
    }
}
分享到:
评论

相关推荐

    python-leetcode题解之228-Summary-Ranges.py

    python python_leetcode题解之228_Summary_Ranges.py

    Leetcode部分试题解析

    24. **Summary Ranges**:将连续的数字范围合并。这可以通过迭代数组并跟踪当前范围来实现。 25. **Reverse Linked List**:反转链表。递归或迭代方法都可以实现。 26. **Binary Tree Paths**:找到二叉树的所有根...

    LeetCode最全代码

    I'll keep updating for full summary and better solutions. Stay tuned for updates. (Notes: "馃摉" means you need to subscribe to [LeetCode premium membership](https://leetcode.com/subscribe/) for the ...

    Practical C++ Programming C++编程实践

    Preface Part I. The Basics 1. What Is C++? A Brief History of C++ C++ Organization How to Learn C++ 2.... Ranges C. Operator Precedence Rules D. Computing Sine Using a Power Series E. Resources Index

    Final_labsheet 3.pdf

    在该问题中,你需要处理一个名为`script.js`的文件,这个文件包含了一个对象数组,每个对象都有如下的属性:`name`、`ranges`、`direction`、`year`、`living`和`link`。你的任务是创建一个新的`index.js`文件,编写...

    tw8816datasheet

    This information is crucial for proper system design and ensures reliable operation within specified ranges. #### TW8816 Register Summary The register summary provides a comprehensive overview of ...

    Html5video.pdf

    15. **事件(Event Summary)**:包括 "play","pause","error" 等事件,允许响应用户交互和播放状态变化。 16. **安全与隐私考虑(Security and Privacy Considerations)**:使用 `&lt;video&gt;` 元素时,必须注意跨域...

    Programming Excel With Vba And .net.chm

    Working with Worksheets and Ranges Section 9.1. Work with Worksheet Objects Section 9.2. Worksheets and Worksheet Members Section 9.3. Sheets Members Section 9.4. Work with Outlines Section...

    Linux下使用valgrind做内存泄露检测及绘制函数调用图

    * --ignore-ranges=0xPP-0xQQ[,0xRR-0xSS]:忽略指定的地址范围 * --malloc-fill=&lt;hexnumber&gt;:malloc 区域的填充值 * --free-fill=&lt;hexnumber&gt;:free 区域的填充值 * --alignment=&lt;number&gt;:最小分配对齐 四、打印...

    Google C++ International Standard.pdf

    Contents Contents ii List of Tables x List of Figures xiv 1 Scope 1 2 Normative references 2 3 Terms and definitions 3 4 General principles 7 4.1 Implementation compliance . ....4.2 Structure of this ...

    C++ 标准 ISO 14882-2011

    - 允许的字符范围(Ranges of characters allowed)、最初不允许的字符范围(Ranges of characters disallowed initially)提供了标识符中可以使用的字符范围。 附录F. 跨引用(Cross references) - 索引(Index)...

    CCD_descript

    In summary, CCDs are sophisticated imaging devices that have revolutionized digital photography and scientific imaging. Understanding their structure, operation, and characteristics is essential for ...

    Build Report Tool 3.0.19.rar

    The size of a cluster can vary, but typical ranges are from 512 bytes to 32K or more. For example, on my C:\ drive, the allocation unit is 4096 bytes. This means that Windows will allocate 4096 bytes ...

    python3.6.5参考手册 chm

    Summary – Release highlights New Features PEP 498: Formatted string literals PEP 526: Syntax for variable annotations PEP 515: Underscores in Numeric Literals PEP 525: Asynchronous Generators ...

    Software Testing and Continuous Quality Improvement

    Software Testing and Continuous Quality Improvement &lt;br&gt;SECTION I SOFTWARE QUALITY IN PERSPECTIVE . . . . . . . . . . . . . . . 1 1 Quality Assurance Framework. . . . . . . ........

    MA245x_DB-R_v1.02_Spetek.pdf

    1.2 Hardware Summary.....................................................................................................................7 1.3 IC nomenclature.............................................

Global site tag (gtag.js) - Google Analytics