当前位置:网站首页>Offer brush questions - 1
Offer brush questions - 1
2022-08-01 06:32:00 【Curry won't shoot 3-pointers】
目录
剑指 Offer 03. 数组中重复的数字
class Solution { public int findRepeatNumber(int[] nums) { Map<Integer,Integer> map=new HashMap<>(); for (int i = 0; i <nums.length; i++) { if (!map.containsKey(nums[i])){ map.put(nums[i],1); }else { map.put(nums[i],map.get(nums[i])+1); } } for (Map.Entry<Integer,Integer> entry:map.entrySet()) { if (entry.getValue()>1){ return entry.getKey(); } } return -1; } }
- My idea is to use onehashmap来存储
- One point to note is thatmap的遍历方式,用Entryto traverse in this formMap的元素
- 也可以用Set,因为其Set的唯一性,If it is encountered, it will directly return to this finger
剑指 Offer 04. 二维数组中的查找
class Solution { public boolean findNumberIn2DArray(int[][] matrix, int target) { if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { return false; } int rows=0; int columns=matrix[0].length-1; if (target>matrix[matrix.length-1][columns]||target<matrix[0][0]){ return false; }else { return helper(matrix,rows,columns,target);//从左上角开始递归 } } private boolean helper(int[][] matrix, int rows, int columns,int target) { if (rows<0||rows>matrix.length-1||columns<0||columns>matrix[0].length){ return false; } if (matrix[rows][columns]==target){ return true; }else if(matrix[rows][columns]>target){ return helper(matrix,rows,columns-1,target); }else { return helper(matrix,rows+1,columns,target); } } }
剑指 Offer 05. 替换空格
class Solution { public String replaceSpace(String s) { return s.replace(" ", "%20") ; } }
- 大概思路,创建一个新字符串,Add any spaces%20,If other characters are encountered,就直接添加
剑指 Offer 06. 从尾到头打印链表
stack solution
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public int[] reversePrint(ListNode head) { LinkedList<Integer> stack=new LinkedList<>(); while (head!=null){ stack.addLast(head.val); head=head.next; } int arr[]=new int[stack.size()]; for (int i = 0; i < arr.length; i++) { arr[i]=stack.removeLast(); } return arr; } }递归解法
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { LinkedList<Integer> list=new LinkedList<>(); public int[] reversePrint(ListNode head) { helper(head); int arr[]=new int[list.size()]; for (int i = 0; i <list.size(); i++) { arr[i]=list.get(i); } return arr; } private void helper(ListNode head) { if (head==null) return; helper(head.next); list.add(head.val); } }
- From this question we can target,In fact, recursion can be implemented using stacks,Recursion is essentially a stack
边栏推荐
- 爆肝3万字,最硬核丨Mysql 知识体系、命令全集 【建议收藏 】
- AspNet.WebApi.Owin custom Token request parameters
- 从离线到实时对客,湖仓一体释放全量数据价值
- matplotlib pyplot
- 企业员工人事管理系统(数据库课设)
- Why is the lightweight VsCode used more and more?Why eat my C drive 10G?How to Painlessly Clean VsCode Cache?Teach you how to lose weight for C drive
- 旋度(7)连接失败localhost8080;连接拒绝了
- 点餐系统数据库设计--SQL Server
- weight distribution
- About making a progress bar for software initialization for Qt
猜你喜欢
随机推荐
太厉害了,终于有人能把文件上传漏洞讲的明明白白了
Matlab simulink particle swarm optimization fuzzy pid control motor pump
Guest brush SQL - 2
Bean的生命周期
安装SQL Server详细教程
权重等比分配
The BP neural network based on MATLAB voice characteristic signal classification
第6章——数据库的安全性
爆肝3万字,最硬核丨Mysql 知识体系、命令全集 【建议收藏 】
dbeaver连接MySQL数据库及错误Connection refusedconnect处理
mysql中添加字段的相关问题
Malicious attacks on mobile applications surge by 500%
AspNet.WebApi.Owin custom Token request parameters
uva12326
Srping bean in the life cycle
史上超强最常用SQL语句大全
头歌MySQL数据库实训答案 有目录
【MySQL必知必会】 表的优化 | 充分利用系统资源
JS的运行原理
2022年牛客多校第四场补题














