[LeetCode] 228. Summary Ranges 总结区间
2024-09-20 09:07:58
Given a sorted integer array without duplicates, return the summary of its ranges.
Example 1:
Input: [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Explanation: 0,1,2 form a continuous range; 4,5 form a continuous range.
Example 2:
Input: [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
Explanation: 2,3,4 form a continuous range; 8,9 form a continuous range.
Java:
class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> list=new ArrayList();
if(nums.length==1){
list.add(nums[0]+"");
return list;
}
for(int i=0;i<nums.length;i++){
int a=nums[i];
while(i+1<nums.length&&(nums[i+1]-nums[i])==1){
i++;
}
if(a!=nums[i]){
list.add(a+"->"+nums[i]);
}else{
list.add(a+"");
}
}
return list;
}
}
Python:
class Solution:
# @param {integer[]} nums
# @return {string[]}
def summaryRanges(self, nums):
ranges = []
if not nums:
return ranges start, end = nums[0], nums[0]
for i in xrange(1, len(nums) + 1):
if i < len(nums) and nums[i] == end + 1:
end = nums[i]
else:
interval = str(start)
if start != end:
interval += "->" + str(end)
ranges.append(interval)
if i < len(nums):
start = end = nums[i] return ranges
Python:
class Solution(object):
def summaryRanges(self, nums):
"""
:type nums: List[int]
:rtype: List[str]
""" ranges = []
for n in nums:
if not ranges or n > ranges[-1][-1] + 1:
ranges += [],
ranges[-1][1:] = n, return ['->'.join(map(str, r)) for r in ranges]
Python:
class Solution(object):
def summaryRanges(self, nums):
"""
:type nums: List[int]
:rtype: List[str]
"""
ranges, r = [], []
for n in nums:
if n-1 not in r:
r = []
ranges += r,
r[1:] = n,
print r, ranges
return ['->'.join(map(str, r)) for r in ranges]
C++:
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string> res;
int i = 0, n = nums.size();
while (i < n) {
int j = 1;
while (i + j < n && nums[i + j] - nums[i] == j) ++j;
res.push_back(j <= 1 ? to_string(nums[i]) : to_string(nums[i]) + "->" + to_string(nums[i + j - 1]));
i += j;
}
return res;
}
};
C++:
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
const int size_n = nums.size();
vector<string> res;
if ( 0 == size_n) return res;
for (int i = 0; i < size_n;) {
int start = i, end = i;
while (end + 1 < size_n && nums[end+1] == nums[end] + 1) end++;
if (end > start) res.push_back(to_string(nums[start]) + "->" + to_string(nums[end]));
else res.push_back(to_string(nums[start]));
i = end+1;
}
return res;
}
};
类似题目:
[LeetCode] 163. Missing Ranges 缺失区间
All LeetCode Questions List 题目汇总
最新文章
- Redis实战阅读笔记——第二章
- BZOJ4590: [Shoi2015]自动刷题机
- linux 下 jdk+tomcat+mysql 的 jsp 环境搭建
- 本地虚拟机中匿名ftp上传文件失败的问题
- Python图片与其矩阵数据互相转换
- java正则表达式应用--验证字符串是否为数字(转载)
- c语言,strcpy
- Quartz1.8.5例子(四)
- java 项目request.getParameter(";";)接收不到值
- kafka学习(二)-zookeeper集群搭建
- 浏览器本地储存方式有哪些?cookie、localStorage、sessionStorage
- UVALive3882-And Then There Was One-约瑟夫问题-递推
- CDOJ 1965 连通域统计【DFS】
- python 爬虫数据存入csv格式方法
- Tree - Decision Tree with sklearn source code
- PL/SQL之DBMS_SQL程序包使用(1)(学习笔记)
- T Fiddler 教程 _转
- 【游记】CTSC&;APIO2017
- http测试工具ab
- bzoj 3192 删除物品