返回介绍

solution / 2500-2599 / 2540.Minimum Common Value / README_EN

发布于 2024-06-17 01:03:04 字数 5557 浏览 0 评论 0 收藏 0

2540. Minimum Common Value

中文文档

Description

Given two integer arrays nums1 and nums2, sorted in non-decreasing order, return _the minimum integer common to both arrays_. If there is no common integer amongst nums1 and nums2, return -1.

Note that an integer is said to be common to nums1 and nums2 if both arrays have at least one occurrence of that integer.

 

Example 1:

Input: nums1 = [1,2,3], nums2 = [2,4]
Output: 2
Explanation: The smallest element common to both arrays is 2, so we return 2.

Example 2:

Input: nums1 = [1,2,3,6], nums2 = [2,3,4,5]
Output: 2
Explanation: There are two common elements in the array 2 and 3 out of which 2 is the smallest, so 2 is returned.

 

Constraints:

  • 1 <= nums1.length, nums2.length <= 105
  • 1 <= nums1[i], nums2[j] <= 109
  • Both nums1 and nums2 are sorted in non-decreasing order.

Solutions

Solution 1: Two Pointers

Traverse the two arrays. If the elements pointed to by the two pointers are equal, return that element. If the elements pointed to by the two pointers are not equal, move the pointer pointing to the smaller element to the right by one bit until an equal element is found or the array is traversed.

The time complexity is $O(m + n)$, where $m$ and $n$ are the lengths of the two arrays respectively. The space complexity is $O(1)$.

class Solution:
  def getCommon(self, nums1: List[int], nums2: List[int]) -> int:
    i = j = 0
    m, n = len(nums1), len(nums2)
    while i < m and j < n:
      if nums1[i] == nums2[j]:
        return nums1[i]
      if nums1[i] < nums2[j]:
        i += 1
      else:
        j += 1
    return -1
class Solution {
  public int getCommon(int[] nums1, int[] nums2) {
    int m = nums1.length, n = nums2.length;
    for (int i = 0, j = 0; i < m && j < n;) {
      if (nums1[i] == nums2[j]) {
        return nums1[i];
      }
      if (nums1[i] < nums2[j]) {
        ++i;
      } else {
        ++j;
      }
    }
    return -1;
  }
}
class Solution {
public:
  int getCommon(vector<int>& nums1, vector<int>& nums2) {
    int m = nums1.size(), n = nums2.size();
    for (int i = 0, j = 0; i < m && j < n;) {
      if (nums1[i] == nums2[j]) {
        return nums1[i];
      }
      if (nums1[i] < nums2[j]) {
        ++i;
      } else {
        ++j;
      }
    }
    return -1;
  }
};
func getCommon(nums1 []int, nums2 []int) int {
  m, n := len(nums1), len(nums2)
  for i, j := 0, 0; i < m && j < n; {
    if nums1[i] == nums2[j] {
      return nums1[i]
    }
    if nums1[i] < nums2[j] {
      i++
    } else {
      j++
    }
  }
  return -1
}
function getCommon(nums1: number[], nums2: number[]): number {
  const m = nums1.length;
  const n = nums2.length;
  let i = 0;
  let j = 0;
  while (i < m && j < n) {
    if (nums1[i] === nums2[j]) {
      return nums1[i];
    }
    if (nums1[i] < nums2[j]) {
      i++;
    } else {
      j++;
    }
  }
  return -1;
}
impl Solution {
  pub fn get_common(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
    let m = nums1.len();
    let n = nums2.len();
    let mut i = 0;
    let mut j = 0;
    while i < m && j < n {
      if nums1[i] == nums2[j] {
        return nums1[i];
      }
      if nums1[i] < nums2[j] {
        i += 1;
      } else {
        j += 1;
      }
    }
    -1
  }
}
int getCommon(int* nums1, int nums1Size, int* nums2, int nums2Size) {
  int i = 0;
  int j = 0;
  while (i < nums1Size && j < nums2Size) {
    if (nums1[i] == nums2[j]) {
      return nums1[i];
    }
    if (nums1[i] < nums2[j]) {
      i++;
    } else {
      j++;
    }
  }
  return -1;
}

Solution 2

impl Solution {
  pub fn get_common(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
    let mut iter1 = nums1.iter();
    let mut iter2 = nums2.iter();
    let mut num1 = iter1.next();
    let mut num2 = iter2.next();

    while let (Some(n1), Some(n2)) = (num1, num2) {
      if n1 == n2 {
        return *n1;
      } else if n1 < n2 {
        num1 = iter1.next();
      } else {
        num2 = iter2.next();
      }
    }

    -1
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文