问题描述:

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 static List<String> changes(int[] nums){
//当nums为空时,应该返回[]
  List<String> list = new ArrayList<String>();
  if(nums == null || nums.length == 0) //对于这种类型的返回值,都是预先设定一个list,然后判断后直接返回list,而不是单独返回null
    return list;
  int begin = nums[0];
  String range = "";
  for(int i = 1; i < nums.length ; i++){
    if(nums[i] - nums[i - 1] == 1)
      continue;
    else {
       if(begin == nums[i - 1]) //若一个数成为一个分段
          range = begin + "";
       else { //若连续几个数成为一个分段
          range = begin + "->" + nums[i - 1];
     }
   list.add(range); //加入一个分段到list中
       begin = nums[i]; //设定下一个分段的初始值
    }
}
  //对最后一个范围进行特殊处理
  if(begin == nums[nums.length - 1])
    range = begin + "";
  else
    range = begin + "->" + nums[nums.length - 1];
  list.add(range);
  return list;
}

summary ranges leetcode java的更多相关文章

  1. Summary Ranges —— LeetCode

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

  2. Summary Ranges leetcode

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

  3. leetcode面试准备:Summary Ranges

    1 题目 Given a sorted integer array without duplicates, return the summary of its ranges. For example, ...

  4. 【LeetCode】228. Summary Ranges 解题报告(Python)

    [LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...

  5. 【刷题-LeetCode】228. Summary Ranges

    Summary Ranges Given a sorted integer array without duplicates, return the summary of its ranges. Ex ...

  6. leetcode-【中等题】228. Summary Ranges

    题目: 228. Summary Ranges Given a sorted integer array without duplicates, return the summary of its r ...

  7. Missing Ranges & Summary Ranges

    Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive ...

  8. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  9. Java for LeetCode 228 Summary Ranges

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

随机推荐

  1. bzoj 1735: [Usaco2005 jan]Muddy Fields 泥泞的牧场 最小点覆盖

    链接 1735: [Usaco2005 jan]Muddy Fields 泥泞的牧场 思路 这就是个上一篇的稍微麻烦版(是变脸版,其实没麻烦) 用边长为1的模板覆盖地图上的没有长草的土地,不能覆盖草地 ...

  2. MySQL 5.7的原生JSON数据类型使用

    新增测试用表: CREATE TABLE lnmp ( `id` ) unsigned NOT NULL AUTO_INCREMENT, `category` JSON, `tags` JSON, P ...

  3. ZOJ 4027 Sequence Swapping(DP)题解

    题意:一串括号,每个括号代表一个值,当有相邻括号组成()时,可以交换他们两个并得到他们值的乘积,问你最大能得到多少 思路:DP题,注定想得掉头发. 显然一个左括号( 的最远交换距离由他右边的左括号的最 ...

  4. 【教程】Git在Eclipse中的安装和基本使用

    一.安装 点击 Help->Install New Software->add 安装地址为:http://download.eclipse.org/egit/updates/ 选择插件   ...

  5. MPU6050可以读取ID值,温度值和原始数据值为零问题解决

    MPU6050可以读取ID值是0x68,但是读取到的原始数据为零(下面虚拟示波器图中温度值是36.529是单位转换公式中的值被打印出来了,实际值也是零).经论坛搜寻,发现MPU6050出现问题的原因有 ...

  6. tomcat启动出现Preparing launch delegate,一直卡在100%

    本地启动项目时,Tomcat一直停留在, Starting Tomcat V8.0 Server at localhost   Preparing launch delegate...    百度可得 ...

  7. Python3简单爬虫抓取网页图片

    现在网上有很多python2写的爬虫抓取网页图片的实例,但不适用新手(新手都使用python3环境,不兼容python2), 所以我用Python3的语法写了一个简单抓取网页图片的实例,希望能够帮助到 ...

  8. 【Java】【问题】

    [log4j:WARN No appenders could be found for logger 解决方案] [解决] 我们在使用Log4j的时候,总是出现: log4j:WARN No appe ...

  9. python os.remove

    remove 只能删除文件,删除目录会报错 >>> import os >>> os.remove("/opt/xxx/server_log/test&q ...

  10. GZip对字符串压缩和解压

    /// <summary> /// 压缩 /// </summary> /// <param name="value">需要压缩字符串</ ...