java
// 这是我乱搞的答案
class Solution {
public int minimumRounds(int[] tasks) {
int size = tasks.length + 1;
int[] mid = new int[10000001];
for(int i : tasks) {
mid[i % mid.length]++;
}
int res = 0;
// System.out.println(mid[2]);
for(int x : mid) {
if(x == 1) {
return -1;
}
if(x % 3 == 0) {
res += (x/3);
}else if(x % 3 == 1) {
res += (x/3);
res++;
}else if(x % 3 == 2) {
res += (x/3);
res++;
}
}
return res;
}
}
//
public class Solution {
public int minimumRounds(int[] tasks) {
// 使用HashMap来统计每个难度级别的任务数量
Map<Integer, Integer> taskCounts = new HashMap<>();
for (int task : tasks) {
taskCounts.put(task, taskCounts.getOrDefault(task, 0) + 1);
}
// 将HashMap中的元素转移到一个列表中,并根据任务数量对列表进行排序
List<Map.Entry<Integer, Integer>> sortedEntries = new ArrayList<>(taskCounts.entrySet());
sortedEntries.sort((a, b) -> b.getValue() - a.getValue());
int rounds = 0;
// 遍历排序后的列表
for (Map.Entry<Integer, Integer> entry : sortedEntries) {
int count = entry.getValue();
// 如果任务数量为1,则无法完成,返回-1
if (count == 1) {
return -1;
}
// 计算完成当前难度级别任务所需的轮数
rounds += (count + 2) / 3;
}
return rounds;
}
}
javapublic class Solution {
public int[] topKFrequent(int[] nums, int k) {
// 使用HashMap来记录每个元素出现的频率
Map<Integer, Integer> frequencyMap = new HashMap<>();
for (int num : nums) {
frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
}
// 创建一个最小堆来存储元素及其频率
PriorityQueue<Map.Entry<Integer, Integer>> minHeap = new PriorityQueue<>(
Map.Entry.comparingByValue()
);
// 遍历HashMap,将元素及其频率添加到最小堆中
for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
minHeap.offer(entry);
if (minHeap.size() > k) {
minHeap.poll(); // 如果堆的大小超过k,移除堆顶元素
}
}
// 从最小堆中取出所有元素,这些元素就是出现频率前k高的元素
int[] result = new int[k];
for (int i = 0; i < k; i++) {
result[i] = minHeap.poll().getKey();
}
return result;
}
}
javaclass Solution {
public char findTheDifference(String s, String t) {
int[] mid1 = new int[128];
int[] mid2 = new int[128];
char res = ' ';
for(char a : s.toCharArray()) {
mid1[a] ++;
}
for(char a : t.toCharArray()) {
//
mid2[a] ++;
}
for(int i = 0;i < 128;i++) {
if(mid1[i] != mid2[i]) {
res = (char)i;
}
}
return res;
}
}
public class Solution {
public char findTheDifference(String s, String t) {
int[] charCount = new int[26];
// 计算s中每个字母的出现次数
for (int i = 0; i < s.length(); i++) {
charCount[s.charAt(i) - 'a']++;
}
// 减去t中每个字母的出现次数
for (int i = 0; i < t.length(); i++) {
charCount[t.charAt(i) - 'a']--;
}
// 找到第一个计数不为0的字母
for (int i = 0; i < charCount.length; i++) {
if (charCount[i] != 0) {
return (char) (i + 'a');
}
}
return ' '; // 如果没有找到,返回一个空格字符
}
}
本文作者:yowayimono
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!