当前位置:网站首页>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]
}
}
};
边栏推荐
- Chrome浏览器的crash问题
- Computer cleaning, deleted system files
- R language ggplot2 visualization: place the title of the visualization image in the upper left corner of the image (customize Title position in top left of ggplot2 graph)
- Roguelike游戏成破解重灾区,如何破局?
- 2022.02.13 - NC004. Print number of loops
- After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
- The mysqlbinlog command uses
- China dihydrolaurenol market forecast and investment strategy report (2022 Edition)
- Revit 二次开发 HOF 方式调用transaction
- Sublime text using ctrl+b to run another program without closing other runs
猜你喜欢

【ROS】usb_ Cam camera calibration

swagger设置字段required必填

Screenshot in win10 system, win+prtsc save location

ROS compilation calls the third-party dynamic library (xxx.so)

【嵌入式】Cortex M4F DSP库

pcd转ply后在meshlab无法打开,提示 Error details: Unespected eof

pytorch训练好的模型在加载和保存过程中的问题

Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school

查看局域网中电脑设备

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
随机推荐
How to conduct interface test? What are the precautions? Nanny level interpretation
R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
Sort according to a number in a string in a column of CSV file
China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
ROS compilation calls the third-party dynamic library (xxx.so)
【MySQL】鎖
JS inheritance method
【ROS】usb_ Cam camera calibration
延迟初始化和密封类
[MySQL] log
Purpose of computer F1-F12
What are the common processes of software stress testing? Professional software test reports issued by companies to share
China vanadium battery Market Research and future prospects report (2022 Edition)
[cloud native topic -45]:kubesphere cloud Governance - Introduction and overall architecture of enterprise container platform based on kubernetes
Function coritization
Visual implementation and inspection of visdom
[brush questions] top101 must be brushed in the interview of niuke.com
Golang force buckle leetcode 1020 Number of enclaves
Esp8266-rtos IOT development
win10系统中的截图,win+prtSc保存位置