Advertisement
Kuroshi1

Untitled

Sep 2nd, 2022
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. class Solution {
  2. public int[] numsSameConsecDiff(int n, int k) {
  3. if (n == 1) return new int[]{k};
  4. List<Integer> result = new ArrayList<>();
  5.  
  6. // for (int i = 1; i < 10; i++) {
  7. // int sum = i;
  8. // for (int j = 1; j < 10; i++) {
  9. // if (Math.abs(i - j) == k) {
  10. // int nextDigit = j;
  11. // sum = sum * 10 + nextDigit;
  12. // }
  13. // }
  14. // }
  15.  
  16. int prev = 1;
  17. int next = 0;
  18. int sum = 1;
  19. while (prev < 10) {
  20. if (Math.abs(prev - next) == k) {
  21. sum = sum * 10 + next;
  22.  
  23. if (sum > Math.pow(10, n - 1)) {
  24. result.add(sum);
  25. prev++;
  26. next = 0;
  27. sum = prev;
  28. continue;
  29. } else {
  30. prev = next;
  31. next = -1;
  32. }
  33.  
  34. prev = next;
  35. next = -1;
  36.  
  37. }
  38.  
  39. if (next == 9 || sum > Math.pow(10, n - 1)) {
  40. prev++;
  41. next = 0;
  42. sum = 1;
  43. continue;
  44. }
  45.  
  46. next++;
  47. System.out.println(sum + " " + result);
  48. }
  49.  
  50. return result.stream().mapToInt(i->i).toArray();
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement