当前位置:网站首页>LeetCode:剑指 Offer 03. 数组中重复的数字
LeetCode:剑指 Offer 03. 数组中重复的数字
2022-07-06 08:44:00 【Bertil】
找出数组中重复的数字。
在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。
示例 1:
输入:
[2, 3, 1, 0, 2, 5, 3]
输出:2 或 3
限制:
- 2 <= n <= 100000
解题思路
1.首先对数组进行排序,然后遍历数组,如果出现相邻元素相等,则返回即可
代码
/** * @param {number[]} nums * @return {number} */
var findRepeatNumber = function(nums) {
nums.sort()
for(let i = 0; i < nums.length - 1; i++){
if(nums[i] === nums[i + 1]) {
return nums[i]
}
}
};
边栏推荐
- After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
- 704 binary search
- R language uses the principal function of psych package to perform principal component analysis on the specified data set. PCA performs data dimensionality reduction (input as correlation matrix), cus
- Rviz仿真时遇到机器人瞬间回到世界坐标原点的问题及可能原因
- MySQL learning records 12jdbc operation transactions
- Deep anatomy of C language -- C language keywords
- 深度剖析C语言数据在内存中的存储
- Verrouillage [MySQL]
- Crash problem of Chrome browser
- Tcp/ip protocol
猜你喜欢
JS native implementation shuttle box
Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
UnsupportedOperationException异常
[brush questions] top101 must be brushed in the interview of niuke.com
Esp8266-rtos IOT development
游戏解包的危害及资源加密的重要性
Sublime text using ctrl+b to run another program without closing other runs
vb.net 随窗口改变,缩放控件大小以及保持相对位置
2022.02.13 - NC001. Reverse linked list
Deep analysis of C language data storage in memory
随机推荐
Promise 在uniapp的简单使用
Shift Operators
被破解毁掉的国产游戏之光
个人电脑好用必备软件(使用过)
Double pointeur en langage C - - modèle classique
What are the common processes of software stress testing? Professional software test reports issued by companies to share
[brush questions] top101 must be brushed in the interview of niuke.com
C语言双指针——经典题型
JVM quick start
Hutool gracefully parses URL links and obtains parameters
PLT in Matplotlib tight_ layout()
Sublime text using ctrl+b to run another program without closing other runs
Visual implementation and inspection of visdom
C語言雙指針——經典題型
Trying to use is on a network resource that is unavailable
China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
2022.02.13 - NC003. Design LRU cache structure
TCP/IP协议
Variable length parameter
ROS compilation calls the third-party dynamic library (xxx.so)