JAVA recursion solution


  • 0
    T
    public class Solution {
        List<Integer> output= new ArrayList<Integer>();
        public List<Integer> lexicalOrder(int n) {
            int start = 1;
            while(start<=n && start <= 9){
                output.add(start);
                for(int i = 0; i < 10; i++){
                    int s = 10*start + i;
                    help(s, n);
                }
                start++;
            }
            return output;
        }
        
        public boolean help(int start, int n){
            if(start<=n){
                output.add(start);
                for(int i = 0; i < 10; i++){
                    int s = 10*start + i;
                    boolean con = help(s, n);
                    if (!con) break;
                }
                return true;
            }
            else{
                return false;
            }
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.