Lettcode_228_Summary Ranges

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-08 08:32:25

Lettcode_228_Summary Ranges的相关文章

228. Summary Ranges

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"]. 这个题目不难,首先对原序列进行排序,然后要注意对特殊情况的处理.同时,要掌握string类的几个重要成员函数: to_string():将数字转换为

[LeetCode]228.Summary Ranges

题目 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"]. 代码 /*--------------------------------------- * 日期:2015-08-04 * 作者:SJF01

Java for LeetCode 228 Summary Ranges

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"]. 解题思路: JAVA实现如下: public List<String> summaryRanges(int[] nums) { List

Summary Ranges —— LeetCode

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 class Solution { public List<String>

leetcode[163]Missing Ranges

Given a sorted integer array where the range of elements are [lower, upper] inclusive, return its missing ranges. For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->49", "51->74", "76-

[LeetCode] Missing Ranges 缺失区间

Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing ranges.For example, given [0, 1, 3, 50, 75], return [“2”, “4->49”, “51->74”, “76->99”] 这道题让我们求缺失区间,跟之前那道Summary Ranges很类似,这道题让我们求缺失的空间,给了一个空间的范围[lo

Summary Ranges

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"]. Runtime: 0ms 1 class Solution { 2 public: 3 vector<string> summaryRa

LeetCode -- Summary Ranges

题目描述: 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"]. Credits:Special thanks to @jianchao.li.fighter for adding this probl

【LeetCode】228 - Summary Ranges

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 class Solution { 2 public: 3 vector<string> summaryRanges(vector&l