LeetCode 面试题 16.24. 数对和

一、题目

  设计一个算法,找出数组中两数之和为指定值的所有整数对。一个数只能属于一个数对。

示例 1:

输入: nums = [5,6,5], target = 11
输出: [[5,6]]

示例 2:

输入: nums = [5,6,5,6], target = 11
输出: [[5,6],[5,6]]

提示:

  • nums.length <= 100000
  • -10^5 <= nums[i], target <= 10^5

  点击此处跳转题目

二、C# 题解

  使用双指针双向检测,注意要先排序。

public class Solution {
    public IList<IList<int>> PairSums(int[] nums, int target) {
        IList<IList<int>> ans = new List<IList<int>>();
        Array.Sort(nums);
        int i = 0, j = nums.Length - 1; // 双指针
        while (i < j) {
            int sum = nums[i] + nums[j];                                // 两数和
            if (sum == target) ans.Add(new[] { nums[i++], nums[j--] }); // 找到则添加,i、j 均挪位
            else if (sum > target) j--;                                 // 如果大了,右指针左移
            else i++;                                                   // 否则左指针右移
        }
        return ans;
    }
}
  • 时间:216 ms,击败 83.33% 使用 C# 的用户
  • 内存:65.98 MB,击败 16.67% 使用 C# 的用户

  直接遍历数组,使用 map 记录出现过的数字和次数,每次查找是否有匹配的 another。

public class Solution {
    public IList<IList<int>> PairSums(int[] nums, int target) {
        IList<IList<int>>    ans = new List<IList<int>>();
        Dictionary<int, int> dic = new Dictionary<int, int>(); // 存储出现过的数字及次数
        foreach (int num in nums) {
            int another = target - num; // 匹配的另一个数
            if (dic.TryGetValue(another, out int value)) { // 尝试找之前是否出现过
                if (value > 0) { // 如果出现过,则次数 - 1,添加结果
                    dic[another]--;
                    ans.Add(new[] { num, another });
                }
                else if (!dic.TryAdd(num, 1)) dic[num]++; // 出现过但次数被用完,添加 num 的记录
            }
            else if (!dic.TryAdd(num, 1)) dic[num]++; // 未出现过,添加 num 的记录
        }
        return ans;
    }
}
  • 时间:204 ms,击败 100.00% 使用 C# 的用户
  • 内存:60.31 MB,击败 100.00% 使用 C# 的用户
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蔗理苦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值