Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
AhkunTa
Arithmetic
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Arithmetic
/
LeetCode
/
287_javascript.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
47 lines (37 loc) · 1.73 KB
master
Breadcrumbs
Arithmetic
/
LeetCode
/
287_javascript.js
Copy path
Top
File metadata and controls
Code
Blame
47 lines (37 loc) · 1.73 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数。假设只有一个重复的整数,找出这个重复的数。
// 示例 1:
// 输入: [1,3,4,2,2]
// 输出: 2
// 示例 2:
// 输入: [3,1,3,4,2]
// 输出: 3
// 说明:
// 不能更改原数组(假设数组是只读的)。
// 只能使用额外的 O(1) 的空间。
// 时间复杂度小于 O(n2) 。
// 数组中只有一个重复的数字,但它可能不止重复出现一次。
// 原数组不是有序,但是我们知道重复的那个数字肯定是 1 到 n 中的某一个,而 1,2...,n 就是一个有序序列。因此我们可以对 1,2...,n 进行二分查找。
// mid = (1 + n) / 2,接下来判断最终答案是在 [1, mid] 中还是在 [mid + 1, n] 中。
// 我们只需要统计原数组中小于等于 mid 的个数,记为 count。
// 如果 count > mid ,鸽巢原理,在 [1,mid] 范围内的数字个数超过了 mid ,所以一定有一个重复数字。
// 否则的话,既然不在 [1,mid] ,那么最终答案一定在 [mid + 1, n] 中。
/**
* @param {number[]} nums
* @return {number}
*/
var findDuplicate = (nums) => {
let left = 1, right = nums.length - 1 // 数组项的范围 1 到 n
while (left < right) { // 在循环中缩小区间,区间闭合循环结束
let mid = (left + right) >> 1 // 求中位数
let count = 0
for (let i = 0; i < nums.length; i++) {
if (nums[i] <= mid) count++ // 统计小于等于mid的个数
}
if (count > mid) { // 重复数落在 [1,mid]
right = mid // 区间收缩
} else { // 落在 [mid+1,n]
left = mid + 1 // 区间收缩
}
}
return left
}
You can’t perform that action at this time.