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# 题解

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

csharp 复制代码
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。

csharp 复制代码
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# 的用户
相关推荐
StickToForever4 小时前
第4章 信息系统架构(五)
经验分享·笔记·学习·职场和发展
软件黑马王子5 小时前
C#初级教程(4)——流程控制:从基础到实践
开发语言·c#
计算机小白一个6 小时前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
万事可爱^6 小时前
HDBSCAN:密度自适应的层次聚类算法解析与实践
算法·机器学习·数据挖掘·聚类·hdbscan
欧了1118 小时前
洛谷P9240 [蓝桥杯 2023 省 B] 冶炼金属
职场和发展·蓝桥杯·洛谷·蓝桥杯大学b组c语言
大数据追光猿8 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Dream it possible!9 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
夏末秋也凉9 小时前
力扣-回溯-46 全排列
数据结构·算法·leetcode
南宫生9 小时前
力扣每日一题【算法学习day.132】
java·学习·算法·leetcode
柠石榴9 小时前
【练习】【回溯No.1】力扣 77. 组合
c++·算法·leetcode·回溯