Re: [閒聊] 每日leetcode
看板Marginalman作者sustainer123 (caster )時間1年前 (2024/03/09 17:28)推噓1(1推 0噓 0→)留言1則, 1人參與討論串28/1548 (看更多)
※ 引述《NCKUEECS (小惠我婆)》之銘言:
: 水P幣時間
: 2540. Minimim Common Value
: 兩個排序過的array,找出同時出現的最小值,沒有就回傳-1
: 思路:two pointer
: int getCommon(int* nums1, int nums1Size, int* nums2, int nums2Size){
: int i=0, j=0;
: while(i!=nums1Size && j!=nums2Size){
: if(nums1[i]==nums2[j])
: return nums1[i];
: else if(nums1[i]<nums2[j])
: i++;
: else
: j++;
: }
: return -1;
: }
思路:
找交集的最小值
Python Code:
class Solution:
def getCommon(self, nums1: List[int], nums2: List[int]) -> int:
if set(nums1) & set(nums2):
return min(set(nums1) & set(nums2))
else:
return -1
用two pointer應該比較快
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.140.202.202 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1709976517.A.E1A.html
推
03/09 17:30,
1年前
, 1F
03/09 17:30, 1F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 28 之 1548 篇):