大家好,我是编程熊,今天是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. X Sever —— Xorg

    X Sever -- Xorg  发表于 2020-03-20 分类于 系统服务 , Xorg 阅读次数:39 阅读次数:48 本文字数: 7k 阅读时长 ≈ 6 分钟 Xorg:基于X11协议的服务 ...

  2. Sqoop 安装部署

    1. 上传并解压 Sqoop 安装文件 将 sqoop-1.4.7.bin__hadoop-2.6.0.tar.gz 安装包上传到 node-01 的 /root/ 目录下并将其解压 [root@no ...

  3. 列出 Ubuntu 和 Debian 上已安装的软件包

    列出 Ubuntu 和 Debian 上已安装的软件包 如果你经常用 apt 命令,你可能觉得会有个命令像 apt 一样可以列出已安装的软件包.不算全错. apt-get 命令 没有类似列出已安装软件 ...

  4. 053.Python前端Django框架模板层

    模板层 一 模板语法之变量 在 Django 模板中遍历复杂数据结构的关键是句点字符, 语法: {{ var_name }} [root@node10 mysite]# cat app01/urls. ...

  5. python基础之字符串类型

    一.python字符串类型概述 定义:在单引号\双引号\三引号内,由一串字符组成 name='Test' name = 'test' print(type(name)) --------------- ...

  6. 8.12-14 df 、mkswap、swapon、swapoff、sync

    8.12 df:报告文件系统磁盘空间的使用情况   -a    显示所有文件系统 -h    以容易理解的格式显示磁盘的使用情况端 -i    显示文件系统的inode信息迷 -t    显示指定类型 ...

  7. BUCK BOOST学习总结

    首先对于我这种电源方面的小白来说 关于电源用的最多的就是线性稳压了 开关类的如  TI 的TPS系列  我是只知道应用电路而不知道具体原理的 但是长此以往也不是个办法 于是今天就带打家详细的来讲一下 ...

  8. java 集合梳理

    使用 processOn 画的java 集合图谱,应付面试应该可以了

  9. mysql数据库-简介

    目录 1 MySQL 的三大主要分支 1.1 官方文档 1.2 版本演变 1.3 MySQL 安装方式 1.3.1 RPM包安装Mysql 1.3.2 二进制安装MySQL 1.4 mysql组成 1 ...

  10. 大数据学习之路—环境配置——IP设置(虚拟机修改Ip的内在原因及实现)

    一.IP原理 关于IP我的理解, (1)主要去理解IP地址的作用,IP地址包括网络相关部分和主机的相关部分.即:用一段特殊的数据,来标识网络特征和主机的特征. 至于具体的技术实现,日后可以慢慢体会和了 ...