当前位置:网站首页>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
边栏推荐
- Solve the problem of page flicker caused by browser scroll bars
- About making a progress bar for software initialization for Qt
- 微信小程序获取手机号phonenumber.getPhoneNumber接口开发
- 导致锁表的原因及解决方法
- 【MySQL必知必会】 表的优化 | 充分利用系统资源
- Bean的生命周期
- [Translation] Securing cloud-native communications: From ingress to service mesh and beyond
- Selenium: upload and download files
- 【翻译】确保云原生通信的安全:从入口到服务网及更远的地方
- 基于MATLAB的BP神经网络进行语音特征信号分类
猜你喜欢

Flip letters using string container

信息系统项目管理师必背核心考点(五十六)配置控制委员会(CCB)的工作

【FiddlerScript】利用FiddlerScript抓包保利威下载
![Explosive 30,000 words, the hardest core丨Mysql knowledge system, complete collection of commands [recommended collection]](/img/7f/08b323ffc5b5f8e3354bee6775b994.png)
Explosive 30,000 words, the hardest core丨Mysql knowledge system, complete collection of commands [recommended collection]

crypto-js使用

matlab 风速模型 小波滤波

AspNet.WebApi.Owin custom Token request parameters

目标检测概述-上篇

A,H,K,N

datagrip 报错 “The specified database userpassword combination is rejected...”的解决方法
随机推荐
Selenium: Popup Handling
小白的0基础教程SQL: 什么是SQL 01
Classwork (7) - #598. remainder operation (mod)
奇葩问题 npm install 报错 gyp ERR
Motion analysis and parameter optimization of crank-slider mechanism
Selenium: mouse, keyboard events
导致锁表的原因及解决方法
信息系统项目管理师必背核心考点(五十六)配置控制委员会(CCB)的工作
Explosive 30,000 words, the hardest core丨Mysql knowledge system, complete collection of commands [recommended collection]
Does flinkcdc have any solution for mysql's date field type conversion?
dbeaver连接MySQL数据库及错误Connection refusedconnect处理
Hunan institute of technology in 2022 ACM training sixth week antithesis
【FiddlerScript】利用FiddlerScript抓包保利威下载
Qt Widget 项目对qml的加载实例
滚动条样式修改
湖仓一体电商项目(一):项目背景和架构介绍
微信小程序获取手机号phonenumber.getPhoneNumber接口开发
点餐系统数据库设计--SQL Server
Selenium: upload and download files
爬虫基本原理介绍、实现以及问题解决




