← 返回 Kata
EasyLeetCode2026-07-24原题 ↗

063Minimum Common Value

arraytwo-pointers

Problem

给定两个按非递减顺序排列的整数数组 nums1nums2

请找出同时出现在这两个数组中的最小整数。

如果两个数组没有公共整数,返回:

-1

例如:

nums1 = [1, 2, 3]
nums2 = [2, 4]

整数 2 同时出现在两个数组中,并且是最小的公共整数,所以返回:

2

注意:两个数组都已经按从小到大的顺序排列,但其中可能包含重复元素。

Examples

示例 1

Input:  nums1 = [1, 2, 3], nums2 = [2, 4]
Output: 2

解释:2 是两个数组中唯一的公共整数。

示例 2

Input:  nums1 = [1, 2, 3, 6], nums2 = [2, 3, 4, 5]
Output: 2

解释:23 都同时出现在两个数组中,其中较小的是 2

示例 3

Input:  nums1 = [1, 2, 3], nums2 = [4, 5, 6]
Output: -1

解释:两个数组没有公共整数,所以返回 -1

Constraints

  • 11 \leq nums1.length, nums2.length 105\leq 10^5
  • 11 \leq nums1[i], nums2[j] 109\leq 10^9
  • nums1nums2 都按非递减顺序排列

最初怎么想、怎样补完整、最后如何落成。