|
| 1 | +package problems.medium; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.TreeMap; |
| 6 | + |
| 7 | +/** |
| 8 | + * Created by sherxon on 7/1/17. |
| 9 | + */ |
| 10 | +public class LogSystem { |
| 11 | + |
| 12 | + |
| 13 | + TreeMap<Long, Integer> logs = new TreeMap<>(); |
| 14 | + |
| 15 | + public LogSystem() { |
| 16 | + |
| 17 | + } |
| 18 | + |
| 19 | + public static void main(String[] args) { |
| 20 | + LogSystem log = new LogSystem(); |
| 21 | + System.out.println(log.getDate("2004:09:23:19:31:46", "Year", false)); |
| 22 | + System.out.println(log.getDate("2003:12:12:20:30:51", "Year", false)); |
| 23 | + |
| 24 | + } |
| 25 | + |
| 26 | + public void put(int id, String timestamp) { |
| 27 | + logs.put(getDate(timestamp, "Second", false), id); |
| 28 | + } |
| 29 | + |
| 30 | + public List<Integer> retrieve(String s, String e, String gra) { |
| 31 | + |
| 32 | + long fromDay = getDate(s, gra, true); |
| 33 | + long toDay = getDate(e, gra, false); |
| 34 | + return new ArrayList<>(logs.subMap(fromDay, true, toDay, true).values()); |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + private long getDate(String s, String gra, boolean b) { |
| 40 | + int year = 0; |
| 41 | + int month = b ? 1 : 11; |
| 42 | + int day = b ? 1 : 31; |
| 43 | + int hour = b ? 0 : 23; |
| 44 | + int min = b ? 0 : 59; |
| 45 | + int sec = b ? 0 : 59; |
| 46 | + String to[] = s.split(":"); |
| 47 | + switch (gra) { |
| 48 | + case "Second": |
| 49 | + sec = Integer.parseInt(to[5]); |
| 50 | + case "Minute": |
| 51 | + min = Integer.parseInt(to[4]); |
| 52 | + case "Hour": |
| 53 | + hour = Integer.parseInt(to[3]); |
| 54 | + case "Day": |
| 55 | + day = Integer.parseInt(to[2]); |
| 56 | + case "Month": |
| 57 | + month = Integer.parseInt(to[1]) - 1; |
| 58 | + case "Year": |
| 59 | + year = Integer.parseInt(to[0]); |
| 60 | + } |
| 61 | + |
| 62 | + return year * 100000 + month * 10000 + day * 1000 + hour * 100 + min * 10 + sec; |
| 63 | + } |
| 64 | +} |
| 65 | + |
0 commit comments