Re: [閒聊] 每日leetcode

作者: sustainer123 (caster)   2024-03-09 17:28:34
※ 引述《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應該比較快
作者: JIWP (JIWP)   2024-03-09 17:30:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com