我們定義「順次數」為:每一位上的數字都比前一位上的數字大 1 的整數。
請你返回由 [low, high] 范圍內所有順次數組成的 有序 列表(從小到大排序)。
示例 1:
輸出:low = 100, high = 300
輸出:[123,234]
代碼
class Solution {List<Integer> cList=new ArrayList<>();public List<Integer> sequentialDigits(int low, int high) {for(int i=1;i<10;i++)//以不同數字開頭{sequential(low,high,i,i);}Collections.sort(cList);排序return cList;}public void sequential(int low, int high,int pre,int sum) {if(pre>9) return;//不能再遞增1了if(sum>high) return;//數字太大了if(sum>=low) {//滿足條件cList.add(sum);}sequential(low, high, pre+1, sum*10+pre+1);下一位}
}