class Solution {
public int[] topKFrequent(int[] nums, int k) {
int n = nums.length;
int[] repeatNums = new int[n];// 每个值重复的次数
for(int i = 0;i < n; i++){
repeatNums[i] = 1;
}
//执行下段语句后,repeatNums[i]和nums[i]一一对应
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if( i != j){
if(nums[i] == nums[j]){
repeatNums[i]++;
}
}
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n - 1; j++){
if(repeatNums[j] < repeatNums[j + 1]){
int mid = repeatNums[i];
repeatNums[i] = repeatNums[j];
repeatNums[j] = mid;
int mid2 = nums[i];
nums[i] = nums[j];
nums[j] = mid;
}
}
}
return repeatNums;
输出每个数组每个数的重复次数(java)
最新推荐文章于 2022-04-09 14:56:24 发布