A Recursive algorithm


  • 0
    C
    public class Solution {
        public List<Integer> lexicalOrder(int n) {
            List<Integer> ret = new ArrayList<>();
            if (n <= 0)
                return ret;
            addNum(n, 1, ret);   
            return ret;
        }
    
        public void addNum(int n, int curData, List<Integer> dataList) {
            for(int i=0;i<=9;i++){
                if(curData+i<=n && !(i==9 && curData==1)){
                    dataList.add(curData+i);
                }else{
                    return;
                }
                addNum(n,(curData+i)*10,dataList);
            }
        }
    }
    

Log in to reply
 

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