当前位置:网站首页>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
边栏推荐
- LeetCode 0150. Reverse Polish Expression Evaluation
- Guest brush SQL - 2
- LeetCode 0149. 直线上最多的点数
- "By sharing" northwestern university life service | | bytes a second interview on three sides by HR
- Jupyter shortcuts
- Qt Widget 项目对qml的加载实例
- Qt Widget project loading example of qml
- 深度比较两个对象是否相同
- Data organization -- singly linked list of the linear table
- 对话MySQL之父:一个优秀程序员可抵5个普通程序员
猜你喜欢
随机推荐
ORACLE 实现另外一个用户修改包(package)
Selenium: Introduction
深度比较两个对象是否相同
crypto-js uses
JS的运行原理
AspNet.WebApi.Owin custom Token request parameters
基于MATLAB的BP神经网络进行语音特征信号分类
MVVM project development (commodity management system 1)
MySQL row locks and gap locks
return;代表含义
权重等比分配
响应式织梦模板园林花卉类网站
Data organization -- singly linked list of the linear table
Jupyter shortcuts
crypto-js使用
leetcode125 Verify palindrome string
实战演练 Navicat 中英文模式切换
【MySQL必知必会】 表的优化 | 充分利用系统资源
第5章——以程序方式处理MySQL数据表的数据
Hunan institute of technology in 2022 ACM training sixth week antithesis














