`

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;
    }
}
分享到:
评论

相关推荐

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

    Beginning Python (2005).pdf

    Try It Out: Printing a Summary of Your IMAP Mailbox 329 IMAP’s Unique Message IDs 330 Try It Out: Fetching a Message by Unique ID 330 Secure POP3 and IMAP 331 Webmail Applications Are Not E-mail ...

    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

    Barcodes.with.iOS.Bringing.together.the.digital.and.physical.worlds

    Summary Barcodes with iOS is the first and only book that comprehensively addresses barcode technology for the iOS developer. It offers an introduction to commonly used formats, such as ISBN and UPC ...

    Introduction to Communication Electronic Warfare Systems.pdf

    1.4.4 ES Summary 9 1.5 Electronic Attack 10 1.5.1 EA Summary 11 1.6 Typical EW System Configuration 11 1.6.1 System Control 11 1.6.2 Antennas 12 1.6.3 Signal Distribution 13 1.6.4 Search Receiver 13 ...

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

    C++ 标准 ISO 14882-2011

    Contents Contents iii List of Tables xi List of Figures xv 1 General 1 1.1 Scope . . . . ....1.2 Normative references ....1.3 Terms and definitions ....1.4 Implementation compliance ....1.5 Structure of this ...

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

    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