有一批订单记录,数据有订单号,入店时间,离店时间;
输入一个时间值A,需要在这批记录中找到符合入离店时间范围(A大于等于入店时间,并且A小于等于离店时间)内的所有记录。 单次查询时间复杂度控制在O(logN)
※注意:订单号升序输出
记录数:10
时间值A:20180602
订单号 入店时间 离店时间
1001 20180103 20180105
1002 20180202 20180203
1003 20180304 20180306
1004 20180401 20180408
1005 20180501 20180504
1006 20180601 20180604
1007 20180705 20180706
1008 20180801 20180804
1009 20180903 20180903
1010 20181003 20181003
以上输入都为整型
1006
10 20180602 1001 20180103 20180105 1002 20180202 20180203 1003 20180304 20180306 1004 20180401 20180408 1005 20180501 20180504 1006 20180601 20180604 1007 20180705 20180706 1008 20180801 20180804 1009 20180903 20180903 1010 20181003 20181003
1006
5 20170103 1013 20180103 20180105 1022 20180102 20180103 1103 20180104 20180106 1034 20180101 20180102 1105 20180201 20180204
null
查不到时输出null字符串(小写)
4 20180103 1013 20180103 20180105 1022 20180102 20180103 1026 20180103 20180103 1007 20180101 20180109
1007 1013 1022 1026
/* 思路:把入店和离店时间转成int,然后与给定的时间进行比较,符合条件的存入集合 ※注意:订单号升序输出,sort方法 */ import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; public class Main{ public static void main(String[] args) throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int timeA = Integer.parseInt(br.readLine()); //int count = 0; ArrayList<Integer> list = new ArrayList<>(); for(int i = 0;i < n;i++){ String[] str = br.readLine().split(" "); int orderid = Integer.parseInt(str[0]); int intime = Integer.parseInt(str[1]); int outtime = Integer.parseInt(str[2]); if(timeA >= intime && timeA <= outtime){ list.add(orderid); } } if(list.isEmpty()) System.out.println("null"); else{ Collections.sort(list); for(Object obj:list) System.out.println(obj); } } }
import java.util.Scanner; import java.util.TreeSet; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int target = sc.nextInt(); TreeSet<Integer> ans = new TreeSet<>(); while (t-- > 0){ int n = sc.nextInt(); if (sc.nextInt() <= target & sc.nextInt() >= target)//注意不能使用短路与 ans.add(n); } if (ans.isEmpty()) System.out.println("null"); else for (int i: ans) System.out.println(i); } }
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { public static void main(String[] args) { //输入数据 Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int A=sc.nextInt(); ArrayList<Integer> list=new ArrayList<>(); int[][] data=new int[n][3]; for (int i = 0; i <n ; i++) { for (int j = 0; j <3; j++) { data[i][j]=sc.nextInt(); } if(data[i][1]<=A&&data[i][2]>=A){ list.add(data[i][0]); } } if(list.size()==0){ System.out.println("null");//没有的话也是要输出结果的!以字符串的形式输出! return; } //接下来给动态数组排序 Collections.sort(list);//疑问?是降序还是升序? for (int i = 0; i <list.size() ; i++) { System.out.println(list.get(i)); } } }
import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; /** * @Author: Lee * @Date: 2019/8/8 23:00 * @Description: */ public class Main { public void process() { Scanner input = new Scanner(System.in); int orderNum = input.nextInt(); int orderTime = input.nextInt(); /** * 使用List数组存储订单信息,然后使用Java8中的Lambda表达式筛选输入数据 */ List<DataDetail> dataDetails = new ArrayList<DataDetail>(); for (int i = 0; i < orderNum; i++) { DataDetail detail = new DataDetail(); detail.setId(input.nextInt()); detail.setTimeIn(input.nextInt()); detail.setTimeOut(input.nextInt()); dataDetails.add(detail); } List<Integer> collect = dataDetails.stream() .filter(d -> d.getTimeIn() <= orderTime) .filter(dataDetail -> dataDetail.getTimeOut() >= orderTime) .map(DataDetail::getId) .sorted().collect(Collectors.toList()); if (collect.size() == 0) { System.out.println("null"); return; } collect.stream().forEach(d -> System.out.println(d + " ")); } public static void main(String[] args) { new Main().process(); } } /** * 用于存储订单数据 */ class DataDetail { private int id; private int timeIn; private int timeOut; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getTimeIn() { return timeIn; } public void setTimeIn(int timeIn) { this.timeIn = timeIn; } public int getTimeOut() { return timeOut; } public void setTimeOut(int timeOut) { this.timeOut = timeOut; } public DataDetail() { } public DataDetail(int id, int timeIn, int timeOut) { this.id = id; this.timeIn = timeIn; this.timeOut = timeOut; } }