-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelative-sort-array.cs
38 lines (35 loc) · 1.15 KB
/
relative-sort-array.cs
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
// https://leetcode.com/problems/relative-sort-array/
public class Solution {
public int[] RelativeSortArray(int[] arr1, int[] arr2) {
int index = 0;
int lastI = -1;
for(int i=0;i<arr1.Length && index<arr2.Length;i++){
if(arr1[i]==arr2[index]) continue;
else{
for(int j=i+1;j<arr1.Length;j++){
if(arr1[j]==arr2[index]){
int temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
while(arr1[i]==arr2[index]) i++;
}
}
if(arr1[i]!=arr2[index]) i--;
index++;
}
lastI=i;
}
if(lastI<arr1.Length-1 && index>=arr2.Length){
for(int i=lastI+1;i<arr1.Length;i++){
for(int j=i+1;j<arr1.Length;j++){
if(arr1[j]<arr1[i]){
int temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
}
}
}
}
return arr1;
}
}