问题描述:

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. Linux---centos 配置网络

    Linux配置网络,有两种方式,一种是通过图像化的界面来配置网络IP,另一种方式是通过命令行来配置IP 1.第一种方式通过图形化的界面来配置IP 1.0修改之前的IP地址 1.1点击图片中的那个 网络 ...

  2. 微服务架构与实践4_Docker

    构建Docker映像 定义Dockerfile=>Docker根据Dockerfile构建出映像 包含: 基础映像(父映像)信息 维护者信息 映像操作命令 容器启动命令 .net standar ...

  3. facebook api之Ad

    Ad Contains information to display an ad and associate it an ad set. Each ad is associated with an a ...

  4. 洛谷P2362 围栏木桩----dp思路

    在翻dp水题的时候找到的有趣的题0v0 原文>>https://www.luogu.org/problem/show?pid=2362<< 题目描述 某农场有一个由按编号排列的 ...

  5. [Err] 1449 - The user specified as a definer ('rybhe'@'%') does not exist

    转载: 最近在做一个项目,由于服务器切换,所以需要将原有服务器的mysql数据表以及存储过程导入到另一个服务器的mysql数据库中.导入完成之后以为一切是那么的简单,却没有想到总还是出现了一些莫名其妙 ...

  6. Jenkins-job迁移

    摘自:http://www.cnblogs.com/topplay/p/3899330.html Jenkins迁移job 说明:从一个Jenkins服务器A将现有job迁移到另外一个Jenkins服 ...

  7. JavaScript——执行环境、变量对象、作用域链

    前言 这几天在看<javascript高级程序设计>,看到执行环境和作用域链的时候,就有些模糊了.书中还是讲的不够具体.通过上网查资料,特来总结,以备回顾和修正. 目录: EC(执行环境或 ...

  8. HDU 5791 Two(LCS求公共子序列个数)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5791 题意: 给出两个序列,求这两个序列的公共子序列的总个数. 思路: 和LCS差不多,dp[i][ ...

  9. PHP 时间函数time、date和microtime的区别

    一.time.date 和 microtime函数 time----返回当前的 Unix 时间戳 date----格式化一个本地时间/日期 microtime----返回当前的 Unix 时间戳和微秒 ...

  10. git pull 提示 There is no tracking information for the current branch

    在执行git pull的时候,提示当前branch没有跟踪信息: git pull There is no tracking information for the current branch. P ...