三道面试题

//    有一个字符串它的构成是词+空格的组合,如“北京 杭州 杭州 北京”, 要求输入一个匹配模式(简单的以字符来写),
//     比如 aabb, 来判断该字符串是否符合该模式, 举个例子:
//    1. pattern = "abba", str="北京 杭州 杭州 北京" 返回 ture
//    2. pattern = "aabb", str="北京 杭州 杭州 北京" 返回 false
//    3. pattern = "baab", str="北京 杭州 杭州 北京" 返回 ture

 

 

public static void main(String[] args) {


    //str_list = str_list.stream().distinct().collect(Collectors.toList());

    String pattern = "abaa", str = "北京 杭州 杭州 北京";

    boolean b = judge(pattern, str);
    System.out.println("========>" + b);
}

public static boolean judge(String pattern, String str) {
    String[] strs = str.split(" ");
    List<String> str_list = new ArrayList(Arrays.asList(strs));


    List<Character> chs_list = new ArrayList();
    char[] chs = pattern.toCharArray();

    for (char c : chs) {
        chs_list.add(c);
    }

    if (str_list.size() != chs_list.size()) {
        return false;
    }

    Map<Character, String> map = new HashMap<>();
    for (int i = 0; i < chs_list.size(); i++) {
        if (map.containsKey(chs_list.get(i))) {
            if (map.get(chs[i]).equals(str_list.get(i))) {

            } else {
                return false;
            }
        } else {
            if(map.containsValue(str_list.get(i))){
                return false;
            }
            map.put(chs_list.get(i), str_list.get(i));
        }
    }
    return true;
}

 

 

 

public class TicketManager {

//    机场购买机票, 客户输入: 出发地, 结束地, 上机时间,  给出最低价的机票

    private List<Ticket> tickets =new ArrayList<>();

    void generateTicket(){
        for(int i =0;i<100;i++){
            if(i%5==0){
                tickets.add(new Ticket("杭州","北京","8:00",(Math.random()*1000)+1000));
            }else if(i%5==1){
                tickets.add(new Ticket("上海","北京","8:00",(Math.random()*1000)+1000));
            }else if(i%5==2){
                tickets.add(new Ticket("杭州","北京","9:00",(Math.random()*1000)+1000));
            }else if(i%5==3){
                tickets.add(new Ticket("上海","北京","9:00",(Math.random()*1000)+1000));
            }else {
                tickets.add(new Ticket("杭州","北京","9:00",(Math.random()*1000)));
            }
        }
    }

    public Ticket search(String from, String to, String time) {
        //请实现TODO

        List<Ticket> collect = tickets.stream()
                .filter(x -> x.getFrom().equals(from))
                .filter(x -> x.getTo().equals(to))
                .filter(x -> x.getTime().equals(time))
                .sorted((x, y) -> {
                    return new Double(x.getPrice() - y.getPrice()).intValue();
                })
                .collect(Collectors.toList());

        return collect.get(0);
    }

    public static void main(String[] args) {
        TicketManager ticketManager = new TicketManager();
        ticketManager.generateTicket();
        Ticket ticket = ticketManager.search("杭州","北京","9:00");
        System.out.println(ticket);
    }
}

class Ticket {
    private String from;
    private String to;
    private String time;
    private double price;

    @Override
    public String toString() {
        return "Ticket{" +
                "from='" + from + '\'' +
                ", to='" + to + '\'' +
                ", time='" + time + '\'' +
                ", price=" + price +
                '}';
    }

    public Ticket(String from, String to, String time, double price) {
        this.from = from;
        this.to = to;
        this.time = time;
        this.price = price;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}

 

 

 

//共计9个苹果,有2只猴子,一个猴子每次拿2个苹果,一个猴子每次拿3个苹果,
    // 如果剩余的苹果不够猴子每次拿的数量,则2只猴子停止拿苹果,请用java多线程模拟上面的描述,要求性能尽可能高效
    static List<Apple> init() {

        List<Apple> list = new ArrayList();
        for (int i = 0; i < 41000; i++) {
            list.add(new Apple(i + 1, "我是苹果" + (1 + i)));
        }
        return list;
    }

    public static void main(String[] args) {

        method1();

    }

    static void method1(){
        List<Apple> list = init();
        long start = System.currentTimeMillis();
        Thread t1 = new Thread(() -> {
            Monkey monkey = new Monkey(1, "金丝猴", 30);
            while (monkey.takeApple(list)) {
                System.out.println(monkey.getName());
            }
            System.out.println("1结束!");
        });
        Thread t2 = new Thread(() -> {
            Monkey monkey = new Monkey(2, "马猴", 20);
            while (monkey.takeApple(list)) {
                System.out.println(monkey.getName());

            }
            System.out.println("2结束!");
        });

        t1.start();
        t2.start();
        try {
            t2.join();
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("执行完毕!耗时" + (end - start) + "毫秒,剩下" + list.size() + "个苹果");
    }

    static void method2(){
        List<Apple> list = init();
        long start = System.currentTimeMillis();
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        Monkey monkey1 = new Monkey(1, "金丝猴", 30, list);
        Monkey monkey2 = new Monkey(2, "马猴", 20, list);
        FutureTask<Boolean> result1 = new FutureTask<>(monkey1);
        FutureTask<Boolean> result2 = new FutureTask<>(monkey2);
        executorService.submit(result1);
        executorService.submit(result2);

        try {
            result1.get();
            result2.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("执行完毕!耗时" + (end - start) + "毫秒,剩下" + list.size() + "个苹果");
    }
}

class Monkey implements Callable {

    private int id;

    private String name;

    private int capacity;

    private List<Apple> list;

    public Monkey(int id, String name, int capacity) {
        this.id = id;
        this.name = name;
        this.capacity = capacity;
    }

    public Monkey(int id, String name, int capacity, List<Apple> list) {
        this.id = id;
        this.name = name;
        this.capacity = capacity;
        this.list = list;
    }

    public boolean takeApple(List<Apple> list) {

        synchronized (list) {

            if (list.size() < capacity) {
                return false;
            }
            for (int i = 0; i < capacity; i++) {
                list.remove(0);
            }
            return true;
        }
    }

    public Monkey(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    @Override
    public Object call() throws Exception {
        while (takeApple(list)) {
            System.out.println(this.getName());
        }
        return 0;
    }
}

class Apple {

    private int id;
    private String name;

    public Apple(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值