LeetCode——Summary Ranges
Description:
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.length == 0)
return list;
if(nums.length == 1) {
list.add(nums[0]+"");
return list;
}
boolean flag = false;
StringBuilder ele = new StringBuilder();
for(int i=0; i<nums.length; i++) {
int start = 0, end = 0; if(!flag) {
start = nums[i];
flag = true;
ele.append(start+"");
} if(i<nums.length-1 && nums[i] != nums[i+1]-1) {
end = nums[i];
flag = false;
if(nums[i] != start) {
ele.append("->" + end);
}
} if(ele != null) {
if(i == nums.length-2 && nums[i] == nums[i+1]-1) {
end = nums[i+1];
ele.append("->" + end);
list.add(ele.toString());
ele.delete(0, ele.length());
break;
}
if(!flag || flag&&i==nums.length-1) {
list.add(ele.toString());
ele.delete(0, ele.length());
}
}
}
return list;
}
}
LeetCode——Summary Ranges的更多相关文章
- [LeetCode] Summary Ranges 总结区间
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- (leetcode)Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- 228. [LeetCode] Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- LeetCode Summary Ranges (统计有序数列范围)
题意:给出个有序不重复数列(可能负数),用缩写法记录这个数列. 思路:找每个范围的起始和结束即可. class Solution { public: vector<string> summ ...
- leetcode面试准备:Summary Ranges
1 题目 Given a sorted integer array without duplicates, return the summary of its ranges. For example, ...
- 【LeetCode】228. Summary Ranges 解题报告(Python)
[LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...
- 【刷题-LeetCode】228. Summary Ranges
Summary Ranges Given a sorted integer array without duplicates, return the summary of its ranges. Ex ...
- [LeetCode] Missing Ranges 缺失区间
Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing r ...
- leetcode-【中等题】228. Summary Ranges
题目: 228. Summary Ranges Given a sorted integer array without duplicates, return the summary of its r ...
随机推荐
- Spark Streaming中的操作函数讲解
Spark Streaming中的操作函数讲解 根据根据Spark官方文档中的描述,在Spark Streaming应用中,一个DStream对象可以调用多种操作,主要分为以下几类 Transform ...
- python 提示 AttributeError: module 'json' has no attribute 'dumps'
最近在学习python解析json的时候遇到问题, 提示 AttributeError: module 'json' has no attribute 'dumps' (模块没有dumps属性的意思) ...
- _STORAGE_WRITE_ERROR_
:( _STORAGE_WRITE_ERROR_:./Application/Runtime/Cache/User/788524be80db1959c132efbce25367a0.php 错误位置 ...
- OC基础--常用类的初步介绍与简单实用之集合类
集合类的异同点 一.NSArray\NSMutableArray *有序 *快速创建(只有不可变数组可以):@[obj1, obj2, obj3]; *快速访问元素:数组名[i] *只能存放对象 二. ...
- 关于c# 发射的调用并进行缓存
private static object CreateObject(string AssemblyPath, string classNamespace) { object objObject = ...
- TCP协议的问题
Server端接收到Client端信息后不会返回给Client端 // TCPEchoServer.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h&quo ...
- 【R】提升R代码运算效率的11个实用方法
低.有许多种方法可以提升你的代码运算效率,但或许你更想了解运算效率能得到多大的提升.本文将介绍几种适用于大数据领域的方法,包括简单的逻辑调整设计.并行处理和Rcpp的运用,利用这些方法你可以轻松地处理 ...
- C++:在堆上创建对象,还是在栈上?
这篇文章来自于一次讨论:http://www.devbean.net/2013/01/qt-study-road-2-model-view/#comment-17532.关于究竟是在堆上还是在栈上创建 ...
- 关于Cocos2d-x中背景音乐和音效的添加
1.首先引入头文件和命名空间 #include "SimpleAudioEngine.h" using namespace CocosDenshion; 2.在GameScene. ...
- MFC 无边框窗体实现用鼠标拖动窗体边缘实现窗体大小变化
无边框窗体如何实现用鼠标拖动窗体边缘实现窗体大小变动呢?下面介绍一种方法,通过以下几个步骤即可实现: 1.实现WM_NCHITTEST消息,实现四条边框的模拟 2.实现WM_NCLBUTTONDOWN ...