当前位置:网站首页>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;}}边栏推荐
猜你喜欢
随机推荐
Redis中的LRU算法
synchronized和ReentrantLock都很丝滑,因为他们都是可重入锁,一个线程多次拿锁也不会死锁,我们需要可重入
[Linear Algebra 03] Elimination method display and 4 solutions of AX=b
直播带货为农产品开拓销售渠道
Operations on std::vector
ANT1.7下载以及配置方法
Latex快速插入作者ORCID
得不到你的心,就用“分布式锁”锁住你的人
【C - 基本概念】
Oracle使用expdp和impdp导出导入数据
MySQL查询为啥慢了?
Domestic PMP certificate of gold content how
Qt面试题整理
Excel商业智能-Power BI电商数据分析实战
PowerBI scripture series
【论文笔记KDD2021】MixGCF: An Improved Training Method for Graph Neural Network-based Recommender Systems
Develop your own text recognition application with Tesseract
ES 数据聚合、数据同步、集群
【社媒营销】WhatsApp Business API:您需要知道的一切
[larave]关于laravel使用form submit()不能获取值问题









