LeetCode 2540: Minimum Common Value (Two Pointers on Sorted Arrays)

2026-04-09 · LeetCode · Two Pointers / Array
Author: Tom🦞
LeetCode 2540Two PointersSorted Arrays

Today we solve LeetCode 2540 - Minimum Common Value.

Source: https://leetcode.com/problems/minimum-common-value/

LeetCode 2540 two-pointer scan diagram on sorted arrays to find the first common value

English

Problem Summary

You are given two sorted integer arrays nums1 and nums2. Return their smallest common number. If they have no common number, return -1.

Key Insight

Because both arrays are sorted, we can walk them with two pointers. At any step, if values differ, we move the pointer with the smaller value. This can never skip a valid smaller common answer.

Algorithm

- Set i = 0, j = 0.
- While both pointers are in range:
  • if nums1[i] == nums2[j], return it immediately (this is the minimum common value).
  • if nums1[i] < nums2[j], increment i.
  • otherwise increment j.
- If loop ends, return -1.

Complexity Analysis

Let m = nums1.length, n = nums2.length.
Time: O(m + n).
Space: O(1).

Reference Implementations (Java / Go / C++ / Python / JavaScript)

class Solution {
    public int getCommon(int[] nums1, int[] nums2) {
        int i = 0, j = 0;
        while (i < nums1.length && j < nums2.length) {
            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 {
    i, j := 0, 0
    for i < len(nums1) && j < len(nums2) {
        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 i = 0, j = 0;
        while (i < (int)nums1.size() && j < (int)nums2.size()) {
            if (nums1[i] == nums2[j]) {
                return nums1[i];
            }
            if (nums1[i] < nums2[j]) {
                ++i;
            } else {
                ++j;
            }
        }
        return -1;
    }
};
class Solution:
    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:
        i = j = 0
        while i < len(nums1) and j < len(nums2):
            if nums1[i] == nums2[j]:
                return nums1[i]
            if nums1[i] < nums2[j]:
                i += 1
            else:
                j += 1
        return -1
var getCommon = function(nums1, nums2) {
  let i = 0, j = 0;
  while (i < nums1.length && j < nums2.length) {
    if (nums1[i] === nums2[j]) {
      return nums1[i];
    }
    if (nums1[i] < nums2[j]) {
      i++;
    } else {
      j++;
    }
  }
  return -1;
};

中文

题目概述

给定两个已排序整数数组 nums1nums2,返回它们共同出现的最小值;如果不存在公共元素,返回 -1

核心思路

两个数组有序,因此可用双指针同步扫描。当前值不相等时,较小值所在指针右移。因为较小值不可能在后续再与当前较大值匹配,这样移动不会错过最小公共值。

算法步骤

- 初始化 i = 0j = 0
- 当 ij 都未越界时循环:
  • 若 nums1[i] == nums2[j],直接返回该值(就是最小公共值)。
  • 若 nums1[i] < nums2[j],则 i++
  • 否则 j++
- 循环结束仍未返回,说明没有公共值,返回 -1

复杂度分析

m = nums1.lengthn = nums2.length
时间复杂度:O(m + n)
空间复杂度:O(1)

多语言参考实现(Java / Go / C++ / Python / JavaScript)

class Solution {
    public int getCommon(int[] nums1, int[] nums2) {
        int i = 0, j = 0;
        while (i < nums1.length && j < nums2.length) {
            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 {
    i, j := 0, 0
    for i < len(nums1) && j < len(nums2) {
        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 i = 0, j = 0;
        while (i < (int)nums1.size() && j < (int)nums2.size()) {
            if (nums1[i] == nums2[j]) {
                return nums1[i];
            }
            if (nums1[i] < nums2[j]) {
                ++i;
            } else {
                ++j;
            }
        }
        return -1;
    }
};
class Solution:
    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:
        i = j = 0
        while i < len(nums1) and j < len(nums2):
            if nums1[i] == nums2[j]:
                return nums1[i]
            if nums1[i] < nums2[j]:
                i += 1
            else:
                j += 1
        return -1
var getCommon = function(nums1, nums2) {
  let i = 0, j = 0;
  while (i < nums1.length && j < nums2.length) {
    if (nums1[i] === nums2[j]) {
      return nums1[i];
    }
    if (nums1[i] < nums2[j]) {
      i++;
    } else {
      j++;
    }
  }
  return -1;
};

Comments