大家好,我是编程熊,今天是LeetCode每日一题的第一天,今天的你比昨天更加优秀啦!

题意

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

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。

你可以按任意顺序返回答案。

样例

输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。

题解

方法一

设两个要找的位置上的数字为 AB,则要找的就是满足 A+B=target ,因此简单的思路是,两层 for 循环遍历给的数组,第一层 for 找 A,第二层 forB,如果 AB 不是同一个位置的数且满足 A+B=target ,我们就找到了答案要找的两个位置。

for (int i = 1; i <= n; i++) {
for (int j = 1; j < i; j++) {
// 找到满足条件的nums[i]+nums[j]=target
}
}

时间复杂度:O(n^2)

空间复杂度:O(1)

方法二

基于方法一的前提,我们思考一下我们能不能更低的时间复杂度呢?刚刚我们固定了 A,还遍历了数组去找 B 确定是否存在 A+B=target, 那么我们能不能加快查找 B 的速度呢?

A+B=target 可以看做 B=target-A,因此可以固定 A,检查是否存在 B 满足 B=target-A。快速检索一个数据的索引,可以哈希表。此题中可以把数组中的值作为哈希表中的 key ,下标索引当 value

C++中 map 是基于红黑树,插入和查询的时间复杂度都为 $O(logn)$; unorder_map 底层基于哈希表,插入和查询时间复杂度为$O(1)$。因此,选择用 unorder_map 实现。

时间复杂度: O(n)

空间复杂度: O(n)

C++代码

class Solution {
public:
unordered_map<int, int> numExist;
vector<int> twoSum(vector<int> &nums, int target) {
vector<int> ans;
for (int i = 0; i < nums.size(); i++) {
int b = target - nums[i];
if (numExist.count(b)) {
ans.push_back(i);
ans.push_back(numExist.at(b));
break;
}
numExist[nums[i]] = i;
}
return ans;
}
};

Java代码

class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> numExist = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; ++i) {
if (numExist.containsKey(target - nums[i])) {
return new int[]{i, numExist.get(target - nums[i])};
}
numExist.put(nums[i], i);
}
return new int[2];
}
}

题目链接: https://leetcode-cn.com/problems/two-sum/

我是编程熊,致力于让大家都成为更好的人,欢迎 『关注』、『点赞』、『转发』支持~

【LeetCode每日一题 Day 1】1. 两数之和的更多相关文章

  1. 【LeetCode每天一题】Two Sum(两数之和)

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  2. leecode刷题(8)-- 两数之和

    leecode刷题(8)-- 两数之和 两数之和 描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输 ...

  3. LeetCode刷题 1. Two Sum 两数之和 详解 C++语言实现 java语言实现

    1. Two Sum 两数之和 Given an array of integers, return indices of the two numbers such that they add up ...

  4. leetcode题库练习_两数之和

    题目:两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素不能 ...

  5. LeetCode 371. Sum of Two Integers (两数之和)

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  6. 【Leetcode】【简单】【1. 两数之和】【JavaScript】

    题目描述 1. 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能 ...

  7. LeetCode初级算法之数组:1 两数之和

    两数之和 题目地址:https://leetcode-cn.com/problems/two-sum/ 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个整 ...

  8. LeetCode题解【题2】:两数相加

    原题链接:https://leetcode-cn.com/problems/add-two-numbers/ 查看请另起链接打开. 解题思路执行用时 :2 ms, 在所有 Java 提交中击败了99. ...

  9. [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  10. 【LeetCode每天一题】4Sum(4数之和)

    Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...

随机推荐

  1. copy和deep.copy

    https://blog.csdn.net/qq_32907349/article/details/52190796 加上crossin公众号上的可变对象与不可变对象 a=[1,2,3,[4]] b= ...

  2. Android面试必问!View 事件分发机制,看这一篇就够了!

    在 Android 开发当中,View 的事件分发机制是一块很重要的知识.不仅在开发当中经常需要用到,面试的时候也经常被问到. 如果你在面试的时候,能把这块讲清楚,对于校招生或者实习生来说,算是一块不 ...

  3. JSX语法详解

    一.基础1.JSX是什么JSX是一种像下面这样的语法: const element = <h1>Hello, world!</h1>;1它是一种JavaScript语法扩展,在 ...

  4. prometheus node-exporter增加新的自定义监控项

    项目中collector中新增加自己所需监控项即可 定义启动node-exporter是传入的参数 var ( phpEndPoint = kingpin.Flag("collector.p ...

  5. python基础之psutil模块和发邮件(smtplib和yagmail)

    除了内建的模块外,Python还有大量的第三方模块. 基本上,所有的第三方模块都会在PyPI - the Python Package Index上注册,只要找到对应的模块名字,即可用pip安装. 此 ...

  6. 炫彩流光按钮 CSS + HTML

    炫彩流光按钮 写在前面 你若要喜爱你自己的价值,你就得给世界创造价值.--歌德 效果图 三个绝美的样例 HTML代码 <div class="box"> <but ...

  7. Django(43)restful接口规范

    restful接口规范   什么是接口规范?接口规范就是为了采用不同的后台语言,也能使用同样的接口获取到同样的数据.如何写接口:接口规范是规范化书写接口的,写接口要写url.响应数据 ​  注:如果将 ...

  8. idea-创建web.xml模板

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="htt ...

  9. K8s - Kubernetes重要概念介绍(Cluster、Master、Node、Pod、Controller、Service、Namespace)

    K8s - Kubernetes重要概念介绍(Cluster.Master.Node.Pod.Controller.Service.Namespace)       Kubernetes 是目前发展最 ...

  10. Jmeter- 笔记12 - 性能测试分析 & 性能测试流程

    性能测试分析 场景设计.监视图表: 设计场景:阶梯式.波浪式 监视器: 收集用于性能分析的数据:TPS图表.聚合报告\汇总报告.察看结果树.响应时间.吞吐量 服务器资源监控:cpu.内存.磁盘io 分 ...