Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
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.