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.
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 ;
}
}