T1.设计停车系统 题目大意:
停车场i有大中小三种车位若干个,设计一个停车系统,判断停车场是否能停那么多车?如果可以停的话就停进去并输出true,否则输出false。
题目分析:
水题,直接就三个属性走起。
题目答案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 class ParkingSystem { private int big; private int medium; private int small; public ParkingSystem (int big, int medium, int small) { this .big = big; this .medium = medium; this .small = small; } public boolean addCar (int carType) { if (carType == 1 ){ if (big>0 ){ big--; return true ; } }else if (carType == 2 ){ if (medium>0 ){ medium--; return true ; } }else { if (small>0 ){ small--; return true ; } } return false ; } }
T2.警告一小时内使用相同员工卡大于等于三次的人 题目大意:
有两个String数组,一个存储人名,另一个存储打卡时间(HH:mm)格式。
如果一个人在一个小时内打卡了三次,就会被警告。
返回去重 后的收到系统警告的员工名字,将它们按 字典序升序 排序后返回。
(请注意 "10:00"
- "11:00"
视为一个小时时间范围内,而 "23:51"
- "00:10"
不被视为一小时内)
题目分析:
用一个HashMap<String,List*String>来存储数据。以Name为Key,打卡时间的list为Value。
多用Collections.Sort()去排序。
题目答案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;class Solution { List<String> result; public List<String> alertNames (String[] keyName, String[] keyTime) { result = new ArrayList<>(); HashMap<String,List<String>> map = new HashMap<>(); for (int i=0 ;i<keyName.length;i++){ if (!map.containsKey(keyName[i])){ map.put(keyName[i],new ArrayList<>()); map.get(keyName[i]).add(keyTime[i]); }else { map.get(keyName[i]).add(keyTime[i]); } } for (Map.Entry<String,List<String>> entry : map.entrySet()) { if (vetify(entry.getValue())){ result.add(entry.getKey()); } } Collections.sort(result); return result; } public Boolean vetify (List<String> arr) { Collections.sort(arr); if (arr.size()<3 ) return false ; for (int i=2 ;i<arr.size();i++){ if (timediff(arr.get(i-2 ),arr.get(i))){ return true ; } } return false ; } public Boolean timediff (String time1,String time2) { int re1 = (int )time1.charAt(0 ); int re2 = (int )time2.charAt(0 ); if (re1>re2) return false ; DateFormat df = new SimpleDateFormat("HH:mm" ); long minutes = 0L ; try { Date d1 = df.parse(time1); Date d2 = df.parse(time2); long diff = Math.abs(d1.getTime() - d2.getTime()); minutes = diff / (1000 * 60 ); } catch (ParseException e) { System.out.println("error" ); } return minutes<=60 ; } }
T3.给定行和列的和求可行矩阵 题目大意:
要求:你不知道矩阵里的每个元素,但是你知道每一行和每一列的和。
请找到大小为 rowSum.length x colSum.length 的任意 非负整数 矩阵,且该矩阵满足要求。
请你返回任意一个满足题目要求的二维矩阵,题目保证存在 至少一个 可行矩阵。
题目分析:
贪心,两个for循环就搞定了。
题目答案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Solution { public int [][] restoreMatrix(int [] rowSum, int [] colSum) { int [][] result = new int [rowSum.length][colSum.length]; for (int i=0 ;i<rowSum.length;i++){ for (int j=0 ;j<colSum.length;j++){ int val = Math.min(rowSum[i],colSum[j]); rowSum[i] -= val; colSum[j] -= val; result[i][j] = val; } } return result; } }
T4.放弃