<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)323:無向圖中連通分量的數(shù)目

          共 2902字,需瀏覽 6分鐘

           ·

          2021-07-18 11:19

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做 無向圖中連通分量的數(shù)目,我們先來看題面:
          https://leetcode-cn.com/problems/number-of-connected-components-in-an-undirected-graph/

          Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to find the number of connected components in an undirected graph.

          給定編號從 0 到 n-1 的 n 個節(jié)點和一個無向邊列表(每條邊都是一對節(jié)點),請編寫一個函數(shù)來計算無向圖中連通分量的數(shù)目。


          示例



          解題

          https://blog.csdn.net/Scarlett_Guan/article/details/104086040
          這個題目可以轉(zhuǎn)化為用并查集求一共有多少個老大的問題。

          class Solution {
          public:
              //找每一個頂點的老大
              int find_father(vector<int> &f, int i){
                  while(i!=f[i]){
                      i=f[i];
                  }
                  return i;
              }
           
              int countComponents(int n, vector<vector<int>>& edges) {
                  vector<int>f(n);
                  //將每一個頂點單獨分成一組
                  for(int i=0; i<n; ++i){
                      f[i]=i;
                  }
                  //進(jìn)行同一組的頂點的合并
                  for(auto x:edges){
                      int p=find_father(f, x[0]);
                      int q=find_father(f, x[1]);
                      if(p==q) continue;
                      else f[p]=q;
                  }
                  //找一共有多少個不同的老大
                  unordered_set<int>s;
                  for(int i=0; i<f.size(); ++i){
                      s.insert(find_father(f, i));
                  }
                  return s.size();
              }
          };


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力 。

          上期推文:
          LeetCode1-320題匯總,希望對你有點幫助!
          LeetCode刷題實戰(zhàn)321:拼接最大數(shù)
          LeetCode刷題實戰(zhàn)322:零錢兌換

          瀏覽 38
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日本黄色视频官网 | 北条麻妃无码观看 | 黄色操逼片 | 欧美成人性之站 | 日韩无码性爱视频 |