721. Accounts Merge

Given a list accounts, each element accounts[i] is a list of strings, where the first element accounts[i][0] is a name, and the rest of the elements are emails representing emails of the account.

Now, we would like to merge these accounts. Two accounts definitely belong to the same person if there is some email that is common to both accounts. Note that even if two accounts have the same name, they may belong to different people as people could have the same name. A person can have any number of accounts initially, but all of their accounts definitely have the same name.

After merging the accounts, return the accounts in the following format: the first element of each account is the name, and the rest of the elements are emails in sorted order. The accounts themselves can be returned in any order.

Example 1:
Input: 
accounts = [["John", "johnsmith@mail.com", "john00@mail.com"], ["John", "johnnybravo@mail.com"], ["John", "johnsmith@mail.com", "john_newyork@mail.com"], ["Mary", "mary@mail.com"]]
Output: [["John", 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com'],  ["John", "johnnybravo@mail.com"], ["Mary", "mary@mail.com"]]
Explanation: 
The first and third John's are the same person as they have the common email "johnsmith@mail.com".
The second John and Mary are different people as none of their email addresses are used by other accounts.
We could return these lists in any order, for example the answer [['Mary', 'mary@mail.com'], ['John', 'johnnybravo@mail.com'], 
['John', 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com']] would still be accepted.
Note:

The length of accounts will be in the range [1, 1000].
The length of accounts[i] will be in the range [1, 10].
The length of accounts[i][j] will be in the range [1, 30].

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/accounts-merge
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

用并集,把所有属于同一个人的email都关联起来,再和email的owner组合。

import java.util.Map.Entry;
class Solution {
    public List<List<String>> accountsMerge(List<List<String>> accounts) {
        Map<String, String> root = new HashMap<>();
        Map<String, String> owner = new HashMap<>();
        Map<String, Set<String>> emailUnion = new HashMap<>();
        // root放的是当前email和它同属一个owner的另外一个email的对应关系。先初始化为自身。
        // 如果两个点属于相同的组,就将其中一个点的 root 值赋值为另一个点的位置,这样只要是相同组里的两点,通过 find 函数得到相同的值。
        
        // owner是每个emial对应的owner。
        for (List<String> list : accounts) {
            for (int i = 1; i < list.size(); i++) {
                root.put(list.get(i), list.get(i));
                owner.put(list.get(i), list.get(0));
            }
        }

        // 第一个遍历,从给定的accounts中取出一个list,然后取出一个email,让这个list中的其他所有的email,都在email的map中跟这个对应起来。
        // 把所有的email属于同一个owner的,都关联起来。
        for (List<String> list : accounts) {
            String pattern = find(list.get(1), root);
            for (int i = 1; i < list.size(); i++) {
                root.put(find(list.get(i), root), pattern);
            }
        }

        // emailUnion,key值是其中一个email,value值是跟他相同owner的所有email的set集合
        for (List<String> list : accounts) {
            for (int i = 1; i < list.size(); i++) {
                String pattern = find(list.get(i), root);
                if (!emailUnion.containsKey(pattern)) {
                    emailUnion.put(pattern, new HashSet<>());
                }
                emailUnion.get(pattern).add(list.get(i));
            }
        }

        // 把emailUnion整理出来,在owner中找到名字。组成最后答案。
        List<List<String>>ans = new ArrayList<>();
        for (Entry<String, Set<String>>entry: emailUnion.entrySet()) {
            List<String> list = new ArrayList<>();
            list.add(owner.get(entry.getKey()));
            for (String value :entry.getValue()) {
                list.add(value);
            }
            Collections.sort(list);
            ans.add(list);
        }
        return ans;
    }

    String find(String email, Map<String, String> map) {
        if (map.get(email).compareTo(email) == 0) {
            return map.get(email);
        } else {
            return find(map.get(email), map);
        }
    }
}

 

内容概要:本文详细介绍了施耐德M580系列PLC的存储结构、系统硬件架构、上电写入程序及CPU冗余特性。在存储结构方面,涵盖拓扑寻址、Device DDT远程寻址以及寄存器寻址三种方式,详细解释了不同类型的寻址方法及其应用场景。系统硬件架构部分,阐述了最小系统的构建要素,包括CPU、机架和模块的选择与配置,并介绍了常见的系统拓扑结构,如简单的机架间拓扑和远程子站以太网菊花链等。上电写入程序环节,说明了通过USB和以太网两种接口进行程序下载的具体步骤,特别是针对初次下载时IP地址的设置方法。最后,CPU冗余部分重点描述了热备功能的实现机制,包括IP通讯地址配置和热备拓扑结构。 适合人群:从事工业自动化领域工作的技术人员,特别是对PLC编程及系统集成有一定了解的工程师。 使用场景及目标:①帮助工程师理解施耐德M580系列PLC的寻址机制,以便更好地进行模块配置和编程;②指导工程师完成最小系统的搭建,优化系统拓扑结构的设计;③提供详细的上电写入程序指南,确保程序下载顺利进行;④解释CPU冗余的实现方式,提高系统的稳定性和可靠性。 其他说明:文中还涉及一些特殊模块的功能介绍,如定时器事件和Modbus串口通讯模块,这些内容有助于用户深入了解M580系列PLC的高级应用。此外,附录部分提供了远程子站和热备冗余系统的实物图片,便于用户直观理解相关概念。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值