[Swift Weekly Contest 117]LeetCode967. 具有相同连续差异的数字 | 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. 1 <= N <= 9
  2. 0 <= K <= 9


返回长度为n的所有非负整数,使每两个连续数字之间的绝对差为k。

注意,答案中的每个数字都不能有前导零,除了数字0本身。例如,01有一个前导零,它是无效的,但0是有效的。

您可以按任何顺序返回答案。

例1:

输入:n=3,k=7

输出:【181292707818929】

说明:请注意,070不是有效数字,因为它有前导零。

例2:

输入:n=2,k=1

输出:【10、12、21、23、32、34、43、45、54、56、65、67、76、78、87、89、98】

注:

  1. 1 <n= 9
  2. 0 <= k<=9


28ms

 1 class Solution {
 2     var v:[Int] = [Int]()
 3     var n:Int = 0
 4     var k:Int = 0
 5     var val:Int = 0
 6     func numsSameConsecDiff(_ N: Int, _ K: Int) -> [Int] {
 7         if N == 1
 8         {
 9             for i in 0...9
10             {
11                 v.append(i)
12             }
13             return v
14         }
15         n = N
16         k = K
17         val = 0
18         dfs(0,0)
19         v = v.sorted(by:>)
20         return v
21     }
22
23     func dfs(_ cur:Int,_ pr:Int)
24     {
25         if cur == n
26         {
27             v.append(val)
28             return
29         }
30         for i in 0...9
31         {
32             if cur == 0
33             {
34                 if i != 0
35                 {
36                     val = i
37                     dfs(cur + 1,i)
38                 }
39             }
40             else
41             {
42                 val *= 10
43                 val += i
44                 if abs(pr - i) == k
45                 {
46                     dfs(cur + 1, i)
47                 }
48                 val -= i
49                 val /= 10
50             }
51         }
52     }
53 }

原文地址:https://www.cnblogs.com/strengthen/p/10201442.html

时间: 2024-08-30 16:30:23

[Swift Weekly Contest 117]LeetCode967. 具有相同连续差异的数字 | Numbers With Same Consecutive Differences的相关文章

[Swift Weekly Contest 117]LeetCode965. 单值二叉树 | Univalued Binary Tree

A binary tree is univalued if every node in the tree has the same value. Return true if and only if the given tree is univalued. Example 1: Input: [1,1,1,1,1,null,1] Output: true Example 2: Input: [2,2,2,5,2] Output: false Note: The number of nodes i

[Swift Weekly Contest 117]LeetCode966.元音拼写检查 | Vowel Spellchecker

Given a wordlist, we want to implement a spellchecker that converts a query word into a correct word. For a given query word, the spell checker handles two categories of spelling mistakes: Capitalization: If the query matches a word in the wordlist (

[Swift Weekly Contest 113]LeetCode952. 按公因数计算最大组件大小 | Largest Component Size by Common Factor

Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.

[Swift Weekly Contest 118]LeetCode974. 和可被 K 整除的子数组 | Subarray Sums Divisible by K

Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K. Example 1: Input: A = [4,5,0,-2,-3,1], K = 5 Output: 7 Explanation: There are 7 subarrays with a sum divisible by K = 5: [4, 5, 0, -2

[Swift Weekly Contest 108]LeetCode930. 和相同的二元子数组 | Binary Subarrays With Sum

In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] Note: A.length <= 30000 0 <= S <

[Swift Weekly Contest 108]LeetCode929. 独特的电子邮件地址 | Unique Email Addresses

Every email consists of a local name and a domain name, separated by the @ sign. For example, in [email protected], alice is the local name, and leetcode.com is the domain name. Besides lowercase letters, these emails may contain '.'s or '+'s. If you

[Swift Weekly Contest 108]LeetCode932. 漂亮数组 | Beautiful Array

For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such that: For every i < j, there is no k with i < k < j such that A[k] * 2 = A[i] + A[j]. Given N, return any beautiful array A.  (It is guaranteed th

[Swift Weekly Contest 108]LeetCode931. 下降路径最小和 | Minimum Falling Path Sum

Given a square array of integers A, we want the minimum sum of a falling path through A. A falling path starts at any element in the first row, and chooses one element from each row.  The next row's choice must be in a column that is different from t

[Swift Weekly Contest 109]LeetCode933. 最近的请求次数 | Number of Recent Calls

Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t represents some time in milliseconds. Return the number of pings that have been made from 3000 milliseconds ago until now. Any ping with time in [t - 3