从0开始刷剑指Offer

剑指Offer题解

剑指 Offer 03. 数组中重复的数字

思路一:哈希表O(n)

class Solution {public int findRepeatDocument(int[] documents) {int len = documents.length;int[] arr = new int[100010];for(int i = 0; i < len; i ++ ) {arr[documents[i]] ++;if (arr[documents[i]] == 2) {return documents[i];}}return 0;}
}

思路二: 原地交换O(n)

class Solution {public int findRepeatDocument(int[] documents) {int len = documents.length;for(int i = 0; i < len; i ++ ){while (documents[i] != i) {if(documents[i] == documents[documents[i]]) {return documents[i];}int t = documents[i];documents[i] =  documents[t];documents[t] = t;}}return -1;}
}

剑指 Offer 04. 二维数组中的查找

思路一:单调性扫描O(n+m)

class Solution {public boolean findTargetIn2DPlants(int[][] plants, int target) {int len1 = plants.length;if (len1 <= 0) return false;int len2 = plants[0].length;if (len2 <= 0) return false;for(int i = len1 - 1, j = 0; ;) {if(plants[i][j] <target) {j ++;} else if(plants[i][j] > target){i --;} else {return true;}if(i < 0 || j >= len2) {break;}}return false;}
}

思路二:二分O(nlogn)

class Solution {public boolean findTargetIn2DPlants(int[][] plants, int target) {int len1 = plants.length;if (len1 <= 0) return false;int len2 = plants[0].length;if (len2 <= 0) return false;for(int i = 0; i < len1; i ++ ) {if (midf(plants, i, target)) {return true;}}return false;}public boolean midf(int[][] plants, int i, int target) {int l = 0;int r = plants[i].length - 1;while(l < r) {int mid = l + r >> 1;if(plants[i][mid] >= target) {r = mid;} else {l = mid + 1;}}if(plants[i][l] == target) return true;return false;}
}

剑指 Offer 05. 替换空格

思路一:遍历添加O(n)

class Solution {public String pathEncryption(String path) {StringBuilder sb = new StringBuilder();char[] c = path.toCharArray();for(char ct : c) {if(ct == '.') sb.append(' ');else sb.append(ct);}return sb.toString();}
}

思路二:正则表达式替换O(n)

class Solution {public String pathEncryption(String path) {return path.replaceAll("\\.", " ");}
}

剑指 Offer 06. 从尾到头打印链表

思路一:辅助栈法O(n)

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public int[] reverseBookList(ListNode head) {Stack<Integer> stack = new Stack();int[] q;ListNode dummy = head;int length = 0;while(dummy != null) {stack.push(dummy.val);dummy = dummy.next;length ++;}q = new int[length];for(int i = 0; i < length; i++ ) {q[i] = stack.pop();}return q;}
}

思路二:递归法O(n)

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {ArrayList<Integer> temp = new ArrayList();public int[] reverseBookList(ListNode head) {recur(head);  int[] res = new int[temp.size()];for(int i = 0; i < res.length; i ++) {res[i] = temp.get(i);}     return res;}void recur(ListNode head) {if(head == null) return;recur(head.next);temp.add(head.val);}
}

思路三:遍历反转法O(n)

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {ArrayList<Integer> list = new ArrayList();public int[] reverseBookList(ListNode head) {int length = 0;ListNode dummy = head;while(dummy != null) {list.add(dummy.val);dummy = dummy.next;length ++;}Collections.reverse(list);int[] q = new int[list.size()];for(int i = 0; i < q.length; i ++) {q[i] = list.get(i);}return q;}
}

剑指 Offer 07. 重建二叉树

思路一:递归法O(n)

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {int[] preorder;HashMap<Integer, Integer> hmap = new HashMap();public TreeNode deduceTree(int[] preorder, int[] inorder) {this.preorder = preorder;for(int i = 0; i < inorder.length; i ++ ) {hmap.put(inorder[i], i);}return recur(0, 0, inorder.length - 1);}TreeNode recur(int root, int left, int right) {if(left > right) return null;TreeNode node = new TreeNode(preorder[root]);int i = hmap.get(preorder[root]);node.left = recur(root + 1, left, i - 1);node.right = recur(root + i - left + 1, i + 1, right);return node;}
}

剑指 Offer 08. 二叉树的下一个结点

思路一:中序遍历法O(n)

import java.util.*;
public class Solution {ArrayList<TreeLinkNode> nodes = new ArrayList<>();public TreeLinkNode GetNext(TreeLinkNode pNode) {// 获取根节点TreeLinkNode root = pNode;while(root.next != null) root = root.next;// 中序遍历打造nodesInOrder(root);// 进行匹配int n = nodes.size();for(int i = 0; i < n - 1; i++) {TreeLinkNode cur = nodes.get(i);if(pNode == cur) {return nodes.get(i+1);}}return null;}// 中序遍历void InOrder(TreeLinkNode root) {if(root != null) {InOrder(root.left);nodes.add(root);InOrder(root.right);}}
}

思路二:分类直接查找法O(n)

import java.util.*;
public class Solution {public TreeLinkNode GetNext(TreeLinkNode pNode) {// 情况一if(pNode.right != null) {TreeLinkNode rchild = pNode.right;// 一直找到右子树的最左下的结点为返回值while(rchild.left != null) rchild = rchild.left; return rchild;}// 情况二if(pNode.next != null && pNode.next.left == pNode) {return pNode.next;}// 情况三if(pNode.next != null) {TreeLinkNode ppar = pNode.next;// 沿着左上一直爬树,爬到当前结点是其父节点的左自己结点为止while(ppar.next != null && ppar.next.right == ppar) ppar = ppar.next; return ppar.next;}return null;}
}

剑指 Offer 09. 用两个栈实现队列

思路:栈模拟O(n)

class CQueue {Stack<Integer> stack1;Stack<Integer> stack2;public CQueue() {stack1 = new Stack();stack2 = new Stack();}public void appendTail(int value) {stack1.push(value);}public int deleteHead() {if (stack2.isEmpty()) {while(!stack1.isEmpty()) {stack2.push(stack1.pop());}}return stack2.isEmpty() ? -1 : stack2.pop();}
}/*** Your CQueue object will be instantiated and called as such:* CQueue obj = new CQueue();* obj.appendTail(value);* int param_2 = obj.deleteHead();*/

剑指 Offer 10- I. 斐波那契数列

思路一:暴力递归O(2^n) 超出时间限制

class Solution {public int fib(int n) {if(n < 2) return n;return (fib(n - 1) + fib(n - 2)) % 1000000007;}
}

**思路二:记忆化递归O(n) **

class Solution {public int fib(int n) {if(n < 2) return n;int[] fib = new int[n + 1];fib[0] = 0;fib[1] = 1;for(int i = 2; i <= n; i ++) {fib[i] = (fib[i - 1] + fib[i - 2]) % 1000000007;}return fib[n];}
}

**思路三:动态规划O(n) **

class Solution {public int fib(int n) {if (n < 2) return n;int a = 0;int b = 1;int sum;for(int i = 0; i < n; i ++) {sum = (a + b) % 1000000007;a = b;b = sum;}// i =0 时, a就是fib[1]的值return a;}
}

剑指 Offer 10- II. 青蛙跳台阶问题

**思路一:记忆化递归O(n) **

class Solution {public int trainWays(int num) {if(num < 2) return 1;int[] fib = new int[num + 1];fib[0] = 1;fib[1] = 1;for(int i = 2; i <= num; i++ ){fib[i] = (fib[i - 1] + fib[i - 2]) % 1000000007;}return fib[num];}
}

思路二:动态规划O(n)

class Solution {public int trainWays(int num) {int a = 0;int b = 1;int sum;for(int i = 0; i < num; i ++) {sum = (a + b) % 1000000007;a = b;b = sum;}//i= 0时,b就是fib[1]的值,和上一题的区别就是num = 0时,有一种方法return b;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/238744.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【爬虫软件】孔夫子二手书采集

项目演示 孔网爬取图书信息 目录结构 [ |-- api-ms-win-core-synch-l1-2-0.dll, |-- api-ms-win-core-sysinfo-l1-1-0.dll, |-- api-ms-win-core-timezone-l1-1-0.dll, |-- api-ms-win-core-util-l1-1-0.dll, |-- api-ms-win-crt-conio-l1-1-0.dll, |-- api…

表格实现合并单元格

实现的效果 一、列合并 此需求的列合并比较简单, 直接使用el-table-column包括即可 <el-table-column align"center" sortable label"目标"><el-table-column prop"target1" sortable label"预设目标" /><el-table-c…

如何在Linux下搭建接口自动化测试平台

我们今天来学习一下在Linux下如何搭建基于HttpRunner开发的接口自动化测试平台吧&#xff01; 需要在Linux上提前准备的环境&#xff08;下面是本人搭建时的环境&#xff09;&#xff1a; 1&#xff0c;Python 3.6.8 2&#xff0c;MySQL 5.7 一&#xff1a;下载HttpRunner…

JMeter---JSON提取器

JMeter的JSON提取器是一个用于从JSON响应中提取数据的元件。它可以从JSON响应中提取特定字段的值&#xff0c;并将这些值用于后续的测试步骤。 使用JSON提取器的步骤如下&#xff1a; 添加一个HTTP请求&#xff0c;用于获取包含JSON响应的数据。 在HTTP请求之后添加一个JSON提…

JavaScript高级 构造函数与原型篇

构造函数与原型 1、构造函数 构造函数是一种特殊的函数&#xff0c;主要用来初始化对象&#xff0c;即为对象成员变量赋初始值&#xff0c;它总与new一起使用。我们可以把对象中一些公共的属性和方法抽取出来&#xff0c;然后封装到这个函数里面。 // 定义学生构造函数func…

面试遇到了接口分析和测试用例分析题,该如何下手?

只要有软件产品的公司百分之九十以上都会做接口测试&#xff0c;要做接口测试的公司那是少不了接口测试工程师的&#xff0c;接口测试工程师相对于其他的职位又比较轻松并且容易胜任。如果你想从事接口测试的工作那就少不了对接口进行分析&#xff0c;同时也会对测试用例进行研…

软件测试十大必问面试题(附答案和解析)

01 介绍之前负责的项目 参考答案&#xff1a;先大概描述一下这个项目是做什么的&#xff08;主要功能&#xff09;&#xff0c;包括哪些模块&#xff0c;是什么架构的&#xff08;B/S、C/S、移动端&#xff1f;&#xff09;&#xff0c;你在其中负责哪些模块的测试。期间经历了…

【排序算法】C语言实现选择排序与冒泡排序

文章目录 &#x1f680;前言&#x1f680;冒泡排序✈️冒泡排序的逻辑✈️冒泡排序coding &#x1f680;选择排序✈️选择排序的逻辑✈️选择排序coding &#x1f680;前言 这里是阿辉算法与数据结构专栏的第一篇文章&#xff0c;咱们就从排序算法开始讲起&#xff0c;排序算法…

【C++】揭开运算符重载的神秘面纱

目录 一、引言 优点 二、介绍 1.定义 2.语法 三、示例 1.加法运算符重载 2.一元运算符重载 3.友元函数 4.流插入和流提取 5.自增自减运算符 总结 一、引言 何为运算符重载&#xff1f;运算符重载&#xff0c;是C中的一项强大特性&#xff0c;赋予了程序员在自定义类…

金蝶Apusic应用服务器 loadTree JNDI注入漏洞复现(QVD-2023-48297)

0x01 产品简介 金蝶Apusic应用服务器是一款企业级应用服务器,支持Java EE技术,适用于各种商业环境。 0x02 漏洞概述 由于金蝶Apusic应用服务器权限验证不当,导致攻击者可以向loadTree接口执行JNDI注入,造成远程代码执行漏洞。利用该漏洞需低版本JDK。(漏洞比较旧,8月份…

Java中如何优雅的根治null值引起的Bug问题

1. Java对象为null会引发的问题 NullPointerException&#xff1a;当你尝试调用或访问一个null对象的属性或方法时&#xff0c;Java会抛出NullPointerException异常。例如&#xff0c;如果你有一个名为person的变量&#xff0c;它被设置为null&#xff0c;然后你尝试调用perso…

测试框架|Burp Suite几个基本工具的使用

前阵子项目上想通过测试工具在网页上模拟返回错误代码 500 来查看页面的错误处理&#xff0c;然后去调查了下 burp suite&#xff0c;看了些基本工具的使用文档。虽然最后证实 burp suite 只能用来处理页面测试应用程序的实际行为和响应&#xff0c;而不是尝试模拟不存在的问题…

springboot学习笔记(五)

MybatisPlus进阶 1.MybatisPlus一对多查询 2.分页查询 1.MybatisPlus一对多查询 场景&#xff1a;我有一个表&#xff0c;里面填写的是用户的个人信息&#xff08;姓名&#xff0c;生日&#xff0c;密码&#xff0c;用户ID&#xff09;。我还有一个表填写的订单信息&#x…

4 postman响应数据解析

上一篇:3 使用postman批量创建测试数据-CSDN博客 在接口测试中,从接口的响应结果中获取数据是很常用的。比如说做断言的时候,需要确保接口返回数据是符合预期的。又比如有些接口的输入参数值,需要用到前面接口运行返回的数据。下面先介绍如何解析响应数据(以json数…

持续集成交付CICD:GitLabCI 封装Python类 并结合 ArgoCD 完成前端项目应用发布

目录 一、实验 1. 环境 2. Python代码实现获取文件 3.Python代码实现创建文件 4.Python代码实现更新文件 5.GitLab更新库文件与运行流水线 6.ArgoCD 完成前端项目应用发布 二、问题 1.Python获取GitLab指定仓库文件报错 2. K8S master节点运行Python代码报错 一、实验…

小程序分享携带参数,被覆盖问题

场景&#xff1a; 子组件中写了‘onShareAppMessage’和‘onShareTimeLine’&#xff0c;父组件中也写了这两个函数&#xff0c;并且在url中携带了参数&#xff0c;发现分享后在分享页‘onLoad’中取不到参数。 // 分享目标页 onLoad(async (props: any) > {const { share…

Java日志框架Logback

logback.xml文件配置(放在src下微服务建议放在resources下) <?xml version"1.0" encoding"UTF-8"?> <configuration><!--定义日志文件的存储地址,使用绝对路径--><property name"LOG_HOME" value"d:/logs"/>…

文件上传——后端

文件上传流程&#xff1a; 创建阿里云OSS&#xff08;对象存储服务&#xff09;的bucket 登录阿里云&#xff0c;并完成实名认证&#xff0c;地址&#xff1a;https://www.aliyun.com/. 可以通过搜索&#xff0c;进入以下页面&#xff1a; 点击立即使用后&#xff1a; 点击…

Goby 漏洞发布| 金蝶 EAS createDataSource 路径 jndiName 参数远程代码执行漏洞

漏洞名称&#xff1a;Apusic 应用服务器 createDataSource 远程代码执行漏洞 English Name&#xff1a;Kingdee EAS createDataSource path jndiName parameter remote code execution vulnerability CVSS core: 9.8 影响资产数&#xff1a; 26513 漏洞描述&#xff1a; 金…

[Linux] MySQL数据表(数据结构)管理

一、数据库 1.1 数据库的基本概念 数据库&#xff08;database&#xff09;是用来组织、存储和管理数据的仓库 数据库管理系统&#xff08;DBMS&#xff09;&#xff1a;是实现对数据有效组织&#xff0c;管理和存取的系统软件。 数据的建立和维护功能&#xff0c;数据定义…