第一题:https://leetcode-cn.com/problems/find-lucky-integer-in-an-array/
AC代码,时间问题就随便搞了个方法:
class Solution {
public static int findLucky(int[] arr) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
if(map.containsKey(arr[i])) {
map.put(arr[i], map.get(arr[i]) + 1);
} else {
map.put(arr[i], 1);
}
}
int res = -1;
for (Map.Entry<Integer, Integer> entry: map.entrySet()) {
if(entry.getKey() == entry.getValue()) {
res = res > entry.getKey() ? res : entry.getKey();
}
}
return res;
}
}
第二题:https://leetcode-cn.com/problems/count-number-of-teams/
更直接了,直接暴力:
public int numTeams(int[] rating) {
int res = 0;
for (int i = 0; i < rating.length - 2; i++) {
for (int j = i + 1; j < rating.length - 1; j++) {
for (int k = j + 1; k < rating.length; k++) {
if (rating[i] > rating[j] && rating[j] > rating[k] || rating[i] < rating[j] && rating[j] < rating[k]) {
res ++;
}
}
}
}
return res;
}
第三题:https://leetcode-cn.com/problems/design-underground-system/
写的有点蠢:
package leetcode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class UndergroundSystem {
private Map<Integer, Pair> map1;
private Map<Pair2, ArrayList<Integer>> map2;
public UndergroundSystem() {
map1 = new HashMap<>();
map2 = new HashMap<>();
}
public void checkIn(int id, String stationName, int t) {
map1.put(id, new Pair(t, stationName));
}
public void checkOut(int id, String stationName, int t) {
Pair2 pair2 = new Pair2(map1.get(id).getName(), stationName);
if (map2.containsKey(pair2)) {
map2.get(pair2).add(t - map1.get(id).getId());
} else {
ArrayList<Integer> temp = new ArrayList<>();
temp.add(t - map1.get(id).getId());
map2.put(pair2, temp);
}
map1.remove(id);
}
public double getAverageTime(String startStation, String endStation) {
Pair2 pair2 = new Pair2(startStation, endStation);
double res = 0;
ArrayList<Integer> arr = map2.get(pair2);
for (int i = 0; i < arr.size(); i++) {
res += arr.get(i);
}
return res / arr.size();
}
class Pair2 {
private String s1;
private String s2;
public Pair2(String s1, String s2) {
this.s1 = s1;
this.s2 = s2;
}
public String getS1() {
return s1;
}
public String getS2() {
return s2;
}
@Override
public int hashCode() {
String s3 = s1 + s2;
return s1.hashCode() + s2.hashCode() + s3.hashCode();
}
@Override
public boolean equals(Object obj) {
return s1.equals(((Pair2) obj).getS1()) && s2.equals(((Pair2) obj).getS2());
}
}
class Pair {
private int time;
private String name;
public Pair(int time, String name) {
this.time = time;
this.name = name;
}
public int getId() {
return time;
}
public String getName() {
return name;
}
}
}
第四题:https://leetcode-cn.com/problems/find-all-good-strings/
第四题没有做出来,还是挺难的,最后没有通过的代码也发出来吧!!!
写的很烂,情况都没有想清楚!
public static int findGoodStrings(int n, String s1, String s2, String evil) {
// 所有的
if (!compare(s1, s2)) {
return 0;
}
int common = 0;
for (int i = 0; i < n; i++) {
if (s1.charAt(i) != s2.charAt(i)) {
common = i;
break;
}
}
int res = 1;
List<Integer> list = new ArrayList<>();
for (int i = 0; i < s1.length(); i++) {
list.add(1);
}
for (int i = common; i < n; i++) {
int a = s2.charAt(i) - s1.charAt(i) + 1;
list.set(i, a);
}
// not
int temp = 1;
for (int i = 0; i < s1.length() - evil.length(); i++) {
temp = 1;
if (compare(s1.substring(i, i + evil.length()), evil) && compare(evil, s2.substring(i, i + evil.length()))) {
for (int j = 0; j < list.size(); j++) {
if (j >= 0 && j < i || j > i + evil.length()) {
temp *= list.get(j);
temp %= 100000007;
} else if (list.get(j) != 0){
temp *= list.get(j);
temp %= 100000007;
}
}
res -= temp;
}
}
return res;
}
private static boolean compare(String s1, String s2) {
for (int i = 0; i < s1.length(); i++) {
if(s1.charAt(i) > s2.charAt(i)) {
return false;
}
}
return true;
}
总结:一共就参加了两次,做题会很急,解法没有仔细思考,有个方法就用上,过了的三题完全没有技巧,最后一题想用分类讨论的思路做,各种情况还没有考虑到,哎呀,要走的路还很长啊!
排名800+。。。。。
先记录一下,还要复盘一下。