当前位置:网站首页>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;}}边栏推荐
- 三个多月、40余场面试浓缩为6000字
- 未知点云结构文件转换需求
- com.jacob.com.ComFailException: Invoke of: ActiveDocument
- 一招包治pycharm DEBUG报错 UnicodeDecodeError: ‘utf-8‘ codec can‘t decode
- torch单机多卡和多机多卡训练
- Milvus configuration related
- 剑指Offer | 数值的整数次方
- idea 仓库地址连接不上问题
- [Linear Algebra 03] Elimination method display and 4 solutions of AX=b
- 《剑指offer》刷题分类
猜你喜欢
随机推荐
【TCP/IP 五 ICMP】
Hardware factors such as CPU, memory, and graphics card also affect the performance of your deep learning model
PowerBI scripture series
限制tensorflow使用Cpu核数
Webmine Webpage Mining Trojan Analysis and Disposal
基于事实的讨论
【Social Marketing】WhatsApp Business API: Everything You Need to Know
Excel商业智能-Power BI电商数据分析实战
中大型商业银行堡垒机升级改造方案!必看!
如何在项目中正确使用WebSocket
移动web开发03
Oracle使用expdp和impdp导出导入数据
剑指Offer | 数值的整数次方
JVM内存配置参数GC日志
七夕,当爱神丘比特遇上牛郎和织女
# #ifndef/#define/#endif使用详解
边缘检测——(纯享版)
【模拟面试-10年工作】项目多一定是优势吗?
Analysis and treatment of Ramnit infectious virus
后排乘客不系安全带?事故瞬间被甩出






![单片机原理[一] 学好单片机必会的五张图](/img/65/cac34bee5470ae85288d4366d8d957.jpg)

