问题描述:

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. P2473 [SCOI2008]奖励关

    思路 n<=15,所以状压 因为求期望,所以采用逆推的思路,设\(f[i][S]\)表示1~i的宝物获得情况是S,i+1~k的期望 状态转移是当k可以取时,\(f[i][S]+=max(f[i+ ...

  2. Matconvnet 的一些记录

    Matconvnet 的一些记录 Example code from ADNet: Action-Decision Networks for Visual Tracking with Deep Rei ...

  3. communication

    Always consider the challenge as a chance. Basic principles: Know your audience. Know your purpose. ...

  4. CAS Client集群环境的Session问题及解决方案介绍,下篇介绍作者本人项目中的解决方案代码

    CAS Client集群环境的Session问题及解决方案  程序猿讲故事  2016-05-20  原文 [原创申明:文章为原创,欢迎非盈利性转载,但转载必须注明来源] 之前写过一篇文章,介绍单点登 ...

  5. Codeforces 600E. Lomsat gelral(Dsu on tree学习)

    题目链接:http://codeforces.com/problemset/problem/600/E n个点的有根树,以1为根,每个点有一种颜色.我们称一种颜色占领了一个子树当且仅当没有其他颜色在这 ...

  6. 【二十八】xml编程(dom\xpath\simplexml)

    1.xml基础概念 作用范围: 作为程序通讯的标准. 作为配置文件. 作为小型数据库. xml语法: <根标签> <标签 元素="元素值" ...>< ...

  7. PHP 的命令行模式

    php CLI SAPI 内置Web Server 从版本 4.3.0 开始,PHP 提供了一种新类型的 CLI SAPI(Server Application Programming Interfa ...

  8. 修改linux镜像

    修改镜像的常用命令有: guestfish,guestmount等. ●修改静态文件. 命令:guestfish 安装包:yum install -y guestfish ①mount镜像:guest ...

  9. css无定宽水平居中

    转载:http://www.cnblogs.com/jogen/p/5213566.html 这个博客的菜单ui还是棒棒的. 方法一 思路:显示设置父元素为:table,子元素为:cell-table ...

  10. 第 6 章 存储 - 038 - Docker 的两类存储资源

    存储资源 Docker 为容器提供了两种存放数据的资源: 由 storage driver 管理的镜像层和容器层 Data Volume 1.storage driver 容器由最上面一个可写的容器层 ...