本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/46762039


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"].

思路:

(1)题意为给定已排好序的数组。求解数组中连续数字的范围,并以"->"连接起始数字和终止数字。

(2)这道题相对比较简单。只需设置两个变量m、n,初始化指向数组第一个元素,循环遍历数组,如果第i个元素和第i+1个元素值相差1,则变量n往后移;如果不相等,则变量m和n所指位置的元素即为当前起始数字和终止数字,即可获得字符串并存入集合中,此时m和n需要往后移动1位;循环直到数组遍历完成。在遍历的过程中还需要注意的是:对倒数第2个元素和倒数第1个元素的判断,以及不连续情况下m和n是否相等的判断。详情见下方代码。(代码虽然长点,但是思路还是比较清晰的)

(3)希望本文对你有所帮助。

算法代码实现如下:

package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author liqqc
 *
 */
public class Summary_Ranges {

	public static void main(String[] args) {
		int[] arr = { 1, 3 };
		summaryRanges(arr);
	}

	public static List<String> summaryRanges(int[] nums) {
		int len = nums.length;
		List<String> result = new ArrayList<String>();
		int m = 0, n = 0;
		StringBuffer buffer = null;

		if (len == 1) {
			buffer = new StringBuffer();
			buffer.append(nums[0]);
			result.add(buffer.toString());
			return result;
		}

		for (int i = 0; i < len - 1; i++) {
			buffer = new StringBuffer();
			if (nums[i] + 1 == nums[i + 1]) {
				n++;
				//i为倒数第二个元素,则i+1为最后一个元素
				if (i + 1 == len - 1) {
					buffer.append(nums[m]);
					buffer.append("->");
					buffer.append(nums[n]);
					result.add(buffer.toString());
				}
			} else {
				// 不连续了
				if(m==n){
					buffer.append(nums[m]);
				}else{
					buffer.append(nums[m]);
					buffer.append("->");
					buffer.append(nums[n]);
				}
				result.add(buffer.toString());

				m = i + 1;
				n = i + 1;

				//不连续情况下
				if (i == len - 2) {
					buffer = new StringBuffer();
					buffer.append(nums[len - 1]);
					result.add(buffer.toString());
				}
			}
		}
		return result;
	}
}

Leetcode_228_Summary Ranges的更多相关文章

  1. [LeetCode] Summary Ranges 总结区间

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

  2. [LeetCode] Missing Ranges 缺失区间

    Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing r ...

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

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

  4. Java for LeetCode 228 Summary Ranges

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

  5. LeetCode Missing Ranges

    原题链接在这里:https://leetcode.com/problems/missing-ranges/ 题目: Given a sorted integer array where the ran ...

  6. ✡ leetcode 163. Missing Ranges 找出缺失范围 --------- java

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

  7. Ranges用法

    RANGES语句:要用与选择表相同的结构创建内表,可使用RANGES语句,如下所示: 语法:RANGES <seltab> FOR <f>. 该语句创建选择表<selta ...

  8. Summary Ranges

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

  9. Missing Ranges & Summary Ranges

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

随机推荐

  1. The packages can be overrided by Java Endorsed Standards

     Endorsed Standards APIs The Endorsed Standards for Java SE constitute all classes and interfaces ...

  2. Android简易实战教程--第二十七话《自定义View入门案例之开关按钮详细分析》

    转载此博客请注明出处点击打开链接       http://blog.csdn.net/qq_32059827/article/details/52444145 对于自定义view,可能是一个比较大的 ...

  3. RecyclerView下拉刷新上拉加载(三)—对Adapter的封装

    RecyclerView下拉刷新上拉加载(一) http://blog.csdn.net/baiyuliang2013/article/details/51506036 RecyclerView下拉刷 ...

  4. 04 AutoCompleteTextView

    作用:输入部分文字跳处下拉框列出相应的条目 <pre name="code" class="html"> <!-- 当文本框出现两个字符才开始 ...

  5. java设计模式---三种工厂模式之间的区别

    简单工厂,工厂方法,抽象工厂都属于设计模式中的创建型模式.其主要功能都是帮助我们把对象的实例化部分抽取了出来,优化了系统的架构,并且增强了系统的扩展性. 本文是本人对这三种模式学习后的一个小结以及对他 ...

  6. ROS_Kinetic_14 ROS工具roswtf的基本使用方法等

    ROS_Kinetic_14 ROS工具roswtf的基本使用方法 官网教程:http://wiki.ros.org/cn/ROS/Tutorials/Getting%20started%20with ...

  7. iOS中 自定义系统相机 作者:韩俊强

    需要框架: #import <AVFoundation/AVFoundation.h> #import <AssetsLibrary/AssetsLibrary.h> 布局如下 ...

  8. UNIX环境高级编程——进程间通讯方法整理

    一.无名管道pipe #include <unistd.h> int pipe(int fd [2]) 二.fifo #include <sys/stat.h> int mkf ...

  9. 通过freemarker生成一个word,解决生成的word用wps打开有问题的问题,解决出word时中文文件名乱码问题,解决打开出word时打开的word出现问题的问题,出图片,解决动态列表

     通过freemarker制作word比较简单 步骤:制作word模板.制作方式是:将模板word保存成为xml----在xml的word模板中添加相应的标记----将xml的word文件的后缀名 ...

  10. Android图片色彩变幻

    最近在做图片相关的应用,所以就各方积累到一些常用的操作,一般来说会有多种方式来实现这一功能,比如 采用色度变换 采用ColorMatrix颜色矩阵 采用对像素点的直接操作 等等,今天就复习一下第一种方 ...