大家好,我是编程熊,今天是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. [时间模块、random模块]

    [时间模块.random模块] time模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏 ...

  2. QT学习笔记(一)——Helloworld

    QT学习笔记(一)--Helloworld 一.调试的基本方法: Log调试法 --在代码中加入一定的打印语句 --打印程序状态和关键变量的值 断点调试法: --在开发环境中的对应代码行加上断点 -- ...

  3. linux进阶之Tomcat服务篇

    一.Tomcat简介 Tomcat服务器是一个免费的开放源代码的Web应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选. Tomca ...

  4. Jenkins 入门介绍

    一.概念 近几年,DevOps理念一致处于一个比较热门的状态.我每个月在工作群或者技术交流群都会看到这个名词出现.前年,当我第一次看到这个"DevOps",我压根不知道这是一个什么 ...

  5. [leetcode] 46. 全排列(Java)

    46. 全排列 这题我们可以借用31. 下一个排列写的nextPermutation函数来做,稍微改造一下即可 注意要先给nums排个序 class Solution { // 当没有下一个排列时re ...

  6. HTML5之WebSocket(转自知乎)

    在认识websocket之前,我们必须了解的是websocket有什么用? 他能解决我们遇到的什么问题? 如果没用,那么我们就么有使用它的必要的. websocket就是建立起全双工协议的,提高了效率 ...

  7. Pptx的形状转为WPF的Geometry

    本文是将演示如何解析pptx文件的形状到WPF当中,并且绘制显示出来 安装Openxml sdk 首先,我们先安装nuget的openxml sdk,下面两种方式都可以安装: nuget包管理器控制台 ...

  8. A100计算能力

    A100计算能力 A100 GPU支持新的计算功能8.0.表1比较了NVIDIA GPU架构的不同计算功能的参数. 表1.计算能力:GP100 vs. GV100 vs. GA100. MIG架构 尽 ...

  9. 使用nGraph的Intel&#174;Xeon&#174;上的高性能TensorFlow

    使用nGraph的IntelXeon上的高性能TensorFlow High-performance TensorFlow* on Intel Xeon Using nGraph 最近宣布了nGrap ...

  10. thymeleaf+Springboot实现自定义标签

    在项目开发中,有一些组件不能满足我们快速开发的要求,我们需要封装一些组件来更加的便利我们.比如,我们可以封装一个下拉框组件,只要开发人员只有引用这个组件的标签,就能出现效果,而不用再去请求url,渲染 ...