本文是在学习中的总结,欢迎转载但请注明出处: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. 理解 Linux 的硬链接与软链接

    Linux 的文件与目录 现代操作系统为解决信息能独立于进程之外被长期存储引入了文件,文件作为进程创建信息的逻辑单元可被多个进程并发使用.在 UNIX 系统中,操作系统为磁盘上的文本与图像.鼠标与键盘 ...

  2. iOS9中如何在日历App中创建一个任意时间之前开始的提醒(三)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 四.创建任意时间之前开始的提醒 现在我们找到了指定源中的指定日 ...

  3. 高通msm8994性能及温度监测脚本

    [plain] view plain copystartTime=$(date +%Y-%m-%d-%H-%M-%S)  pathName="/data/cpu_logs"  fi ...

  4. [ExtJS5学习笔记]第五节 使用fontawesome给你的extjs5应用增加字体图标

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/38458411本文作者:sushengmiyan-------------------- ...

  5. findViewById中NullPointerException的错误

    最近在弄一个对话框的登录时,发现一个总是报NullPointerException的错误,折腾了两小时,一直没有发现细小的区别..先上图,一边说明原因 首先是 Activity类中定义的findVie ...

  6. Learn Lua in 15 Minutes

    原文地址:http://tylerneylon.com/a/learn-lua/ Learn Lua in 15 Minutes more or less For a more in-depth Lu ...

  7. 最简单的基于FFMPEG的图像编码器(YUV编码为JPEG)

    伴随着毕业论文的完成,这两天终于腾出了空闲,又有时间搞搞FFMPEG的研究了.想着之前一直搞的都是FFMPEG解码方面的工作,很少涉及到FFMPEG编码方面的东西,于是打算研究一下FFMPEG的编码. ...

  8. ABB机器人基础培训资料整理与总结

    之前对机械臂了解较少,这方面知识比较匮乏.只使用过PowercCube六自由度机械臂. 感谢ABB公司何老师的耐心指导. 学习资料汇总:(最重要的ABB Robot 官网就不列出了,这里以中文资料为主 ...

  9. TCP状态转换

    最近笔试遇到一个题目:如果tcp建立连接时第三次握手失败,tcp会做何操作?该问题的本质是判断我们对tcp的状态转换是否能有比较深刻的理解.只要理解了下面的状态转换图,很容易回答上述问题. 在此,将& ...

  10. Spring揭秘 读书笔记 七 BeanFactory的启动分析

    首先,先看我自己画的BeanFactory启动时的时序图. 第一次接触时序图,可能有些地方画的不是很符合时序图的规则,大家只关注调用顺序即可. public static void main(Stri ...