在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ A Stepping Number is an integer such that all of its adjacent digits have an absolute difference of exactly Given two integers
Example 1: Input: low = 0, high = 21 Output: [0,1,2,3,4,5,6,7,8,9,10,12,21]
Constraints:
如果一个整数上的每一位数字与其相邻位上的数字的绝对差都是 例如, 给你两个整数,
示例: 输入:low = 0, high = 21 输出:[0,1,2,3,4,5,6,7,8,9,10,12,21]
提示:
Runtime: 152 ms
Memory Usage: 22.3 MB
1 class Solution { 2 var L:Int = 0 3 var S:[Int] = [Int]() 4 5 func countSteppingNumbers(_ low: Int, _ high: Int) -> [Int] { 6 self.L = high 7 var ans:[Int] = [Int]() 8 for i in 1...9 9 { 10 search(i) 11 } 12 if 0 >= low && 0 <= high 13 { 14 ans.append(0) 15 } 16 for i in S 17 { 18 if i >= low && i <= high 19 { 20 ans.append(i) 21 } 22 } 23 ans.sort() 24 return ans 25 } 26 27 func search(_ x:Int) 28 { 29 if x > L{return} 30 S.append(x) 31 let last:Int = x % 10 32 if last != 0 33 { 34 search(x * 10 + last - 1) 35 } 36 if last != 9 37 { 38 search(x * 10 + last + 1) 39 } 40 } 41 }
|
请发表评论