1、剑指 Offer 10- I. 斐波那契数列(简单)
题目
https://leetcode-cn.com/problems/fei-bo-na-qi-shu-lie-lcof/
思路
代码
public class Offer10 {/*** 缓存* */public static final int[] MEMORY = new int[101];public static int fib(int n) {if (n==0) {return 0;}if (n==1) {return 1;}// 判断缓存if (MEMORY[n] != 0) {return MEMORY[n];}MEMORY[n] = (fib(n-1)+fib(n-2)) % 1000000007;return MEMORY[n];}public static void main(String[] args) {System.out.println(fib(50));}}
2、剑指 Offer 10- II. 青蛙跳台阶问题(简单)
题目
https://leetcode-cn.com/problems/qing-wa-tiao-tai-jie-wen-ti-lcof/
思路
代码
public class OfferTwo10 {/*** 缓存* */public static final int[] MEMORY = new int[101];public static int numWays(int n) {if (n == 0) return 1;if (n == 1) return 1;if (n == 2) return 2;if (MEMORY[n] != 0) {return MEMORY[n];}// 题目要求取模,不然算出来可能是负数的。int mod = (numWays(n - 1) + numWays(n - 2)) % 1000000007;MEMORY[n] = mod;return MEMORY[n];}public static void main(String[] args) {System.out.println(numWays(0));}}
3、面试题 08.01. 三步问题(简单)
题目
https://leetcode-cn.com/problems/three-steps-problem-lcci/
思路
按正常两阶递归增加一阶逻辑。但是算出来一直是负数。原因是int越界了。
代码
public class Interview0801 {/*** 缓存* */public static final int[] MEMORY = new int[1000001];public static int waysToStep(int n) {if (n==0) return 1;if (n==1) return 1;if (n==2) return 2;if (MEMORY[n] != 0) {return MEMORY[n];}// 第一次提交的时候 int mod = (waysToStep(n - 1) + waysToStep(n - 2) + waysToStep(n - 3) % 1000000007)// 发现结果成了负数了。int mod = ((waysToStep(n - 1) + waysToStep(n - 2)) % 1000000007 + waysToStep(n - 3)) % 1000000007;MEMORY[n] = mod;return MEMORY[n];}public static void main(String[] args) {System.out.println(waysToStep(1000));}}
4、剑指 Offer 06. 从尾到头打印链表(简单)
题目
https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/
思路
每次都是打印当前节点+后续节点所有的值。
注意点
本地跑main的时候因为方法和list都被加上了static 关键字。在提交的时候出现了这种情况:
测试用例:[]
测试结果:[2,3,1]
期望结果:[]
代码
import java.util.ArrayList;import java.util.Arrays;import java.util.List;public class Offer06 {private List<Integer> resultList = new ArrayList<>();public int[] reversePrint(ListNode head) {// 当前为尾节点,返回空数组if (head == null) {return resultList.stream().mapToInt(Integer::intValue).toArray();}// 递归调用reversePrint(head.next);// 因为归的时候实际上就是相当于先进后出,所以将结果加到数组最后面即可。resultList.add(head.val);// 类型转换return resultList.stream().mapToInt(Integer::intValue).toArray();}// public static void main(String[] args) {// ListNode node1 = new ListNode(1);// ListNode node2 = new ListNode(2, node1);// ListNode node3 = new ListNode(3, node2);// ListNode node4 = new ListNode(1, node3);// int[] ints = reversePrint(node4);// for (int anInt : ints) {// System.out.println(anInt);// }// }public static class ListNode {int val;ListNode next;ListNode() {}ListNode(int val) { this.val = val; }ListNode(int val, ListNode next) { this.val = val; this.next = next; }}}
5、剑指 Offer 24. 反转链表(简单)
题目
https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/
思路
专项练习用递归实现,正常思路直接循环遍历节点之后采用头插法。
递的话主要就是传递下一个节点之后的链表。
归的话主要是假设有2个节点需要反转怎么弄。
然后发现3、4、5…个节点也能这样实现。
注意点在于遍历链表的时候定义一个指针会比较方便想明白。
代码
import java.util.Arrays;/*** 剑指 Offer 24. 反转链表(简单)* 题目* https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/** @author Altman* @date 2022/4/23*/public class Offer24 {public static ListNode reverseList(ListNode head) {// 空节点if (head == null) {return null;}// 意味着只有一个节点,返回新的链表if (head.next == null) {return new ListNode(head.val);}ListNode newList = reverseList(head.next);// 这里指定一个指针,靠指针的移动来表示当前指向哪一个节点。ListNode p = newList;// 遍历链表,将该节点的值按照尾插法。while (newList != null) {// 当前节点指向 null 就可以尾插节点了if (p.next == null) {ListNode newNode = new ListNode(head.val);p.next = newNode;break;}p = p.next;}return newList;}public static void main(String[] args) {ListNode node = ListNode.str2Node("5,4,3,2,1");System.out.println(ListNode.toString(reverseList(node)));}public static class ListNode {int val;ListNode next;ListNode() {}ListNode(int val) {this.val = val;}ListNode(int val, ListNode next) {this.val = val;this.next = next;}/*** 打印链表内容* @date 2022/4/23*/public static String toString(ListNode listNode) {StringBuilder sb = new StringBuilder();if (listNode == null) {return sb.toString();}sb.append(listNode.val);while (true) {if (listNode.next == null) {break;}sb.append(",");sb.append(listNode.next.val);listNode = listNode.next;}return sb.toString();}/*** string转链表(方便测试没有做过多校验)* @param s 逗号分割节点* @date 2022/4/23*/public static ListNode str2Node(String s) {String[] split = s.split(",");ListNode listNode = new ListNode();ListNode p = listNode;for (String val : split) {if (val == null || val == "") {continue;}ListNode node = new ListNode(Integer.valueOf(val));p.next = node;p = p.next;}return listNode.next;}}}
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/)
