228:汇总区间
This commit is contained in:
parent
b06f95d7a8
commit
9c39c21fce
93
src/main/java/leetcode/editor/cn/SummaryRanges.java
Normal file
93
src/main/java/leetcode/editor/cn/SummaryRanges.java
Normal file
@ -0,0 +1,93 @@
|
||||
//给定一个 无重复元素 的 有序 整数数组 nums 。
|
||||
//
|
||||
// 返回 恰好覆盖数组中所有数字 的 最小有序 区间范围列表 。也就是说,nums 的每个元素都恰好被某个区间范围所覆盖,并且不存在属于某个范围但不属于
|
||||
//nums 的数字 x 。
|
||||
//
|
||||
// 列表中的每个区间范围 [a,b] 应该按如下格式输出:
|
||||
//
|
||||
//
|
||||
// "a->b" ,如果 a != b
|
||||
// "a" ,如果 a == b
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:nums = [0,1,2,4,5,7]
|
||||
//输出:["0->2","4->5","7"]
|
||||
//解释:区间范围是:
|
||||
//[0,2] --> "0->2"
|
||||
//[4,5] --> "4->5"
|
||||
//[7,7] --> "7"
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:nums = [0,2,3,4,6,8,9]
|
||||
//输出:["0","2->4","6","8->9"]
|
||||
//解释:区间范围是:
|
||||
//[0,0] --> "0"
|
||||
//[2,4] --> "2->4"
|
||||
//[6,6] --> "6"
|
||||
//[8,9] --> "8->9"
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 0 <= nums.length <= 20
|
||||
// -2³¹ <= nums[i] <= 2³¹ - 1
|
||||
// nums 中的所有值都 互不相同
|
||||
// nums 按升序排列
|
||||
//
|
||||
// Related Topics 数组 👍 209 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
//228:汇总区间
|
||||
public class SummaryRanges {
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new SummaryRanges().new Solution();
|
||||
|
||||
}
|
||||
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public List<String> summaryRanges(int[] nums) {
|
||||
if (nums.length == 0) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
Arrays.sort(nums);
|
||||
List<String> list = new ArrayList<>();
|
||||
int bef = nums[0];
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
if (nums[i] - nums[i - 1] == 1) {
|
||||
continue;
|
||||
}
|
||||
if (bef == nums[i - 1]) {
|
||||
list.add("" + bef);
|
||||
} else {
|
||||
list.add(bef + "->" + nums[i - 1]);
|
||||
}
|
||||
bef = nums[i];
|
||||
}
|
||||
if (bef == nums[nums.length - 1]) {
|
||||
list.add("" + bef);
|
||||
} else {
|
||||
list.add(bef + "->" + nums[nums.length - 1]);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
<p>给定一个 <strong>无重复元素</strong> 的 <strong>有序</strong> 整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>返回 <em><strong>恰好覆盖数组中所有数字</strong> 的 <strong>最小有序</strong> 区间范围列表 </em>。也就是说,<code>nums</code> 的每个元素都恰好被某个区间范围所覆盖,并且不存在属于某个范围但不属于 <code>nums</code> 的数字 <code>x</code> 。</p>
|
||||
|
||||
<p>列表中的每个区间范围 <code>[a,b]</code> 应该按如下格式输出:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>"a->b"</code> ,如果 <code>a != b</code></li>
|
||||
<li><code>"a"</code> ,如果 <code>a == b</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [0,1,2,4,5,7]
|
||||
<strong>输出:</strong>["0->2","4->5","7"]
|
||||
<strong>解释:</strong>区间范围是:
|
||||
[0,2] --> "0->2"
|
||||
[4,5] --> "4->5"
|
||||
[7,7] --> "7"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [0,2,3,4,6,8,9]
|
||||
<strong>输出:</strong>["0","2->4","6","8->9"]
|
||||
<strong>解释:</strong>区间范围是:
|
||||
[0,0] --> "0"
|
||||
[2,4] --> "2->4"
|
||||
[6,6] --> "6"
|
||||
[8,9] --> "8->9"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= nums.length <= 20</code></li>
|
||||
<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
|
||||
<li><code>nums</code> 中的所有值都 <strong>互不相同</strong></li>
|
||||
<li><code>nums</code> 按升序排列</li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>数组</li></div></div><br><div><li>👍 209</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user