Leetcode 967 Numbers With Same Consecutive Differences
Return all non-negative integers of length N
such that the absolute difference between every two consecutive digits is K
.
Note that every number in the answer must not have leading zeros except for the number 0
itself. For example, 01
has one leading zero and is invalid, but 0
is valid.
You may return the answer in any order.
Example 1:
Input: N = 3, K = 7 Output: [181,292,707,818,929] Explanation: Note that 070 is not a valid number, because it has leading zeroes.
Example 2:
Input: N = 2, K = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98]
Note:
1 <= N <= 9
0 <= K <= 9
這個題的意思是:N為數的位數,K為可以調整的數的大小
class Solution {
public int[] numsSameConsecDiff(int N, int K) {
if(N == 1){
return new int[]{0,1,2,3,4,5,6,7,8,9};
}//如果為1的話就是所有的個位數,注意0也包含在內
int array[] = new int[]{1,2,3,4,5,6,7,8,9};//1到9進行迴圈
for(int i = 2 ; i <= N;++i){//位數的迴圈
ArrayList<Integer> list = new ArrayList<>();//易於新增
for(int j : array){//調取array中的陣列
int y = j % 10;//抽取出個位數來
if(K + y < 10){
list.add(10 * j + y + K);
}//加上K之後的數
if(y - K >= 0 && K != 0){
list.add(10 * j + y - K);
}//減掉K之後的數
}
array = list.stream().mapToInt(j->j).toArray();//將list中的數轉化為array中的數
}
return array;
}
}
假如N=3,K=8,array中數字的變化:
1,2,3,4,5,6,7,8,9
19,80,91
191,808,919
其中程式碼中的list.stream().mapToInt().toArray()為Java8中的語法,其中stream是將list中的數變為資料流,mapToInt是將流轉化為list,其中的J->J是轉化的方式,就是1:1對映(如果為J->J*J則為平方的方式轉化),toArray是將之前轉化的數變為Array的形式。
以下附上Java8的文件:Java8
相關文章
- Leetcode Same TreeLeetCode
- Leetcode-Longest Consecutive SequenceLeetCode
- Longest Consecutive Sequence leetcode javaLeetCodeJava
- [leetcode]same-treeLeetCode
- Leetcode-Same TreeLeetCode
- Same Tree leetcode javaLeetCodeJava
- Leetcode - Bitwise AND of Numbers RangeLeetCode
- LeetCode-Lexicographical NumbersLeetCode
- LeetCode128:Longest Consecutive SequenceLeetCode
- Leetcode 298 Binary Tree Longest Consecutive SequenceLeetCode
- LeetCode- Binary Tree Longest Consecutive SequenceLeetCode
- Leetcode 100. Same TreeLeetCode
- LeetCode-Bitwise AND of Numbers RangeLeetCode
- Leetcode Sum Root to Leaf NumbersLeetCode
- Leetcode Add Two NumbersLeetCode
- LeetCode 2 Add Two NumbersLeetCode
- Leetcode-Add Two NumbersLeetCode
- Sum Root to Leaf Numbers leetcode javaLeetCodeJava
- Add Two Numbers leetcode javaLeetCodeJava
- 100. Same Tree--LeetCode RecordLeetCode
- Leetcode 165 Compare Version NumbersLeetCode
- LeetCode-2 Add Two NumbersLeetCode
- LeetCode 2. Add Two NumbersLeetCode
- Leetcode[421] Maximum XOR of Two Numbers in an ArrayLeetCode
- LeetCode- Count Numbers with Unique DigitsLeetCodeGit
- leetcode刷題--Compare Version NumbersLeetCode
- LeetCode OJ : 2 Add Two NumbersLeetCode
- LeetCode 129. Sum Root to Leaf NumbersLeetCode
- [leetCode][016] Add Two NumbersLeetCode
- LeetCode129:Sum Root to Leaf NumbersLeetCode
- LeetCode之Sum of Even Numbers After Queries(Kotlin)LeetCodeKotlin
- LeetCode 448. Find All Numbers Disappeared in an ArrayLeetCodeAPP
- [Leetcode] 2. Add Two Numbers QuestionEditorial SolutionLeetCode
- LeetCode2: Add two numbers(兩數相加)LeetCode
- [LeetCode] Add Two Numbers 兩個數字相加LeetCode
- 【LeetCode 100_二叉樹_遍歷】Same TreeLeetCode二叉樹
- Fifth. LeetCode 2:Add Two Numbers 兩數之和LeetCode
- Leetcode 1365. How Many Numbers Are Smaller Than the Current Number (cpp)LeetCode