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
/
922_javascript.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
70 lines (51 loc) · 1.8 KB
master
Breadcrumbs
Arithmetic
/
LeetCode
/
922_javascript.js
Copy path
Top
File metadata and controls
Code
Blame
70 lines (51 loc) · 1.8 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// 给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。
// 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。
// 你可以返回任何满足上述条件的数组作为答案。
// 示例:
// 输入:[4,2,5,7]
// 输出:[4,5,2,7]
// 解释:[4,7,2,5],[2,5,4,7],[2,7,4,5] 也会被接受。
/**
* @param {number[]} A
* @return {number[]}
*/
// 双指针
// 时间 O(n)
// 空间 O(n)
var sortArrayByParityII = function(A) {
let oddIndex= 1;evenIndex = 0
let result = [];
for(let i=0; i<A.length; i++){
if(A[i] % 2 == 0){
result[evenIndex] = A[i];
evenIndex +=2;
}else {
result[oddIndex] = A[i];
oddIndex +=2;
}
}
return result;
};
// 不开辟额外空间
var sortArrayByParityII = function(A) {
let odd = 1;
// 通过偶数下标来遍历
for (let even = 0; even < A.length; even += 2) {
// 1.如果为偶数,则进入下一次循环,检查下一个偶数下标的值是否为偶数
// 2.如果非偶数,就用odd找到一个奇数下标的值为偶数来交换
if (A[even] % 2 != 0) {
// 偶数位出现不为偶数,说明奇数位肯定能找到偶数
// 2.1 跳过奇数,找到一个奇数位为偶数值的下标
while (A[odd] % 2 == 1) {
odd += 2;
}
// 2.2 奇数位找到偶数了,将偶数位的奇数和奇数位的偶数的值交换
let tmp = A[even];
A[even] = A[odd];
A[odd] = tmp;
// 奇数下标后移
odd += 2;
}
}
return A;
}
You can’t perform that action at this time.