当前位置:网站首页>To Offer | 03. Repeat Numbers in the array
To Offer | 03. Repeat Numbers in the array
2022-08-04 22:19:00 【Lonely Wild Crane】
The sword refers to Offer 03. Repeated numbers in an array - LeetCode(LeetCode)https://leetcode.cn/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof/Open a pit today and brush a wave of sword offers (although I have brushed a little bit...
Method 1: Make use of the features of HashSet that can be quickly searched, put elements into HashSet and compare.
class Solution {public int findRepeatNumber(int[] nums) {HashSet integers = new HashSet<>();for (int i = 0; i < nums.length; i++) {if (integers.contains(nums[i])) {return nums[i];}integers.add(nums[i]);}return -1;}}
Method 2: Subscript method, constantly exchange elements so that the elements are the same as their corresponding subscripts until a conflict occurs.
class Solution {public int findRepeatNumber(int[] nums) {// loop through the arrayfor(int i = 0; i < nums.length; i++) {// The reason for using while is because after the swap, the element at that position is still not in the correct positionwhile(i != nums[i]){if(nums[i] == nums[nums[i]]){return nums[i];}// nums[i] is in the correct position at nums[nums[i]]int k = nums[nums[i]];nums[nums[i]] = nums[i];nums[i] = k;}}return -1;}}
边栏推荐
猜你喜欢
Why is MySQL query slow?
Is the International Project Manager PMP certificate worth taking?
Deep Learning RNN Architecture Analysis
3D激光SLAM:LeGO-LOAM---两步优化的帧间里程计及代码分析
得不到你的心,就用“分布式锁”锁住你的人
Unknown point cloud structure file conversion requirements
LeetCode143:重排链表
驱动点云格式修改带来的效率提升
用Tesseract开发一个你自己的文字识别应用
Ts——项目实战应用enum枚举
随机推荐
3D激光SLAM:LeGO-LOAM---两步优化的帧间里程计及代码分析
[Linear Algebra 02] 2 interpretations of AX=b and 5 perspectives of matrix multiplication
Leaflets of three bouquet of roses
论文解读(PPNP)《Predict then Propagate: Graph Neural Networks meet Personalized PageRank》
后排乘客不系安全带?事故瞬间被甩出
Exploration and Practice of Database Governance
驱动点云格式修改带来的效率提升
BUG | 接口返回异常数据
【论文笔记KDD2021】MixGCF: An Improved Training Method for Graph Neural Network-based Recommender Systems
ES6高级-async的用法
快速web开发框架——learun framework
Rt-thread [二] 系统初始化流程
Several ways for rk3399 to drive screen parameters
Qiangwang Cup 2022 - WEB
老叶的三束玫瑰
一招包治pycharm DEBUG报错 UnicodeDecodeError: ‘utf-8‘ codec can‘t decode
遍历await方法的区别:以for和forEach为例
Redisson
Latex fast insert author ORCID
[larave]关于laravel使用form submit()不能获取值问题