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
/
75_javascript.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
53 lines (37 loc) · 1.46 KB
master
Breadcrumbs
Arithmetic
/
LeetCode
/
75_javascript.js
Copy path
Top
File metadata and controls
Code
Blame
53 lines (37 loc) · 1.46 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
48
49
50
51
52
53
// 给定一个包含红色、白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。
// 此题中,我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。
// 注意:
// 不能使用代码库中的排序函数来解决这道题。
// 示例:
// 输入: [2,0,2,1,1,0]
// 输出: [0,0,1,1,2,2]
// 进阶:
// 一个直观的解决方案是使用计数排序的两趟扫描算法。
// 首先,迭代计算出0、1 和 2 元素的个数,然后按照0、1、2的排序,重写当前数组。
// 你能想出一个仅使用常数空间的一趟扫描算法吗?
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
// 双指针 设置left right 两个指针
var sortColors = function(nums) {
let left = 0;
let right = nums.length - 1;
// 起始点在 [left, right],注意 right 是动态的
// right最理想的值为数字2 的长度
for (let i = left; i <= right; i++) {
// 如果是 0,交换到左边
if (nums[i] === 0) {
[nums[left], nums[i]] = [nums[i], nums[left]];
left++;
} else if (nums[i] === 2) {
[nums[right], nums[i]] = [nums[i], nums[right]];
right--;
// 当为2 时 将i-- 以便再次循环
// 有可能 nums[i] = nums[right] = 2 这种情况
i--;
}
}
// 4. 返回结果
return nums;
};
You can’t perform that action at this time.