Leetcode-967 Numbers With Same Consecutive Differences(连续差相同的数字)
Posted Asurudo Jyo の 倉 庫
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode-967 Numbers With Same Consecutive Differences(连续差相同的数字)相关的知识,希望对你有一定的参考价值。
1 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 2 class Solution 3 { 4 public: 5 vector<int> rnt; 6 void dfs(int N,int K,int h,string s) 7 { 8 if(h==N) 9 { 10 stringstream ss; 11 ss<<s; 12 int tmp; 13 ss>>tmp; 14 rnt.push_back(tmp); 15 return ; 16 } 17 if(h==0) 18 { 19 _for(i,0,10) 20 { 21 s+=i+‘0‘; 22 dfs(N,K,h+1,s); 23 s.pop_back(); 24 } 25 } 26 else 27 { 28 _for(i,0,10) 29 { 30 if(s[h-1]==‘0‘&&h==1) 31 break; 32 if(abs(i+‘0‘-s[h-1])==K) 33 { 34 s+=i+‘0‘; 35 dfs(N,K,h+1,s); 36 s.pop_back(); 37 } 38 } 39 } 40 } 41 vector<int> numsSameConsecDiff(int N, int K) 42 { 43 rnt.clear(); 44 string s; 45 dfs(N,K,0,s); 46 return rnt; 47 } 48 };
以上是关于Leetcode-967 Numbers With Same Consecutive Differences(连续差相同的数字)的主要内容,如果未能解决你的问题,请参考以下文章
LeetcodeCount Numbers with Unique Digits
PAT1023. Have Fun with Numbers (20)