队列先进先出 FIFO

基于队列的广度优先排序BFS

  1. class TreeNode {
  2. int val;
  3. TreeNode left;
  4. TreeNode right;
  5. TreeNode() {
  6. }
  7. TreeNode(int val) {
  8. this.val = val;
  9. }
  10. TreeNode(int val, TreeNode left, TreeNode right) {
  11. this.val = val;
  12. this.left = left;
  13. this.right = right;
  14. }
  15. /**
  16. * DSF 前置排序 中左右
  17. * 利用队列的先进先出特性
  18. */
  19. public List<Integer> preOrder1(TreeNode root) {
  20. List<Integer> result = new ArrayList<>();
  21. Queue<TreeNode> queue = new LinkedList<>();
  22. if (root == null) {
  23. return result;
  24. }
  25. queue.offer(root);
  26. while (!queue.isEmpty()) {
  27. TreeNode poll = queue.poll();
  28. result.add(poll.val);
  29. if (poll.left != null) {
  30. queue.offer(poll.left);
  31. }
  32. if (poll.right != null) {
  33. queue.offer(poll.right);
  34. }
  35. }
  36. return result;
  37. }
  38. }

完全平方数相加最少个数

  1. /**
  2. * 完全平方数相加最少个数
  3. * 12
  4. * 0+1 0+4 0+9 0+16
  5. * 0+1+1 0+1+4 0+1+9 0+4+1 0+4+1 0+4+4 0+4+9
  6. * 0+1+1+1 0+1+1+4 0+1+1+9 0+1+4+1 0+1+4+4 0+1+4+9 0+1+9+1 0+1+9+4 0+4+1+1 0+4+1+4 0+4+1+9 0+4+4+1 0+4+4+4
  7. */
  8. public int getMinCount(int n) {
  9. Queue<Integer> queue = new LinkedList<>();
  10. // 保存已遍历的节点值
  11. Set<Integer> set = new HashSet<>();
  12. queue.offer(0);
  13. set.add(0);
  14. int num = 0;
  15. while (!queue.isEmpty()) {
  16. num++;
  17. // 每一层节点数量
  18. int size = queue.size();
  19. // 遍历当前层的所有节点
  20. for (int i = 0; i < size; i++) {
  21. // 当前节点值
  22. int rs = queue.poll();
  23. // 访问当前节点的子节点,类比于二叉树的左右子节点
  24. for (int j = 1; j <= n; j++) {
  25. // 子节点的值
  26. int nodeValue = rs + j * j;
  27. // 如果大于n,终止内层循环
  28. if (nodeValue > n) {
  29. break;
  30. }
  31. // nodeValue始终是完全平方数的和,当他等于n的时候直接返回
  32. if (nodeValue == n) {
  33. return num;
  34. }
  35. // 除去值相同插入 如 1+4 4+1
  36. if (!set.contains(nodeValue)) {
  37. queue.offer(nodeValue);
  38. set.add(nodeValue);
  39. }
  40. }
  41. }
  42. }
  43. return num;
  44. }

打开转盘锁

  1. /**
  2. * 打开转盘锁
  3. * 给出解锁需要的最小旋转次数,如果无论如何不能解锁,返回 -1
  4. * 初始值 0000
  5. * p输入:deadends =["0201","0101","0102","1212","2002"],target ="0202"
  6. * 输出:6
  7. */
  8. public int openLock(String[] deadends, String target) {
  9. Set<String> set = new HashSet<>(Arrays.asList(deadends));
  10. //开始遍历的字符串是"0000",相当于根节点
  11. String startStr = "0000";
  12. if (set.contains(startStr)) {
  13. return -1;
  14. }
  15. int level = 0;
  16. Queue<String> queue = new LinkedList<>();
  17. Set<String> history = new HashSet<>();
  18. queue.offer(startStr);
  19. history.add(startStr);
  20. while (!queue.isEmpty()) {
  21. // 当前队列元素数量
  22. int size = queue.size();
  23. for (int i = 0; i < size; i++) {
  24. String nodeVal = queue.poll();
  25. if (nodeVal.equals(target)) {
  26. return level;
  27. }
  28. for (int j = 0; j < 4; j++) {
  29. /**
  30. * j = 0 '' +'1' + '000' '' +'9' + '000'
  31. * j = 1 '0' +'1' + '00' '0' +'9' + '00'
  32. * j = 2 '00' +'1' + '0' '00' +'9' + '0'
  33. * j = 3 '000' +'1' + '' '000' +'9' + ''
  34. */
  35. char charAt = nodeVal.charAt(j);
  36. String addStr = nodeVal.substring(0, j) + (charAt == '9' ? 0 : charAt - '0' + 1) + nodeVal.substring(j + 1);
  37. String subStr = nodeVal.substring(0, j) + (charAt == '0' ? 9 : charAt - '0' - 1) + nodeVal.substring(j + 1);
  38. if (!set.contains(addStr) && !history.contains(addStr)) {
  39. queue.offer(addStr);
  40. history.add(addStr);
  41. }
  42. if (!set.contains(subStr) && !history.contains(subStr)) {
  43. queue.offer(subStr);
  44. history.add(subStr);
  45. }
  46. }
  47. }
  48. level++;
  49. }
  50. return -1;
  51. }