虾皮笔试
虾皮笔试全a 还在注释里埋了个彩蛋 哈哈 身为菜鸡的我第一次这么开心🥳
import java.util.*; public class Main{ public static void main(String[] args) { Main main = new Main(); String[] trans = new String[]{"alice,20,800,sz","alice,50,100,sg"}; main.invalidTransaction(trans); } public String[] invalidTransaction (String[] vTransaction) { // write code here HashMap<String,ArrayList<Trans>> table = new HashMap<>(); for(String trans : vTransaction) { System.out.println(trans); String[] items = trans.split(","); System.out.println(Arrays.toString(items)); int time = Integer.parseInt(items[1]); int price = Integer.parseInt(items[2]); Trans t = new Trans(time,price,items[3]); ArrayList<Trans> list = table.get(items[0]); if( list == null) { list = new ArrayList<Trans>(); } list.add(t); table.put(items[0],list); } List<String> ans = new ArrayList<>(); System.out.println(table); for(String name : table.keySet()) { ArrayList<Trans> list = table.get(name); list.sort(new Com()); int n = list.size(); boolean needAdd2 = false; for(int i = 0; i < n;){ if(i + 1 < n) { Trans t1 = list.get(i); Trans t2 = list.get(i+1); if(t2.time - t1.time <= 60 &&!t2.city.equals(t1.city)) { ans.add(name + t1.toString()); ans.add(name + t2.toString()); needAdd2 = true; } } if(needAdd2) { i+= 2; } else { Trans t = list.get(i); if(t.price > 1000) ans.add(name + t.toString()); i += 1; } } } int size = ans.size(); String[] ret = new String[size]; for(int i = 0; i < size; i++) ret[i] = ans.get(i); System.out.println(Arrays.toString(ret)); return ret; } } class Trans { int time; int price; String city; public Trans(int t,int p,String c){ time = t; price = p; city = c; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append(",").append(time).append(",").append(price).append(",").append(city); return sb.toString(); } } class Com implements Comparator<Trans>{ public int compare(Trans a,Trans b) { if(a.time == b.time) return a.price - b.price; else return a.time - b.time; } }