1、剑指 Offer 10- I. 斐波那契数列(简单)

题目

https://leetcode-cn.com/problems/fei-bo-na-qi-shu-lie-lcof/

思路

基本属于将题目翻译成代码,如果不加缓存逻辑会导致超时。

代码

  1. public class Offer10 {
  2. /**
  3. * 缓存
  4. * */
  5. public static final int[] MEMORY = new int[101];
  6. public static int fib(int n) {
  7. if (n==0) {
  8. return 0;
  9. }
  10. if (n==1) {
  11. return 1;
  12. }
  13. // 判断缓存
  14. if (MEMORY[n] != 0) {
  15. return MEMORY[n];
  16. }
  17. MEMORY[n] = (fib(n-1)+fib(n-2)) % 1000000007;
  18. return MEMORY[n];
  19. }
  20. public static void main(String[] args) {
  21. System.out.println(fib(50));
  22. }
  23. }

2、剑指 Offer 10- II. 青蛙跳台阶问题(简单)

题目

https://leetcode-cn.com/problems/qing-wa-tiao-tai-jie-wen-ti-lcof/

思路

同上一题,主要区别在于阶梯为0的时候,要处理成1。

代码

  1. public class OfferTwo10 {
  2. /**
  3. * 缓存
  4. * */
  5. public static final int[] MEMORY = new int[101];
  6. public static int numWays(int n) {
  7. if (n == 0) return 1;
  8. if (n == 1) return 1;
  9. if (n == 2) return 2;
  10. if (MEMORY[n] != 0) {
  11. return MEMORY[n];
  12. }
  13. // 题目要求取模,不然算出来可能是负数的。
  14. int mod = (numWays(n - 1) + numWays(n - 2)) % 1000000007;
  15. MEMORY[n] = mod;
  16. return MEMORY[n];
  17. }
  18. public static void main(String[] args) {
  19. System.out.println(numWays(0));
  20. }
  21. }

3、面试题 08.01. 三步问题(简单)

题目

https://leetcode-cn.com/problems/three-steps-problem-lcci/

思路

按正常两阶递归增加一阶逻辑。但是算出来一直是负数。原因是int越界了。

代码

  1. public class Interview0801 {
  2. /**
  3. * 缓存
  4. * */
  5. public static final int[] MEMORY = new int[1000001];
  6. public static int waysToStep(int n) {
  7. if (n==0) return 1;
  8. if (n==1) return 1;
  9. if (n==2) return 2;
  10. if (MEMORY[n] != 0) {
  11. return MEMORY[n];
  12. }
  13. // 第一次提交的时候 int mod = (waysToStep(n - 1) + waysToStep(n - 2) + waysToStep(n - 3) % 1000000007)
  14. // 发现结果成了负数了。
  15. int mod = ((waysToStep(n - 1) + waysToStep(n - 2)) % 1000000007 + waysToStep(n - 3)) % 1000000007;
  16. MEMORY[n] = mod;
  17. return MEMORY[n];
  18. }
  19. public static void main(String[] args) {
  20. System.out.println(waysToStep(1000));
  21. }
  22. }

4、剑指 Offer 06. 从尾到头打印链表(简单)

题目

https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/

思路

每次都是打印当前节点+后续节点所有的值。
IMG_1785.jpg

注意点
本地跑main的时候因为方法和list都被加上了static 关键字。在提交的时候出现了这种情况:
测试用例:[]
测试结果:[2,3,1]
期望结果:[]

代码

  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. public class Offer06 {
  5. private List<Integer> resultList = new ArrayList<>();
  6. public int[] reversePrint(ListNode head) {
  7. // 当前为尾节点,返回空数组
  8. if (head == null) {
  9. return resultList.stream().mapToInt(Integer::intValue).toArray();
  10. }
  11. // 递归调用
  12. reversePrint(head.next);
  13. // 因为归的时候实际上就是相当于先进后出,所以将结果加到数组最后面即可。
  14. resultList.add(head.val);
  15. // 类型转换
  16. return resultList.stream().mapToInt(Integer::intValue).toArray();
  17. }
  18. // public static void main(String[] args) {
  19. // ListNode node1 = new ListNode(1);
  20. // ListNode node2 = new ListNode(2, node1);
  21. // ListNode node3 = new ListNode(3, node2);
  22. // ListNode node4 = new ListNode(1, node3);
  23. // int[] ints = reversePrint(node4);
  24. // for (int anInt : ints) {
  25. // System.out.println(anInt);
  26. // }
  27. // }
  28. public static class ListNode {
  29. int val;
  30. ListNode next;
  31. ListNode() {}
  32. ListNode(int val) { this.val = val; }
  33. ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  34. }
  35. }

5、剑指 Offer 24. 反转链表(简单)

题目

https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/

思路

专项练习用递归实现,正常思路直接循环遍历节点之后采用头插法。
递的话主要就是传递下一个节点之后的链表。
归的话主要是假设有2个节点需要反转怎么弄。
然后发现3、4、5…个节点也能这样实现。
注意点在于遍历链表的时候定义一个指针会比较方便想明白。

代码

  1. import java.util.Arrays;
  2. /**
  3. * 剑指 Offer 24. 反转链表(简单)
  4. * 题目
  5. * https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/
  6. *
  7. * @author Altman
  8. * @date 2022/4/23
  9. */
  10. public class Offer24 {
  11. public static ListNode reverseList(ListNode head) {
  12. // 空节点
  13. if (head == null) {
  14. return null;
  15. }
  16. // 意味着只有一个节点,返回新的链表
  17. if (head.next == null) {
  18. return new ListNode(head.val);
  19. }
  20. ListNode newList = reverseList(head.next);
  21. // 这里指定一个指针,靠指针的移动来表示当前指向哪一个节点。
  22. ListNode p = newList;
  23. // 遍历链表,将该节点的值按照尾插法。
  24. while (newList != null) {
  25. // 当前节点指向 null 就可以尾插节点了
  26. if (p.next == null) {
  27. ListNode newNode = new ListNode(head.val);
  28. p.next = newNode;
  29. break;
  30. }
  31. p = p.next;
  32. }
  33. return newList;
  34. }
  35. public static void main(String[] args) {
  36. ListNode node = ListNode.str2Node("5,4,3,2,1");
  37. System.out.println(ListNode.toString(reverseList(node)));
  38. }
  39. public static class ListNode {
  40. int val;
  41. ListNode next;
  42. ListNode() {
  43. }
  44. ListNode(int val) {
  45. this.val = val;
  46. }
  47. ListNode(int val, ListNode next) {
  48. this.val = val;
  49. this.next = next;
  50. }
  51. /**
  52. * 打印链表内容
  53. * @date 2022/4/23
  54. */
  55. public static String toString(ListNode listNode) {
  56. StringBuilder sb = new StringBuilder();
  57. if (listNode == null) {
  58. return sb.toString();
  59. }
  60. sb.append(listNode.val);
  61. while (true) {
  62. if (listNode.next == null) {
  63. break;
  64. }
  65. sb.append(",");
  66. sb.append(listNode.next.val);
  67. listNode = listNode.next;
  68. }
  69. return sb.toString();
  70. }
  71. /**
  72. * string转链表(方便测试没有做过多校验)
  73. * @param s 逗号分割节点
  74. * @date 2022/4/23
  75. */
  76. public static ListNode str2Node(String s) {
  77. String[] split = s.split(",");
  78. ListNode listNode = new ListNode();
  79. ListNode p = listNode;
  80. for (String val : split) {
  81. if (val == null || val == "") {
  82. continue;
  83. }
  84. ListNode node = new ListNode(Integer.valueOf(val));
  85. p.next = node;
  86. p = p.next;
  87. }
  88. return listNode.next;
  89. }
  90. }
  91. }

6、剑指 Offer 25. 合并两个排序的链表(中等)

题目

[

](https://leetcode-cn.com/problems/three-steps-problem-lcci/)

思路

代码

7、剑指 Offer 16. 数值的整数次方(中等)

题目

[

](https://leetcode-cn.com/problems/three-steps-problem-lcci/)

思路

代码

8、面试题 08.05. 递归乘法(中等)

题目

[

](https://leetcode-cn.com/problems/three-steps-problem-lcci/)

思路

代码

[

](https://leetcode-cn.com/problems/fei-bo-na-qi-shu-lie-lcof/)