大家好,我是编程熊,今天是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. [MySQL数据库之记录的详细操作:增、改、删、单表查询、多表查询]

    [MySQL数据库之记录的详细操作:增.改.删.单表查询.多表查询] 记录详细操作 增.删.改 增: insert t1(字段1,字段2,字段3) values (值1,值2,值3), (值1,值2, ...

  2. Nifi:nifi内置处理器Processor的开发

    本篇主要是介绍自定义处理器的开发方式及Nifi处理器开发的一些细节 Nifi-Processor自定义开发的流程 之前说过,大部分的数据处理,我们可以基于ExcuseGroovyScript处理器,编 ...

  3. [c++] 细节

    \r退格:printf("asdflkj\r111")输出111flkj(https://blog.csdn.net/tyshtang/article/details/436770 ...

  4. 还可以使用 -c 参数来显示全部内容,并标出不同之处 diff -c test2.txt test1.txt

    二.实例 在test目录下存放了两个文本文件,test1.txt  test2.txt . 比较这两个文件的异同. diff  test1.txt   test2.txt     "5c5& ...

  5. shell脚本就是由Shell命令组成的执行文件,将一些命令整合到一个文件中,进行处理业务逻辑,脚本不用编译即可运行。它通过解释器解释运行,所以速度相对来说比较慢。

    shell脚本?在说什么是shell脚本之前,先说说什么是shell. shell是外壳的意思,就是操作系统的外壳.我们可以通过shell命令来操作和控制操作系统,比如Linux中的Shell命令就包 ...

  6. Linux 系统中如何查看日志 (常用命令) tail -f

    Linux 系统中如何查看日志 (常用命令)  tail -f 日志文件 日 志 文 件 说 明 /var/log/message 系统启动后的信息和错误日志,是Red Hat Linux中最常用的日 ...

  7. WORD表格中的文字总是靠上居中不了

    WORD表格中的文字总是靠上居中不了 将表格选中 然后,点击格式工具栏里的格式(第一个项目)右侧的小三角(通常显示为正文),选择清除格式,然后,再用表格与边框工具栏中的居中功能设置居中就可以了 将表格 ...

  8. 第七章 DevOps工具链

    DevOps工具生态圈 协同开发工具 敏捷开发 可视化 加强团队沟通协作 数据分析 协同开发 持续集成工具 Jenkins 自动化编译 自动化测试 自动化部署 丰富的插件库 版本管理工具 Git 简介 ...

  9. WPF 2D图形 Shape入门(一)--Shape

    本文是篇WPF Shape的入门文章 Shape 首先看看shape的继承链关系: 一个Shape具有哪些重要属性: 属性 说明 DefiningGeometry 默认的几何形状 RenderedGe ...

  10. VMware虚拟机CentOS磁盘扩容

    版本信息: VMware Workstation 15 Pro  15.5.2 build-15785246, CentOS7 原虚拟机默认20G,安装东西多了,磁盘空间不够用, docker mys ...