2441. Largest Positive Integer That Exists With Its Negative

题目 #

Given an integer array nums that does not contain any zeros, find the largest positive integer k such that -k also exists in the array.

Return the positive integer k. If there is no such integer, return -1.

Example 1:

Input: nums = [-1,2,-3,3]
Output: 3
Explanation: 3 is the only valid k we can find in the array.

Example 2:

Input: nums = [-1,10,6,7,-7,1]
Output: 7
Explanation: Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.

Example 3:

Input: nums = [-10,8,6,7,-2,-3]
Output: -1
Explanation: There is no a single valid k, we return -1.

Constraints:

  • $1 <= nums.length <= 1000$
  • $-1000 <= nums[i] <= 1000$
  • $nums[i] != 0$

思路1 排序+双指针 #

分析 #

  • 排序,左右两个指针,相加等于0直接返回右指针;大于0,右指针移动;小于0,左指针移动。

代码 #

 1func findMaxK2(nums []int) int {
 2	sort.Ints(nums)
 3	l, r := 0, len(nums)-1
 4	for l < r {
 5		addResult := nums[l] + nums[r]
 6		if addResult == 0 {
 7			return nums[r]
 8		} else if addResult > 0 {
 9			r--
10		} else {
11			l++
12		}
13	}
14	return -1
15}

思路2 使用map查找遍历解决 #

分析 #

  • 使用map记录每一个数字,遍历找对应的相反数是否出现过,出现过就记录,最终返回最大的

代码 #

 1func abs(v int) int {
 2	if v < 0 {
 3		return -v
 4	}
 5	return v
 6}
 7
 8func findMaxK1(nums []int) int {
 9	numMap := make(map[int]bool)
10	result := -1
11	for _, v := range nums {
12		if _, ok := numMap[-v]; ok {
13			if result < abs(v) {
14				result = abs(v)
15			}
16		}
17		numMap[v] = true
18	}
19	return result
20}