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
Events
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 }}
chenwenhang
AlgorithmPractice
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
AlgorithmPractice
/
src
/
array_and_matrix
/
RelativeSortArray.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
80 lines (55 loc) · 1.7 KB
master
Breadcrumbs
AlgorithmPractice
/
src
/
array_and_matrix
/
RelativeSortArray.java
Copy path
Top
File metadata and controls
Code
Blame
80 lines (55 loc) · 1.7 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
package array_and_matrix;
/**
* @Author: Wenhang Chen
* @Description:给你两个数组,arr1 和 arr2,
* <p>
* arr2 中的元素各不相同
* arr2 中的每个元素都出现在 arr1 中
* 对 arr1 中的元素进行排序,使 arr1 中项的相对顺序和 arr2 中的相对顺序相同。未在 arr2 中出现过的元素需要按照升序放在 arr1 的末尾。
* <p>
*
* <p>
* 示例:
* <p>
* 输入:arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]
* 输出:[2,2,2,1,4,3,3,9,6,7,19]
*
* <p>
* 提示:
* <p>
* arr1.length, arr2.length <= 1000
* 0 <= arr1[i], arr2[i] <= 1000
* arr2 中的元素 arr2[i] 各不相同
* arr2 中的每个元素 arr2[i] 都出现在 arr1 中
* @Date: Created in 9:11 2/14/2020
* @Modified by:
*/
public class RelativeSortArray {
// 典型的空间换时间
// python中in的操作也许可以用这种方式解决
public int[] relativeSortArray(int[] arr1, int[] arr2) {
int[] res = new int[arr1.length];
// m[i]存储数字i出现的次数
int[] m = new int[1001];
// 存进m的过程本身就是使数字有序的过程
for (int i = 0; i < arr1.length; i++) {
m[arr1[i]]++;
}
int cnt = 0;
for (int i = 0; i < arr2.length; i++) {
while (m[arr2[i]] > 0) {
res[cnt++] = arr2[i];
m[arr2[i]]--;
}
}
// 最后把m中剩下的元素加进res
for (int i = 0; i < 1001; i++) {
while (m[i] > 0) {
// 注意添加的是i
res[cnt++] = i;
m[i]--;
}
}
return res;
}
}
You can’t perform that action at this time.