Leetcode--5081. 步进数
如果一個(gè)整數(shù)上的每一位數(shù)字與其相鄰位上的數(shù)字的絕對(duì)差都是 1,那么這個(gè)數(shù)就是一個(gè)「步進(jìn)數(shù)」。
例如,321?是一個(gè)步進(jìn)數(shù),而?421?不是。
給你兩個(gè)整數(shù),low?和?high,請(qǐng)你找出在?[low, high]?范圍內(nèi)的所有步進(jìn)數(shù),并返回?排序后 的結(jié)果。
?
示例:
輸入:low = 0, high = 21
輸出:[0,1,2,3,4,5,6,7,8,9,10,12,21]
?
提示:
0 <= low <= high <= 2 * 10^9
思路:一開始暴力解法,但是取值范圍2*10^9,呃,明顯不可能通過
先把所有步進(jìn)數(shù)求出來放到一個(gè)集合,排下序。然后再遍歷取其中在low與high范圍內(nèi)的數(shù)添加到結(jié)果集合
求步進(jìn)數(shù)是用 DFS 的方法,以 1 到 9 開頭,每次把當(dāng)前數(shù)乘 10 加上個(gè)位數(shù)加 1 或減 1。注意一下個(gè)位為 0 和 9 的特殊情況。
0 特殊處理。
提交的代碼:
class Solution {
? ?public static List<Integer> countSteppingNumbers(int low, int high) {
?? ??? ?int i;
?? ??? ?List<Integer> list = new ArrayList();
?? ??? ?List<Integer> list1 = new ArrayList();
?? ??? ?list.add(0);
?? ??? ?for(i=1;i<=9;i++)
?? ??? ?{
?? ??? ??? ?DFS(list,i);
?? ??? ?}
?? ??? ?Collections.sort(list);
?? ??? ?for(int in:list)
?? ??? ?{
?? ??? ??? ?if(in>=low&&in<=high)
?? ??? ??? ?{?? ??? ?
?? ??? ??? ??? ?list1.add(in);
?? ??? ??? ?}
?? ??? ?}
?? ??? ?return list1;
?? ??? ?
? ? }
?? ?public static void DFS(List<Integer> list,int i)
?? ?{
?? ??? ?list.add(i);
?? ??? ?if(i>Integer.MAX_VALUE / 10)
?? ??? ?{
?? ??? ??? ?return;
?? ??? ?}
?? ??? ?if(i%10!=0)
?? ??? ?{
?? ??? ??? ?DFS(list,(i%10-1+i*10));
?? ??? ?}
?? ??? ?if(i%10!=9)
?? ??? ?{
?? ??? ??? ?DFS(list,(i%10+1+i*10));
?? ??? ?}
?? ?}
}
完整代碼:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class Solution5081 {
?? ?public static List<Integer> countSteppingNumbers(int low, int high) {
?? ??? ?int i;
?? ??? ?List<Integer> list = new ArrayList();
?? ??? ?List<Integer> list1 = new ArrayList();
?? ??? ?list.add(0);
?? ??? ?for(i=1;i<=9;i++)
?? ??? ?{
?? ??? ??? ?DFS(list,i);
?? ??? ?}
?? ??? ?Collections.sort(list);
?? ??? ?for(int in:list)
?? ??? ?{
?? ??? ??? ?if(in>=low&&in<=high)
?? ??? ??? ?{?? ??? ?
?? ??? ??? ??? ?list1.add(in);
?? ??? ??? ?}
?? ??? ?}
?? ??? ?return list1;
?? ??? ?
? ? }
?? ?public static void DFS(List<Integer> list,int i)
?? ?{
?? ??? ?list.add(i);
?? ??? ?if(i>Integer.MAX_VALUE / 10)
?? ??? ?{
?? ??? ??? ?return;
?? ??? ?}
?? ??? ?if(i%10!=0)
?? ??? ?{
?? ??? ??? ?DFS(list,(i%10-1+i*10));
?? ??? ?}
?? ??? ?if(i%10!=9)
?? ??? ?{
?? ??? ??? ?DFS(list,(i%10+1+i*10));
?? ??? ?}
?? ?}
?? ?public static void main(String[] args)
?? ?{
?? ??? ?int low,high;
?? ??? ?low = 10;
?? ??? ?high =15;
?? ??? ?List list = countSteppingNumbers(low,high);
?? ??? ?Iterator it ?= list.iterator();
?? ??? ?while(it.hasNext())
?? ??? ?{
?? ??? ??? ?System.out.println(it.next());
?? ??? ?
?? ??? ?}
?? ??? ?
?? ?}
}
?
可以在DFS中中加入low,high判斷條件,這樣無(wú)需找出所有的步進(jìn)數(shù)。
?
?
提交的代碼:
class Solution {
? ?public static List<Integer> countSteppingNumbers(int low, int high) {
?? ??? ?int i;
?? ??? ?List<Integer> list = new ArrayList();
?? ??? ?if(low==0)
?? ??? ?{
?? ??? ??? ?list.add(0);
?? ??? ?}
?? ??? ?for(i=1;i<=9;i++)
?? ??? ?{
?? ??? ??? ?DFS(list,i,low,high);
?? ??? ?}
?? ??? ?Collections.sort(list);
?? ??? ?return list;
?? ??? ?
? ? }
?? ?public static void DFS(List<Integer> list,int i,int low,int high)
?? ?{
?? ??? ?if(i>=low&&i<=high)
?? ??? ?{
?? ??? ??? ?list.add(i);
?? ??? ?}
?? ??? ?
?? ??? ?if(i>Integer.MAX_VALUE / 10)
?? ??? ?{
?? ??? ??? ?return;
?? ??? ?}
?? ??? ?if(i%10!=0&&(i%10-1+i*10)>=0&&(i%10-1+i*10)<=high)
?? ??? ?{
?? ??? ??? ?DFS(list,(i%10-1+i*10),low,high);
?? ??? ?}
?? ??? ?if(i%10!=9&&(i%10+1+i*10)>=0&&(i%10+1+i*10)<=high)
?? ??? ?{
?? ??? ??? ?DFS(list,(i%10+1+i*10),low,high);
?? ??? ?}
?? ?}
}
完整代碼:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class Solution5081 {
?? ?public static List<Integer> countSteppingNumbers(int low, int high) {
?? ??? ?int i;
?? ??? ?List<Integer> list = new ArrayList();
?? ??? ?if(low==0)
?? ??? ?{
?? ??? ??? ?list.add(0);
?? ??? ?}
?? ??? ?for(i=1;i<=9;i++)
?? ??? ?{
?? ??? ??? ?DFS(list,i,low,high);
?? ??? ?}
?? ??? ?Collections.sort(list);
?? ??? ?return list;
?? ??? ?
? ? }
?? ?public static void DFS(List<Integer> list,int i,int low,int high)
?? ?{
?? ??? ?if(i>=low&&i<=high)
?? ??? ?{
?? ??? ??? ?list.add(i);
?? ??? ?}
?? ??? ?
?? ??? ?if(i>Integer.MAX_VALUE / 10)
?? ??? ?{
?? ??? ??? ?return;
?? ??? ?}
?? ??? ?if(i%10!=0&&(i%10-1+i*10)>=0&&(i%10-1+i*10)<=high)
?? ??? ?{
?? ??? ??? ?DFS(list,(i%10-1+i*10),low,high);
?? ??? ?}
?? ??? ?if(i%10!=9&&(i%10+1+i*10)>=0&&(i%10+1+i*10)<=high)
?? ??? ?{
?? ??? ??? ?DFS(list,(i%10+1+i*10),low,high);
?? ??? ?}
?? ?}
?? ?public static void main(String[] args)
?? ?{
?? ??? ?int low,high;
?? ??? ?low = 0;
?? ??? ?high =21;
?? ??? ?List list = countSteppingNumbers(low,high);
?? ??? ?Iterator it ?= list.iterator();
?? ??? ?while(it.hasNext())
?? ??? ?{
?? ??? ??? ?System.out.println(it.next());
?? ??? ?
?? ??? ?}
?? ??? ?
?? ?}
}
?
總結(jié)
以上是生活随笔為你收集整理的Leetcode--5081. 步进数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【剑指offer】面试题16:数值的整数
- 下一篇: Leetcode--845. 数组中的最