问题描述:

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. Elasticsearch查询Index以及删除

    查询Index信息 GET /bank HTTP/1.1Host: localhost:9200 { "bank": { "aliases": {}, &quo ...

  2. 【拼接属性查询方式】MySql某一列属性值为拼接时的查询方式

    数据库中某一列的值为 使用IN查询是无法查询到数据的,只能查询到IN(2)或者IN(2,3)  查询结果: , style_id) 正确方式:使用FIND_IN_SET函数 , style_id) , ...

  3. 【Hadoop 分布式部署 八:分布式协作框架Zookeeper架构功能讲解 及本地模式安装部署和命令使用 】

    What  is  Zookeeper 是一个开源的分布式的,为分布式应用提供协作服务的Apache项目 提供一个简单的原语集合,以便与分布式应用可以在他之上构建更高层次的同步服务 设计非常简单易于编 ...

  4. 给大家讲个故事,感受一下什么叫CF。不知道的请认真听。

    我朋友是个温柔.体贴.负责.做事认真和口才流利的好男人 他在大一时喜欢上别系的女同学,像他这样的好人我以为这段恋情是手到擒来 但并没有,女方只把它当工具人,一当就当了四年 身为室友的我每天看著她为女方 ...

  5. HDU 5245 Joyful(期望)

    http://acm.hdu.edu.cn/showproblem.php?pid=5245 题意: 给出一个n*m的矩阵格子,现在有k次操作,每次操作随机选择两个格子作为矩形的对角,然后将这范围内的 ...

  6. pymouse 点击指定坐标点

    from pymouse import PyMouse mouse = PyMouse() mouse.click(,)

  7. SpringBoot配置Aop demo

    1. Demo部分 package com.example.demo.controller; import org.springframework.web.bind.annotation.Reques ...

  8. VC_窗口exe_printf信息

    1. #include <io.h> #include <fcntl.h> #include <stdio.h> 2. void InitConsoleWindow ...

  9. ionic 搜索双向数据绑定失效

    1.用data对象存储变化的数据 js: $scope.data={}; $scope.data.keywords = ""; $scope.search = function() ...

  10. react点滴

    1.<SubSubComp {...this.props } /> 传递属性,{...props}的方式为组件传递了这两个属性,这就是JSX中的延展属性,"..."成为 ...