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<String> list = new ArrayList<String>();
		if (nums.length == 0)
			return list;
		int left = nums[0], index = nums[0];
		for (int i = 1; i < nums.length; i++) {
			if (index == nums[i] - 1)
				index++;
			else if (index == left) {
				list.add(index + "");
				left = nums[i];
				index = nums[i];
			} else {
				list.add(left + "->" + index);
				left = nums[i];
				index = nums[i];
			}

		}
		if (index == left)
				list.add(index + "");
		else
				list.add(left + "->" + index);

		return list;
    }
时间: 2024-10-13 08:09:33

Java for LeetCode 228 Summary Ranges的相关文章

[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

LeetCode:Summary Ranges - 获取数组中数字的范围

1.题目名称 Summary Ranges(获取数组中数字的范围) 2.题目地址 https://leetcode.com/problems/summary-ranges/ 3.题目内容 英文:Given a sorted integer array without duplicates, return the summary of its ranges. 中文:给出一个整数数组,返回这个数组中数字的范围 例如:给出数组 [0,1,2,4,5,7],返回 ["0->2",&quo

[LeetCode][JavaScript]Summary Ranges

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"]. https://leetcode.com/problems/summary-ranges/ 简单的模拟题,合并连续的数

【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

【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"]. 意:找出排序数组(无重复数字)中的各个连续的范围 算法思想 基本思想如下,给定排序数组 N<a1,a2,a3,...,an>,

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]8. 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)初始化两个游标ind1=0,ind2=1,一个左边界left=ind1:(2)若nums[ind2]-nums[ind1]==1则ind

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 problem and

[leedcode 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"]. public class Solution { public List<String> summaryRanges(int[] nums