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 };