Leetcode_228_Summary Ranges

标签: LeetCode 算法 编程 JAVA
1112人阅读 评论(0) 收藏 举报
分类:

本文是在学习中的总结,欢迎转载但请注明出处: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

leetcode 228: Summary Ranges python, java, c++
  • xudli
  • xudli
  • 2015-06-26 06:22:18
  • 10259

LeetCode228——Summary Ranges

一个月没写C++代码,现在感到好陌生 Given a sorted integer array without duplicates, return the summary of its range...
  • booirror
  • booirror
  • 2015-07-09 00:25:58
  • 1865

<LeetCode OJ> 228. Summary Ranges

228. Summary Ranges My Submissions Question Total Accepted: 36554 Total Submissions: 159532 Diff...
  • EbowTang
  • EbowTang
  • 2016-02-10 16:11:10
  • 825

Summary Ranges(数组中连续的数字段)

Summary Ranges Given a sorted integer array without duplicates, return the summary of its ranges. ...
  • u010032372
  • u010032372
  • 2015-06-26 11:53:18
  • 624

LeetCode - 228. Summary Ranges - 思路详解- C++

假设有一个已排序整数数组,不包含重复的元素,返回其范围摘要
  • SJWL2012
  • SJWL2012
  • 2017-01-13 20:50:21
  • 190

Summary Ranges - LeetCode 228

题目描述: Given a sorted integer array without duplicates, return the summary of its ranges. For examp...
  • bu_min
  • bu_min
  • 2015-06-28 09:11:16
  • 279

Leetcode_228_Summary Ranges

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/46762039 Given a sorted in...
  • pistolove
  • pistolove
  • 2015-07-05 11:07:31
  • 1112

LeetCode 228: Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges. For example, give...
  • sunao2002002
  • sunao2002002
  • 2015-06-26 18:32:00
  • 3878

[leetcode] 228. Summary Ranges 解题报告

题目链接:https://leetcode.com/problems/summary-ranges/ Given a sorted integer array without duplicates...
  • qq508618087
  • qq508618087
  • 2015-12-27 13:30:50
  • 234

leetcode解题报告228——Summary Ranges

**Given a sorted integer array without duplicates, return the summary of its ranges. For example, g...
  • xxxknight
  • xxxknight
  • 2015-06-28 00:42:37
  • 462
    个人资料
    专栏达人 持之以恒
    等级:
    访问量: 49万+
    积分: 7105
    排名: 4001
    博客专栏
    最新评论