- 浏览: 183394 次
- 性别:
- 来自: 济南
文章分类
最新评论
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"].
从第一个元素开始,每次保留开始的元素的值,依次扫描后面的元素,如果有连续的就一直往后查找,直到遇到不连续的,然后记录这段范围。直到遍历完所有的元素。代码如下:
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; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 929Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 672Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 782You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
python python_leetcode题解之228_Summary_Ranges.py
24. **Summary Ranges**:将连续的数字范围合并。这可以通过迭代数组并跟踪当前范围来实现。 25. **Reverse Linked List**:反转链表。递归或迭代方法都可以实现。 26. **Binary Tree Paths**:找到二叉树的所有根...
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 ...
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
在该问题中,你需要处理一个名为`script.js`的文件,这个文件包含了一个对象数组,每个对象都有如下的属性:`name`、`ranges`、`direction`、`year`、`living`和`link`。你的任务是创建一个新的`index.js`文件,编写...
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 ...
15. **事件(Event Summary)**:包括 "play","pause","error" 等事件,允许响应用户交互和播放状态变化。 16. **安全与隐私考虑(Security and Privacy Considerations)**:使用 `<video>` 元素时,必须注意跨域...
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...
* --ignore-ranges=0xPP-0xQQ[,0xRR-0xSS]:忽略指定的地址范围 * --malloc-fill=<hexnumber>:malloc 区域的填充值 * --free-fill=<hexnumber>:free 区域的填充值 * --alignment=<number>:最小分配对齐 四、打印...
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 ...
- 允许的字符范围(Ranges of characters allowed)、最初不允许的字符范围(Ranges of characters disallowed initially)提供了标识符中可以使用的字符范围。 附录F. 跨引用(Cross references) - 索引(Index)...
In summary, CCDs are sophisticated imaging devices that have revolutionized digital photography and scientific imaging. Understanding their structure, operation, and characteristics is essential for ...
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 ...
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 <br>SECTION I SOFTWARE QUALITY IN PERSPECTIVE . . . . . . . . . . . . . . . 1 1 Quality Assurance Framework. . . . . . . ........
1.2 Hardware Summary.....................................................................................................................7 1.3 IC nomenclature.............................................