-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode2089.java
More file actions
35 lines (35 loc) · 974 Bytes
/
leetcode2089.java
File metadata and controls
35 lines (35 loc) · 974 Bytes
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
class Solution {
public List<Integer> targetIndices(int[] nums, int target) {
quickSort(nums,0,nums.length-1);
List<Integer> list=new ArrayList<>();
for(int i=0;i<nums.length;i++){
if(nums[i]==target){
list.add(i);
}
}
return list;
}
private void quickSort(int[] nums,int low,int high){
if(low<high){
int pivot=partition(nums,low,high);
quickSort(nums,low,pivot-1);
quickSort(nums,pivot+1,high);
}
}
private int partition(int[] nums,int low,int high){
int pivot=nums[high];
int i=low-1;
for(int j=low;j<high;j++){
if(nums[j]<pivot){
i++;
int temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
}
int temp=nums[i+1];
nums[i+1]=nums[high];
nums[high]=temp;
return i+1;
}
}