当前位置:网站首页>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
边栏推荐
- mysql中添加字段的相关问题
- The BP neural network based on MATLAB voice characteristic signal classification
- 字符中的第一个唯一字符
- 微信小程序接口调用凭证(获取token)auth.getAccessToken接口开发
- Robot_Framework: commonly used built-in keywords
- 用位运算为你的程序加速
- 【FiddlerScript】利用FiddlerScript抓包保利威下载
- 基于MATLAB的BP神经网络进行语音特征信号分类
- 权重等比分配
- Selenium: element judgment
猜你喜欢

matlab 风速模型 小波滤波

可视化全链路日志追踪

太厉害了,终于有人能把文件上传漏洞讲的明明白白了

「面经分享」西北大学 | 字节 生活服务 | 一面二面三面 HR 面

Information system project managers must recite the work of the core test site (56) Configuration Control Board (CCB)

Dialogue with the father of MySQL: One excellent programmer is worth 5 ordinary programmers

mysql的行锁和间隙锁

Using FiddlerScript caught poly FiddlerScript 】 【 download

matlab simulink 粒子群优化模糊pid控制的电机泵

crypto-js uses
随机推荐
curl (7) Failed connect to localhost8080; Connection refused
对于升级go1.18的goland问题
Information system project managers must recite the work of the core test site (56) Configuration Control Board (CCB)
Dialogue with the father of MySQL: One excellent programmer is worth 5 ordinary programmers
Introduction to the basic principles, implementation and problem solving of crawler
Selenium: Dropdown Box Actions
Using FiddlerScript caught poly FiddlerScript 】 【 download
Robot_Framework: Assertion
Selenium: form switching
第5章——以程序方式处理MySQL数据表的数据
史上超强最常用SQL语句大全
LeetCode每日一题(309. Best Time to Buy and Sell Stock with Cooldown)
小白的0基础教程SQL: 什么是SQL 01
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
字符中的第一个唯一字符
Induction jian hai JustFE 2022/07/29 team, I learned the efficient development summary (years)
The Bean's life cycle
Practical training Navicat Chinese and English mode switching
微信小程序用户登录auth.code2Session接口开发
The BP neural network based on MATLAB voice characteristic signal classification




