题目
题解
Map + Map
class Solution {
public List topStudents(String[] positive_feedback, String[] negative_feedback, String[] report, int[] student_id, int k) {
// 将分数放到 Map 中
Map score = new HashMap();
for (String s : positive_feedback) {
score.put(s, 3);
}
for (String s : negative_feedback) {
score.put(s, -1);
}
Map idMap = new HashMap();
for (int i = 0; i {
if (Objects.equals(o1.getValue(), o2.getValue())) {
return o1.getKey() - o2.getKey();
}
return o2.getValue() - o1.getValue();
}).map(Map.Entry::getKey)
.limit(k)
.collect(Collectors.toList());
}
}
Map + 二维数组
class Solution {
public List topStudents(String[] positive_feedback, String[] negative_feedback, String[] report, int[] student_id, int k) {
// 将分数放在 Map
Map score = new HashMap();
for (String s : positive_feedback) {
score.put(s, 3);
}
for (String s : negative_feedback) {
score.put(s, -1);
}
int[] scores = new int[student_id.length];
int[][] idArrs = new int[student_id.length][2];
for (int i = 0; i {
if (o1[0] == o2[0]) {
return o1[1] - o2[1];
}
return o2[0] - o1[0];
});
ArrayList res = new ArrayList(k);
for (int i = 0; i < k; i++) {
res.add(idArrs[i][1]);
}
return res;
}
}
本文由mdnice多平台发布