# 0967. Numbers With Same Consecutive Differences ###### tags: `Leetcode` `Medium` `Microsoft` `Backtracking` Link: https://leetcode.com/problems/numbers-with-same-consecutive-differences/ ## 思路 典型dfs backtracking ## Code ```java= class Solution { public int[] numsSameConsecDiff(int n, int k) { List<Integer> ans = new ArrayList<>(); for(int i = 1;i <= 9;i++){ dfs(ans, n-1, k, i, i); } int[] res = new int[ans.size()]; for(int i = 0;i < res.length;i++){ res[i]=ans.get(i); } return res; } private void dfs(List<Integer> ans, int n, int k, int cur, int last){ if(n==0){ ans.add(cur); return; } if(last+k<10){ dfs(ans, n-1, k, 10*cur+last+k, last+k); } if(last-k>=0 && k!=0){ dfs(ans, n-1, k, 10*cur+last-k, last-k); } } } ```