(easy)LeetCode 228.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"].
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
代码如下:
public class Solution {
public List<String> summaryRanges(int[] nums) {
List<String>list=new ArrayList<String>();
int len=nums.length;
if(len==0) return list;
int begin=nums[0];
int end=nums[0];
for(int i=0;i<len;i++){
begin=nums[i];
while(i+1<len && nums[i+1]-nums[i]==1)
i++;
end=nums[i];
String s=null;
if(begin!=end)
s=""+begin+"->"+end;
else
s=""+begin;
list.add(s); }
return list;
}
}
运行结果:
(easy)LeetCode 228.Summary Ranges的更多相关文章
- [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 ...
- LeetCode 228. Summary Ranges (总结区间)
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- 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 ...
- Java [Leetcode 228]Summary Ranges
题目描述: Given a sorted integer array without duplicates, return the summary of its ranges. For example ...
- [leetcode]228. Summary Ranges区间统计
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- 【LeetCode】228. Summary Ranges 解题报告(Python)
[LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...
- leetcode-【中等题】228. Summary Ranges
题目: 228. Summary Ranges Given a sorted integer array without duplicates, return the summary of its r ...
随机推荐
- python exec
exec官方声明This statement supports dynamic execution of Python code. exec语句用来执行储存在字符串或文件中的python语句.exec ...
- MySQL Show命令的使用
show tables或show tables from database_name; 解释:显示当前数据库中所有表的名称 show databases; 解释:显示mysql中所有数据库的名称 sh ...
- JAVA利用ODBC读取DBF,可以解决javadbf.jar对DBF部分中文乱码和错行等杂症
因为需要用ODBC所以需要是windows平台 1.安装VFP for ODBC驱动(系统自带的dBase驱动不行哈) 下载:VFPODBC.msi 安装好后ODBC dsn会多出来一个东东,如下图1 ...
- WCF学习心得----(五)生成客户端
WCF学习心得----(五)生成客户端 1. 通过Svcutil.exe工具直接生成客户端 1.1 将服务承载于IIS上 1.1.1 在IIS中新建网站,所示效果如下图: 1.1.2 ...
- iptables基础信息介绍
在linux系统下,网络安全,除了有SElinux,另外就是iptables防火墙了,这个是用的最多也是功能非常强大的一个工具,今天就对其简单的架构上技术进行概要描述.让自己后续能够逻辑清晰的处理云环 ...
- Jar mismatch! Fix your dependencies的问题
在开发Android项目的时候,有时需要引用多个项目作为library.在引用项目的时候,有时会出现“Jar mismatch! Fix your dependencies”错误. 这是因为两个项目的 ...
- 利用百度地图API,获取经纬度坐标
利用百度地图API,获取经纬度坐标 代码很简单,但在网上没找到现成的获取地图经纬度的页面. 就是想,给当前页面传递一个经纬度,自动定位到此经纬度.然后可以重新选择,选择完返回经纬度. 效果如下: 源代 ...
- 06 Linux下Shell介绍
一.概述 每个人在成功登陆Linux后,系统会出现不同的提示符号,例如$,~,#等,然后你就可以开始输入需要的命令.若命令正确,系统就会依据命令的要求来执行,直到注销系统为止,在登陆到注销期间,输入的 ...
- 战胜忧虑<4>——让平均概率来替你分忧
让平均概率来替你分忧. 我们可以根据事情发生的平均率来评估我们的忧虑究竟值不值,如此一来,我想你和我应该可以去除99%的忧虑. 故事 我从小生长在密苏里州的一个农场,有一天,正帮妈妈采摘樱桃的时候,我 ...
- 使用 HTML5 Shiv 让 IE 支持 HTML5
HTML5 Shiv 使用 html5.js 必须在页面head元素内调用(因为 IE 必须在元素解析前知道这个元素,所以这个 JS 文件不能在页面底部调用.) 作者已经把js文件放在Google c ...