日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Leetcode--5081. 步进数

發布時間:2024/7/19 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Leetcode--5081. 步进数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

如果一個整數上的每一位數字與其相鄰位上的數字的絕對差都是 1,那么這個數就是一個「步進數」。

例如,321?是一個步進數,而?421?不是。

給你兩個整數,low?和?high,請你找出在?[low, high]?范圍內的所有步進數,并返回?排序后 的結果。

?

示例:

輸入: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,呃,明顯不可能通過

先把所有步進數求出來放到一個集合,排下序。然后再遍歷取其中在low與high范圍內的數添加到結果集合
求步進數是用 DFS 的方法,以 1 到 9 開頭,每次把當前數乘 10 加上個位數加 1 或減 1。注意一下個位為 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判斷條件,這樣無需找出所有的步進數。

?

?

提交的代碼:

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

總結

以上是生活随笔為你收集整理的Leetcode--5081. 步进数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。