欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

1291. 顺次数

程序员文章站 2022-07-14 08:02:42
...

怎么说呢,这种题已经挡不住我了????

1291. 顺次数

 

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

1291. 顺次数

相关标签: leetecode