leetcode 1. 两数之和(Two Sum)

目录

  • 题目描述:
  • 示例:
  • 解法:

题目描述:

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

示例:


    给定 nums = [2, 7, 11, 15], target = 9
    因为 nums[0] + nums[1] = 2 + 7 = 9
    所以返回 [0, 1]

解法:


class Solution {
public:
    vector twoSum(vector& nums, int target) {
        unordered_map> mp;
        int sz = nums.size();

        for(int i=0; i= 2){
                        return {it.second[0], it.second[1]};
                    }
                }else{
                    return {it.second[0], mp[b][0]};
                }
            }
        }
        return {};
    }
};

原文地址:https://www.cnblogs.com/zhanzq/p/10551553.html

时间: 2024-07-31 23:04:11

leetcode 1. 两数之和(Two Sum)的相关文章

[算法] LeetCode 1.两数之和

LeetCode 1.两数之和(python) 1.朴素解法 最朴素的两个for循环大法: class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: for i in range(len(nums)): for j in range(i+1,len(nums)): if nums[i] + nums[j] == target: return [i, j] 但注意,不要用enumerate函数写,会超时

前端与算法 leetcode 1. 两数之和

目录 # 前端与算法 leetcode 1. 两数之和 题目描述 概要 提示 解析 解法一:暴力法 解法二:HashMap法 算法 传入[2,7,11,1,12,34,4,15],19的运行结果 执行结果 GitHub仓库 # 前端与算法 leetcode 1. 两数之和 题目描述 给定一个整数数组 nums?和一个目标值 target,请你在该数组中找出和为目标值的那?两个?整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给

leetCode:twoSum 两数之和 【JAVA实现】

LeetCode 两数之和 给定一个整数数组,返回两个数字的索引,使它们相加到特定目标. 您可以假设每个输入只有一个解决方案,并且您可能不会两次使用相同的元素. 更多文章查看个人博客 个人博客地址:twoSum 两数之和 [JAVA实现] 方法一 使用双重循环两两相加判断是否等于目标值 public List<String> twoSum2(int[] arr, int sum) { if (arr == null || arr.length == 0) { return new ArrayL

关于leetcode上两数之和的思考

今天在leetcode上完成这道题目时: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9所以返回 [0, 1] 初步代码为 1 class Solution { 2 public int[] t

LeetCode——1.两数之和

LeetCode的第一题:两数之和,给定一个整数数组nums和一个目标值target,要求在数组中找出和为目标值的两个整数,并返回它们的下标值. 代码如下: 1 var nums = [2, 7, 11, 15] 2 var target = 9 3 4 var twoSum = function (nums, target) { 5 for (let i = 0; i < nums.length; i++) { 6 for (let j = i; j < nums.length; j++)

【LeetCode】 两数之和 twoSum

两数之和 (简单) 题目描述 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数: 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 例如: 给定 nums = [2,7,11,15] ,target = 9 因为 nums[0] + nums[1] = 9: 因此返回 [0,1]: v1.0代码如下: 正数.0或重复值通过测试: 异常用例: [-1, -2, -3, -4, -5] -8; 输出 [] /** * @param {number[]} nums * @par

[Leetcode 18]四数之和 4 Sum

[题目] Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note:The solution set must not contain d

leetcode T1 两数之和详解

两数之和 题目描述 给定一个整数数组 nums?和一个目标值 target,请你在该数组中找出和为目标值的那?两个?整数,并返回他们的数组下标.你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例 给定 nums = [2, 7, 11, 15], target = 9,因为 nums[0] + nums[1] = 2 + 7 = 9,所以返回 [0, 1] 详细题解 暴力枚举 暴力法的思路很简单:遍历数组nums的每一个元素nums[i],在[i+1, ...,

[Leetcode 15]三数之和 3 Sum

[题目] Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. Example: Given array nu