问题描述:

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. windows下使用gvim不支持python3.6问题解决

    在用户目录下C:\Users\Administrator\新建vim配置文件夹vimfiles,然后该文件下建立一个文件vimrc vimrc内容: set pythonthreedll=python ...

  2. 【第十七章】 springboot + devtools(热部署)

    技术介绍 devtools:是boot的一个热部署工具,当我们修改了classpath下的文件(包括类文件.属性文件.页面等)时,会重新启动应用(由于其采用的双类加载器机制,这个启动会非常快,如果发现 ...

  3. C# 中2个问号的作用。C#的??代表是什么意思

    https://www.cnblogs.com/gggg/p/5867412.html 变量定义中含有一个问号,意思是这个数据类型是NullAble类型的.(NullAble意思是可以为空) 变量定义 ...

  4. P4777 【模板】扩展中国剩余定理(EXCRT)

    思路 中国剩余定理解决的是这样的问题 求x满足 \[ \begin{matrix}x \equiv a_1(mod\ m_1)\\x\equiv a_2(mod\ m_2)\\ \dots\\x\eq ...

  5. 题解——Codeforces Round #508 (Div. 2) T3 (贪心)

    贪心的选取最优解 然后相减好 记得要开long long #include <cstdio> #include <algorithm> #include <cstring ...

  6. CentOS7使用httpd apache 和firewalld打开关闭防火墙与端口

    Centos7 使用systemctl 工具操作命令 systemctl 是Centos7的服务管理工具中的主要工具 ,它融合之前service和chkconfig的功能于一体 一.httpd的设置 ...

  7. 【Entity framework】Code First Approach

    开篇之前感谢 china_fucan的文章给我的帮助,下面的评论也解决了很多问题同样给予感谢. code first 项目中的ORM框架如果采用的是EF,那么可能会采用code first的方式去使用 ...

  8. BZOJ 1061: [Noi2008]志愿者招募(线性规划与网络流)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1061 题意: 思路: 直接放上大神的建模过程!!!(https://www.byvoid.com/z ...

  9. 屏幕尺寸,分辨率,像素,PPI之间到底什么关系?

    转载自:http://www.jianshu.com/p/c3387bcc4f6e 感谢博主的无私分享. 今天我给大家来讲讲这几个咱们经常打交道的词到底啥意思,以及他们之间到底有什么关系.这篇文章是我 ...

  10. JS中的document.title可以获取当前网页的标题

    <!DOCTYPE html> <html> <head> <title>jb51.net</title> </head> &l ...