欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          php求兩數(shù)組交集的三種方法詳解

          php求兩數(shù)組交集的三種方法詳解

          題目:給定兩個數(shù)組,編寫一個函數(shù)來計算它們的交集。

          示例 1:

          輸入: nums1 = [1,2,2,1],nums2 = [2,2]

          輸出: [2]

          示例 2:

          輸入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]

          輸出: [9,4]

          說明:

          輸出結(jié)果中的每個元素一定是唯一的。

          我們可以不考慮輸出結(jié)果的順序。

          解法一:迭代一個數(shù)組

          思路分析:

          迭代一個數(shù)組,判斷是否存在另外一個數(shù)組

          PHP 代碼實現(xiàn):

          /**  * @param Integer[] $nums1  * @param Integer[] $nums2  * @return Integer[]  */ function intersection($nums1, $nums2) {     $res = [];     for($i=0;$i<count($nums1);$i++){         if(in_array($nums1[$i],$nums2)){             $res[] = $nums1[$i];         }     }     return array_unique($res); }

          使用:

          $nums2 = [2,4,6,7,8,99]; $nums1 = [1,2,5,9,9,66,89,90,99,99]; var_dump(intersection($nums1, $nums2));

          復(fù)雜度分析:

          時間復(fù)雜度:O(mn)

          解法二:內(nèi)置數(shù)組函數(shù)

          思路分析:

          使用array_intersect()函數(shù)進行取數(shù)組的交集,然后再使用array_unique()去重

          PHP 代碼實現(xiàn):

          /**  * @param Integer[] $nums1  * @param Integer[] $nums2  * @return Integer[]  */ function intersection($nums1, $nums2) {     return array_unique(array_intersect($nums1,$nums2)); }

          使用:

          $nums2 = [2,4,6,7,8,99]; $nums1 = [1,2,5,9,9,66,89,90,99,99]; var_dump(intersection($nums1, $nums2));

          解法三:暴力解法

          思路分析:

          先把兩個數(shù)組合并為一個數(shù)組,再兩次循環(huán)遍歷查找

          PHP 代碼實現(xiàn):

          /**  * @param Integer[] $nums1  * @param Integer[] $nums2  * @return Integer[]  */ function intersection($nums1, $nums2) {     $new_arr = array_merge(array_unique($nums1),array_unique($nums2));     $res = [];     for($i=0;$i<count($new_arr);$i++){         for($j=$i+1;$j<count($new_arr);$j++){             if($new_arr[$i] == $new_arr[$j]){                 $res[] = $new_arr[$i];             }         }     }     return array_unique($res); }

          使用:

          $nums2 = [2,4,6,7,8,99]; $nums1 = [1,2,5,9,9,66,89,90,99,99]; var_dump(intersection($nums1, $nums2));

          復(fù)雜度分析:

          時間復(fù)雜度:O(n^2)

          解法四:雙指針

          思路分析:

          先把兩個數(shù)組排序,通過雙指針往前推來進行查找

          PHP 代碼實現(xiàn):

          /**  * @param Integer[] $nums1  * @param Integer[] $nums2  * @return Integer[]  */ function intersection($nums1, $nums2) {     sort($nums1);     sort($nums2);     $i = $j = 0;     $res = [];     while($i < count($nums1) && $j < count($nums2)){         if($nums1[$i] == $nums2[$j]){             $res[] = $nums1[$i];             $i++;             $j++;         }elseif($nums1[$i] < $nums2[$j]){             $i++;         }elseif($nums1[$i] > $nums2[$j]){             $j++;         }     }     return array_unique($res); }

          使用:

          $nums2 = [2,4,6,7,8,99]; $nums1 = [1,2,5,9,9,66,89,90,99,99]; var_dump(intersection($nums1, $nums2));

          復(fù)雜度分析:

          時間復(fù)雜度:O(nlogn)

          贊(0)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號