跟谁学0923笔试

1、并查集2、交叉链表求交点

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Main3 {
    static int find(int[] p, int x) {
        if(p[x] != x) {
            p[x] = find(p,p[x]); // 路径压缩
        }
        return p[x];
    }
    static List<List<String>> mergeAccount(List<List<String>> accounts) {
        // 在此处编写实现代码逻辑
        List<List<String>> res = new ArrayList<>();
        int n = accounts.size();
        int[] p = new int[n]; // 祖宗节点
        for(int i=0; i < n; i++) p[i] = i;
        Map<String,List<Integer>> map = new HashMap<>();
        for(int i=0; i < n; i++) {
            for(int j=1; j < accounts.get(i).size(); j++) {
                String mail = accounts.get(i).get(j);
                if(map.containsKey(mail)) {
                    map.get(mail).add(i);
                } else {
                    List<Integer> list = new ArrayList<>();
                    list.add(i);
                    map.put(mail, list);
                }
            }
        }
        for(String mail : map.keySet()) {
            List<Integer> list = map.get(mail);
            //System.out.println(list);
            if(list.size() >= 2) {
                int parent = find(p,list.get(0));
                for(int i=0; i < list.size(); i++) {
                    p[find(p, list.get(i))] = parent;
                }
            }
        }
        List<Set<String>> temp = new ArrayList<>();
        for(int i=0; i < n; i++) {
            temp.add(new HashSet<String>());
        }
        for(int i=0; i < n; i++) {
            int parent = find(p, i);
            for(int j=1;  j < accounts.get(i).size(); j++) {
                temp.get(parent).add(accounts.get(i).get(j));
            }
        }
        for(int i=0; i < n; i++) {
            if(temp.get(i).size() > 0) {
                List<String> mails = new ArrayList<>(temp.get(i));
                //Collections.sort(mails);
                res.add(mails);
            }
        }
        return res;
    }
    /******************************结束写代码******************************/


    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int _count = Integer.parseInt(in.nextLine());

        int _i = 0;
        List<List<String>> _accounts = new ArrayList<List<String>>();
        while (_i++ < _count) {
            String _line = in.nextLine();
            String[] _item = _line.split(",");
            _accounts.add(Arrays.asList(_item));
        }

        List<List<String>> res = mergeAccount(_accounts);

        Collections.sort(res, new Comparator<List<String>>() {

            @Override
            public int compare(List<String> o1, List<String> o2) {
                String aName1 = String.join(",", o1);
                String aName2 = String.join(",", o2);
                return aName1.compareTo(aName2);
            }

        });

        for (int res_i = 0; res_i < res.size(); res_i++) {

            List<String> resItem = res.get(res_i);
            System.out.println(String.join(",", resItem));
        }
    }
}
/*
[

    ["John", "johnsmith@mail.com", "john00@mail.com"],

    ["John", "johnnybravo@mail.com"],

    ["John", "johnsmith@mail.com", "john_newyork@mail.com"],

    ["Mary", "mary@mail.com"]

]
4
"John", "johnsmith@mail.com", "john00@mail.com"
"John", "johnnybravo@mail.com"
"John", "johnsmith@mail.com", "john_newyork@mail.com"
"Mary", "mary@mail.com"
输出:
 "john_newyork@mail.com", "john00@mail.com", "johnsmith@mail.com"
 "johnnybravo@mail.com"
 "mary@mail.com"
* */

交叉单链表

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Main2 {

    public static class Node {

        private int data;
        private Node next;

        public int getData() {
            return data;
        }

        public void setData(int data) {
            this.data = data;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node next) {
            this.next = next;
        }
    }


    /*请完成下面这个函数,实现题目要求的功能
    当然,你也可以不按照下面这个模板来作答,完全按照自己的想法来 ^-^
    ******************************开始写代码******************************/
    static Node getCommon(Node headA, Node headB) {
        int len1 = 0, len2 = 0;
        Node p = headA, q = headB;
        while(p!=null) {
            len1++;
            p = p.next;
        }
        p = headB;
        while(p!=null) {
            len2++;
            p = p.next;
        }
        p = headA; q = headB;
        if(len1 < len2) {
            int n = len2 - len1;
            for(int i=0; i < n; i++) q = q.next;
        }
        if(len1 > len2) {
            int n = len1 - len2;
            for(int i=0; i < n; i++) p = p.next;
        }
        while(p != null && q != null) {
            if(p == q) return p;
            p = p.next; q = q.next;
        }
        return null;
    }
    /******************************结束写代码******************************/


    public static void main(String[] args){
        Scanner in = new Scanner(System.in);

        String line1 = in.nextLine();
        String line2 = in.nextLine();

        Node headA = null;
        Node headB = null;

        Map<String, Node> map = new HashMap<String, Node>();

        if (line1 != null && line1.length() > 0) {
            String[] array1 = line1.split(",");
            headA = new Node();
            headA.setData(Integer.parseInt(array1[0].split("\\(")[0]));
            map.put(array1[0], headA);
            Node pre = headA;
            for (int i = 1; i < array1.length; i++) {
                Node node = map.get(array1[i]);
                if (node == null) {
                    node = new Node();
                    node.setData(Integer.parseInt(array1[i].split("\\(")[0]));
                    map.put(array1[i], node);
                }
                pre.next = node;
                pre = node;
            }
        }
        if (line2 != null && line2.length() > 0) {
            String[] array2 = line2.split(",");
            headB = map.get(array2[0]);
            if (headB == null) {
                headB = new Node();
                headB.setData(Integer.parseInt(array2[0].split("\\(")[0]));
                map.put(array2[0], headB);
            }
            Node pre = headB;
            for (int i = 1; i < array2.length; i++) {
                Node node = map.get(array2[i]);
                if (node == null) {
                    node = new Node();
                    node.setData(Integer.parseInt(array2[i].split("\\(")[0]));
                    map.put(array2[i], node);
                }
                pre.next = node;
                pre = node;
            }
        }

        Node res = getCommon(headA, headB);
        System.out.println(res == null ? res : res.getData());
    }
}


posted @ 2020-09-26 21:34  li修远  阅读(113)  评论(0编辑  收藏  举报