提交 1241efad 编写于 作者: C chenguangjian.jk

test pass

上级 6f8f145f
class Solution {
public int[] frequencySort(int[] nums) {
import java.util.Scanner;
import java.util.*;
import java.util.Arrays;
class Main {
public static void main(String[] args) {
// 控制台输入
Scanner scanner = new Scanner(System.in);
String numsStr = scanner.nextLine();
int[] nums = new int[]{};
// 解析字符串,构造数组
numsStr = numsStr.replaceAll(" ","");
if(!"[]".equals(numsStr)){
String[] numsStrArr = numsStr.replaceAll("\\[", "").replaceAll("\\]", "").split("\\,");
nums = new int[numsStrArr.length];
for (int i = 0; i < numsStrArr.length; i++) {
nums[i] = Integer.parseInt(numsStrArr[i].trim());
}
}
scanner.close();
// 调用方法
int[] res = frequencySort(nums);
// 输出结果
System.out.println(Arrays.toString(res));
}
public static int[] frequencySort(int[] nums) {
Map<Integer, Integer> cnt = new HashMap<Integer, Integer>(); Map<Integer, Integer> cnt = new HashMap<Integer, Integer>();
for (int num : nums) { for (int num : nums) {
cnt.put(num, cnt.getOrDefault(num, 0) + 1); cnt.put(num, cnt.getOrDefault(num, 0) + 1);
...@@ -18,4 +54,6 @@ class Solution { ...@@ -18,4 +54,6 @@ class Solution {
} }
return nums; return nums;
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册