1291. 顺次数
程序员文章站
2022-07-14 08:02:42
...
怎么说呢,这种题已经挡不住我了????
import java.util.LinkedList;
import java.util.List;
class Solution {
List<Integer> res = new LinkedList<>();
private void dfs(String s, int len, boolean sign[], int low, int high, int index){
if(s.length() == len){
int a = Integer.valueOf(s);
if(a <= high && a >= low){
res.add(Integer.valueOf(s));
}
return;
}
for(int j = index; j < 10; j++){
if(sign[j]){
continue;
}
if(s.length() == 0){
s += (""+j);
sign[j] = true;
dfs(s, len, sign, low, high, j);
sign[j] = false;
s = s.substring(0, s.length()-1);
}else if(Integer.valueOf(""+s.charAt(s.length()-1)) == j-1){
s += (""+j);
sign[j] = true;
dfs(s, len, sign, low, high, j);
sign[j] = false;
s = s.substring(0, s.length()-1);
}
}
}
public List<Integer> sequentialDigits(int low, int high) {
int len_low = String.valueOf(low).length();
int len_high = String.valueOf(high).length();
boolean sign[] = new boolean[10];
for(int i=len_low; i<=len_high; i++){
dfs("", i, sign, low, high, 1);
}
for(int a: res){
System.out.println(a);
}
return res;
}
public static void main(String[] args) {
Solution s = new Solution();
s.sequentialDigits(100, 300);
}
}
上一篇: 毕设笔记06-使用Python读取Arduino+树莓派串口温湿度数据并保存
下一篇: 子集