leetcode 721. 賬戶合并(并查集)

給定一個列表 accounts,每個元素 accounts[i] 是一個字符串列表,其中第一個元素 accounts[i][0] 是 名稱 (name),其余元素是 emails 表示該賬戶的郵箱地址。

現在,我們想合并這些賬戶。如果兩個賬戶都有一些共同的郵箱地址,則兩個賬戶必定屬于同一個人。請注意,即使兩個賬戶具有相同的名稱,它們也可能屬于不同的人,因為人們可能具有相同的名稱。一個人最初可以擁有任意數量的賬戶,但其所有賬戶都具有相同的名稱。

合并賬戶后,按以下格式返回賬戶:每個賬戶的第一個元素是名稱,其余元素是按字符 ASCII 順序排列的郵箱地址。賬戶本身可以以任意順序返回。

示例 1:

輸入:
accounts = [[“John”, “johnsmith@mail.com”, “john00@mail.com”], [“John”, “johnnybravo@mail.com”], [“John”, “johnsmith@mail.com”, “john_newyork@mail.com”], [“Mary”, “mary@mail.com”]]
輸出:
[[“John”, ‘john00@mail.com’, ‘john_newyork@mail.com’, ‘johnsmith@mail.com’], [“John”, “johnnybravo@mail.com”], [“Mary”, “mary@mail.com”]]
解釋:
第一個和第三個 John 是同一個人,因為他們有共同的郵箱地址 “johnsmith@mail.com”。
第二個 John 和 Mary 是不同的人,因為他們的郵箱地址沒有被其他帳戶使用。
可以以任何順序返回這些列表,例如答案 [[‘Mary’,‘mary@mail.com’],[‘John’,‘johnnybravo@mail.com’],
[‘John’,‘john00@mail.com’,‘john_newyork@mail.com’,‘johnsmith@mail.com’]] 也是正確的。

代碼

class Solution {int[] fa;public void  init(){for(int i=0;i<fa.length;i++)fa[i]=i;}public int  find(int x){if(x!=fa[x])fa[x]=find(fa[x]);return fa[x];}public void   union(int x,int y){x=find(x);y=find(y);if(x==y) return;fa[x]=y;}public List<List<String>> accountsMerge(List<List<String>> accounts) {Map<String,Integer> index=new HashMap<>();Map<Integer,String> name=new HashMap<>();int inx=0;for(List<String> list:accounts){String accName=list.get(0);for(int i=1;i<list.size();i++)if(!index.containsKey(list.get(i))) {//為每個郵箱地址創建下標和姓名映射name.put(inx,accName);index.put(list.get(i), inx++);}}fa=new int[inx];init();for(List<String> list:accounts)//將具有相同郵箱的 連接起來{int father=index.get(list.get(1));for(int i=2;i<list.size();i++)union(index.get(list.get(i)),father);}Map<Integer,List<String>> map=new HashMap<>();for(String s:index.keySet())//創建每個集合中 父節點下標 對 所有郵箱地址的映射{int fa=find(index.get(s));List<String> list=map.getOrDefault(fa,new ArrayList<>());list.add(s);map.put(fa,list);}List<List<String>> res=new ArrayList<>();for(int f:map.keySet())//按特定格式 寫入結果{List<String> temp=new ArrayList<>();List<String> email=map.get(f);Collections.sort(email);temp.add(name.get(f));temp.addAll(email);res.add(temp);}return res;}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392298.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392298.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392298.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

es6重點筆記:數值,函數和數組

本篇全是重點&#xff0c;撿常用的懟&#xff0c;數值的擴展比較少&#xff0c;所以和函數放一起&#xff1a; 一&#xff0c;數值 1&#xff0c;Number.EPSILON&#xff1a;用來檢測浮點數的計算&#xff0c;如果誤差小于這個&#xff0c;就無誤 2&#xff0c;Math.trunc()&am…

SMSSMS垃圾郵件檢測器的專業攻擊

Note: The methodology behind the approach discussed in this post stems from a collaborative publication between myself and Irene Anthi.注意&#xff1a; 本文討論的方法背后的方法來自 我本人和 Irene Anthi 之間 的 合作出版物 。 介紹 (INTRODUCTION) Spam SMS te…

php pdo 緩沖,PDO支持數據緩存_PHP教程

/*** 作者&#xff1a;初十* QQ&#xff1a;345610000*/class myPDO extends PDO{public $cache_Dir null; //緩存目錄public $cache_expireTime 7200; //緩存時間&#xff0c;默認兩小時//帶緩存的查詢public function cquery($sql){//緩存存放總目錄if ($this->cache_Di…

mooc課程下載_如何使用十大商學院的免費課程制作MOOC“ MBA”

mooc課程下載by Laurie Pickard通過勞里皮卡德(Laurie Pickard) 如何使用十大商學院的免費課程制作MOOC“ MBA” (How to make a MOOC “MBA” using free courses from Top 10 business schools) Back when massive open online courses (MOOCs) were new, I started a proje…

leetcode 1584. 連接所有點的最小費用(并查集)

給你一個points 數組&#xff0c;表示 2D 平面上的一些點&#xff0c;其中 points[i] [xi, yi] 。 連接點 [xi, yi] 和點 [xj, yj] 的費用為它們之間的 曼哈頓距離 &#xff1a;|xi - xj| |yi - yj| &#xff0c;其中 |val| 表示 val 的絕對值。 請你返回將所有點連接的最小…

Nagios學習實踐系列

其實上篇Nagios學習實踐系列——基本安裝篇只是安裝了Nagios基本組件&#xff0c;雖然能夠打開主頁&#xff0c;但是如果不配置相關配置文件文件&#xff0c;那么左邊菜單很多頁面都打不開&#xff0c;相當于只是一個空殼子。接下來&#xff0c;我們來學習研究一下Nagios的配置…

在Salesforce中處理Email的發送

在Salesforce中可以用自帶的 Messaging 的 sendEmail 方法去處理Email的發送 請看如下一段簡單代碼&#xff1a; public boolean TextFormat {get;set;} public string EmailTo {get;set;} public string EmailCC {get;set;} public string EmailBCC {get;set;} public string …

kvm vnc的使用,鼠標漂移等

1.宿主機的vnc&#xff08;virtual Network Computing&#xff09;配置 安裝rpm包 yum install tigervnc-server -y 為了防止干擾直接關閉防火墻和selinux /etc/init.d/iptables stop setenforce 0 配置vnc密碼和啟動vncserver服務 vncpasswd vncserver 2.客戶機的vnc 在qemu…

php深淺拷貝,JavaScript 中的深淺拷貝

工作中經常會遇到需要復制 JavaScript 數據的時候&#xff0c;遇到 bug 時實在令人頭疼&#xff1b;面試中也經常會被問到如何實現一個數據的深淺拷貝&#xff0c;但是你對其中的原理清晰嗎&#xff1f;一起來看一下吧&#xff01;一、為什么會有深淺拷貝想要更加透徹的理解為什…

使用Python進行地理編碼和反向地理編碼

Geocoding is the process of taking input text, such as an address or the name of a place, and returning a latitude/longitude location. To put it simply, Geocoding is converting physical address to latitude and longitude.地理編碼是獲取輸入文本(例如地址或地點…

java開發簡歷編寫_如何通過幾個簡單的步驟編寫出色的初級開發人員簡歷

java開發簡歷編寫So you’ve seen your dream junior developer role advertised, and are thinking about applying. It’s time to write that Resume! Nothing better than sitting down to a blank piece of paper and not knowing how to start, right?因此&#xff0c;您…

leetcode 628. 三個數的最大乘積(排序)

給定一個整型數組&#xff0c;在數組中找出由三個數組成的最大乘積&#xff0c;并輸出這個乘積。 示例 1: 輸入: [1,2,3] 輸出: 6 解題思路 最大的乘積可能有兩種情況 1.兩個最小負數和一個最大正數 2.三個最大正數 代碼 class Solution {public int maximumProduct(int[…

[Object-C語言隨筆之三] 類的創建和實例化以及函數的添加和調用!

上一小節的隨筆寫了常用的打印以及很基礎的數據類型的定義方式&#xff0c;今天就來一起學習下如何創建類與函數的一些隨筆&#xff1b; 首先類的創建&#xff1a;在Xcode下&#xff0c;菜單File&#xff0d;New File&#xff0c;然后出現選擇class模板&#xff0c;如下圖&…

2024-AI人工智能學習-安裝了pip install pydot但是還是報錯

2024-AI人工智能學習-安裝了pip install pydot但是還是報錯 出現這樣子的錯誤&#xff1a; /usr/local/bin/python3.11 /Users/wangyang/PycharmProjects/studyPython/tf_model.py 2023-12-24 22:59:02.238366: I tensorflow/core/platform/cpu_feature_guard.cc:182] This …

grafana 創建儀表盤_創建儀表盤前要問的三個問題

grafana 創建儀表盤可視化 (VISUALIZATIONS) It’s easier than ever to dive into dashboarding, but are you doing it right?深入儀表板比以往任何時候都容易&#xff0c;但是您這樣做正確嗎&#xff1f; Tableau, Power BI, and many other business intelligence tools …

qq群 voiceover_如何在iOS上使用VoiceOver為所有人構建應用程序

qq群 voiceoverby Jayven N由Jayven N 如何在iOS上使用VoiceOver為所有人構建應用程序 (How to build apps for everyone using VoiceOver on iOS) 輔助功能入門 (Getting started with accessibility) There’s always those topics that people don’t talk about enough. S…

IntelliJ IDEA代碼常用的快捷鍵(自查)

IntelliJ IDEA代碼常用的快捷鍵有&#xff1a; Alt回車 導入包&#xff0c;自動修正 CtrlN 查找類 CtrlShiftN 查找文件 CtrlAltL 格式化代碼 CtrlAltO 優化導入的類和包 AltInsert 生成代碼(如get,set方法,構造函數等) CtrlE或者AltShiftC 最近更改的代碼 CtrlR…

leetcode 1489. 找到最小生成樹里的關鍵邊和偽關鍵邊(并查集)

給你一個 n 個點的帶權無向連通圖&#xff0c;節點編號為 0 到 n-1 &#xff0c;同時還有一個數組 edges &#xff0c;其中 edges[i] [fromi, toi, weighti] 表示在 fromi 和 toi 節點之間有一條帶權無向邊。最小生成樹 (MST) 是給定圖中邊的一個子集&#xff0c;它連接了所有…

帶彩色字體的man pages(debian centos)

1234567891011121314151617181920212223242526272829303132333435363738我的博客已遷移到xdoujiang.com請去那邊和我交流簡介most is a paging program that displays,one windowful at a time,the contents of a file on a terminal. It pauses after each windowful and prin…

提取json對象中的數據,轉化為數組

var xx1 ["樂譜中的調號為&#xff08; &#xff09;調", "寫出a自然小調音階。", "以G為冠音&#xff0c;構寫增四、減五音程。", "調式分析。", "將下列樂譜移為C大調。", "正確組合以下樂譜。", "以下…