Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int[] numsSameConsecDiff(int n, int k) {
- if (n == 1) return new int[]{k};
- List<Integer> result = new ArrayList<>();
- // for (int i = 1; i < 10; i++) {
- // int sum = i;
- // for (int j = 1; j < 10; i++) {
- // if (Math.abs(i - j) == k) {
- // int nextDigit = j;
- // sum = sum * 10 + nextDigit;
- // }
- // }
- // }
- int prev = 1;
- int next = 0;
- int sum = 1;
- while (prev < 10) {
- if (Math.abs(prev - next) == k) {
- sum = sum * 10 + next;
- if (sum > Math.pow(10, n - 1)) {
- result.add(sum);
- prev++;
- next = 0;
- sum = prev;
- continue;
- } else {
- prev = next;
- next = -1;
- }
- prev = next;
- next = -1;
- }
- if (next == 9 || sum > Math.pow(10, n - 1)) {
- prev++;
- next = 0;
- sum = 1;
- continue;
- }
- next++;
- System.out.println(sum + " " + result);
- }
- return result.stream().mapToInt(i->i).toArray();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement