You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
756 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package class32;
import java.util.ArrayList;
import java.util.List;
public class Problem_0163_MissingRanges {
public static List<String> findMissingRanges(int[] nums, int lower, int upper) {
List<String> ans = new ArrayList<>();
for (int num : nums) {
if (num > lower) {
ans.add(miss(lower, num - 1));
}
if (num == upper) {
return ans;
}
lower = num + 1;
}
if (lower <= upper) {
ans.add(miss(lower, upper));
}
return ans;
}
// 生成"lower->upper"的字符串如果lower==upper只用生成"lower"
public static String miss(int lower, int upper) {
String left = String.valueOf(lower);
String right = "";
if (upper > lower) {
right = "->" + String.valueOf(upper);
}
return left + right;
}
}