![](https://img-blog.csdnimg.cn/20201014180756930.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
剑指offer
ALEN.Z
这个作者很懒,什么都没留下…
展开
-
剑指 Offer 10- I. 斐波那契数列
目录题目分析代码题目分析用递归就错了,肯定回超出时间限制,用迭代会更好代码//超出时间限制class Solution { public int fib(int n) { if (n<2) { return n; } int first = fib(n - 1) % 1000000007; int second = fib(n - 2) % 1000000007; return (first + second) % 1000000原创 2020-09-13 11:48:09 · 44 阅读 · 0 评论 -
剑指 Offer 09. 用两个栈实现队列
目录题目分析代码题目分析用两个栈实现队列,一个用来加,一个用来删代码class CQueue { LinkedList<Integer> stack1; LinkedList<Integer> stack2; public CQueue() { //用来增加 stack1 = new LinkedList<Integer>(); //用来删除 stack2 = new LinkedList<In原创 2020-09-12 17:46:01 · 49 阅读 · 0 评论 -
剑指 Offer 06. 从尾到头打印链表
目录题目分析代码题目分析使用栈的先进后出原则,学习链表的取值,迭代代码class Solution { public int[] reversePrint(ListNode head) { Stack<Integer> stack = new Stack<Integer>(); ListNode p = head; while (p!=null) { stack.add(p.val); p = p.next; }原创 2020-09-12 15:04:50 · 47 阅读 · 0 评论 -
剑指 Offer 05. 替换空格
目录题目分析代码题目分析两个方法:1.直接调用库函数,对字符串中的空格进行替换2.遍历字符,对空格进行替换代码//方法一:class Solution { public String replaceSpace(String s) { return s.replace(" ", "%20"); }}//方法二:额外占用空间class Solution { public String replaceSpace(String s) {原创 2020-09-11 11:31:13 · 46 阅读 · 0 评论 -
剑指 Offer 04. 二维数组中的查找
目录题目分析代码题目分析从右上角开始查找,对有序数组,要敏感代码class Solution { public boolean findNumberIn2DArray(int[][] matrix, int target) { if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { return false; } //右上角开始二分查找原创 2020-09-10 12:37:01 · 69 阅读 · 0 评论 -
剑指offer 3.数组中重复的数字
目录题目分析代码题目分析方法:遍历数组,存储到哈希表或数组中,判断哈希表值或数组值代码//时间复杂度为O(n),空间复杂度为O(n)class Solution { public int findRepeatNumber(int[] nums) { Set<Integer> set = new HashSet<Integer>(); int repeat = -1; for (int num : nums) {原创 2020-09-10 10:58:30 · 87 阅读 · 1 评论 -
剑指 Offer 1. 两数之和
目录)题目分析代码题目分析只用两层遍历即可得出结果代码class Solution { public int[] twoSum(int[] nums, int target) { int[] ans = new int[2]; for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums.length; j++) { if (nums[i] + nums[j] == targ原创 2020-09-09 23:13:22 · 95 阅读 · 0 评论 -
剑指 Offer 53 - II. 0~n-1中缺失的数字
目录题干分析代码题干分析两种方法进行求解,1、顺序求解 2、二分查找(因为是顺序数组)代码package JianZhi;public class JianZhi053 { public static void main(String[] args) { int[] arr = new int[] {0,1,2,3,4,5,6,7,9}; Solution2 solution = new Solution2(); System.out.println(solution.m原创 2020-09-09 22:30:57 · 43 阅读 · 0 评论