[LeetCode] 228. Summary Ranges 总结区间
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 题目汇总
[LeetCode] 228. Summary Ranges 总结区间的更多相关文章
- LeetCode 228. Summary Ranges (总结区间)
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- [leetcode]228. Summary Ranges区间统计
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- C#解leetcode 228. Summary Ranges Easy
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- Java for LeetCode 228 Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- LeetCode(228) Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- (easy)LeetCode 228.Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- Java [Leetcode 228]Summary Ranges
题目描述: Given a sorted integer array without duplicates, return the summary of its ranges. For example ...
- 228 Summary Ranges 汇总区间
给定一个无重复元素的有序整数数组,返回数组中区间范围的汇总. 示例 1: 输入: [0,1,2,4,5,7]输出: ["0->2","4->5",& ...
- [LeetCode] 163. Missing Ranges 缺失区间
Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...
随机推荐
- anyproxy学习1-windows平台安装和抓手机app上https请求
前言 做接口测试肯定离不开抓包,目前比较流行的抓包工具是fiddler和charles,相信并不陌生.这里介绍一个阿里公司研发的一个抓包神器,只需打开web页面,就能抓到手机app上的http和htt ...
- Alpha版本1之后的成绩汇总
作业要求 1.作业内容: 作业具体要求及评分标准的链接 2.评分细则 •给出开头和团队成员列表(10’) •给出发布地址以及安装手册(20’) •给出测试报告(40’) •给出项目情况总结(30’) ...
- mybatis从入门到精通
https://www.cnblogs.com/zwwhnly/p/11104020.html
- C++中的hash_map和map的区别
hash_map和map的区别在哪里?构造函数.hash_map需要hash函数,等于函数:map只需要比较函数(小于函数). 存储结构.hash_map采用hash表存储,map一般采用红黑树(RB ...
- maven的目录
maven目录主要分为: src/main/java:项目主体源代码目录 src/main/resources:项目主体源代码所需资源目录 src/test/java:测试代码目录(测试代码不会被打包 ...
- 海康相机开发(1) SDK安装和开发
1.1 安装包获取 从官网下载最新版本的MVS安装包,支持Windows xp.Windows 7.Windows 8.Windows 10的32和64位系统.安装过程默认即可. 官网下载链接:htt ...
- (尚021)Vue_eslint编码规范检查
1.eslint 1.1说明 1)ESLint是一个代码规范检查工具 2)它定义了很多特定的规则,一旦你的代码违背了某一规则,eslint会做出非常有用的提示 3)官网:http://eslint.o ...
- Noip 2017 题目整理
目录 2017Noip: 小凯的疑惑 时间复杂度 逛公园 奶酪 宝藏(50fen) 列队(QAQ不会,以后再研究吧) 2017Noip: 小凯的疑惑 题目描述 小凯手中有两种面值的金币,两种面值均为正 ...
- 查全率(Recall),查准率(Precision),灵敏性(Sensitivity),特异性(Specificity),F1,PR曲线,ROC,AUC的应用场景
之前介绍了这么多分类模型的性能评价指标(<分类模型的性能评价指标(Classification Model Performance Evaluation Metric)>),那么到底应该选 ...
- C Primer Plus--C预处理器和C库(1)
目录 预处理符号 明显常量 #define 编译程序之前,先由预处理器检查程序(因此称为预处理器).根据程序中使用的预处理器指令,预处理用符号缩略语所代表的内容替换程序中的缩略语. 预处理器可以根据你 ...