---恢复内容开始---

参考博客:

https://www.cnblogs.com/grandyang/p/4130379.html

https://blog.csdn.net/weixin_38715903/article/details/80483609

为了吃饭,于是就开启了leetcode的刷题之旅。其实应该在暑假就开始的,奈何个人太懒惰了,在此进行自我的检讨。

说回正题,leetcode和之前刷的oj的格式感觉不大一样,一开始看的时候感觉很不适应,但后来其实弄清楚门路了就还好。只要把其中的函数部分提取出来用来测试就可以了,就当一般oj使用就行。

回归第一题:

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

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

刚开始是想进行暴力搜索,但是oj的时间复杂度应该是不行的(其实这个也是别的博客说的,我等等去尝试下)。于是就想到了hashmap。其实感觉就是STL里面的map。

具体思路:

1.对数组所有元素形成key-value形式,key为数组值,value为下标。即map[num[0]]=0,map[num[1]]=1.

2.对于nums数组里面的数,遍历,寻找是否存在,map[target-num[i]],这个值;不存在继续寻找

3.若存在这么一个值map[target-num[i]],那判断这个值的下标是不是i,也就是判断target-num[i]这个值,是不是就是num[i],本身。如果是,那就不是两个数,继续寻找。如果不是,就说明存在这么两个数,返回下标,即i和map[target-num[i]]

代码如下:

#include "pch.h"
#include <iostream>
#include <iostream>
#include<math.h>
#include <iomanip>
#include<cstdio>
#include<string>
#include<map>
#include<vector>
#include<list>
#include<algorithm>
#include<stdlib.h>
#include<iterator>
#include<sstream>
#include<string.h>
#include<stdio.h>
using namespace std; vector<int> twoSum(vector<int>& nums, int target) {
vector<int> res;
map<int, int> temp;
for (int i = ; i < nums.size(); i++)
{
temp[nums[i]] = i;
} for (int i = ; i < nums.size(); i++)
{
if (temp.count(target-nums[i])!=&&temp[target-nums[i]]!=i)
{
res.push_back(i);
res.push_back(temp[target - nums[i]]);
break;
}
}
return res;
} int main()
{
//测试部分
vector<int> a;
vector<int> nums = {,,,};
int target = ;
a = twoSum(nums, target); for (int i = ; i < a.size(); i++)
{
cout << a[i] << endl;
} }

[LeetCode]1.Two Sum 两数之和&&第一次刷题感想的更多相关文章

  1. [LeetCode] 1. Two Sum 两数之和

    Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...

  2. [LeetCode] 1.Two Sum 两数之和分析以及实现 (golang)

    题目描述: /* Given an array of integers, return indices of the two numbers such that they add up to a sp ...

  3. 【LeetCode】Two Sum(两数之和)

    这道题是LeetCode里的第1道题. 题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会 ...

  4. [leetcode]1. Two Sum两数之和

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

  5. [LeetCode]1.Two Sum 两数之和(Java)

    原题地址:two-sum 题目描述: 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标. 你可以假设每 ...

  6. 【LeetCode】1. Two Sum 两数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:two sum, 两数之和,题解,leetcode, 力 ...

  7. 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 ...

  8. Leetcode:0002(两数之和)

    LeetCode:0002(两数之和) 题目描述:给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表.你可以假设除了数字 0 之外,这两 ...

  9. Leetcode(1)两数之和

    Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...

随机推荐

  1. Python-Django学习笔记(二)-创建一个Django项目与应用

    1.创建一个Django项目 打开cmd命令行,设置好工作目录(cd 目录路径),然后输入 django-admin startproject projectname #必须安装好Django才可以执 ...

  2. H3C端口隔离

    一.端口隔离简介 为了实现端口间的二层隔离,可以将不同的端口加入不同的VLAN,但VLAN资源有限.采用端口隔离特性,用户只需要将端口加入到隔离组中,就可以实现隔离组内端口之间二层隔离,而不关心这些端 ...

  3. nvm —— Node版本管理工具

    nvm下载 下载地址 下载nvm-setup.zip文件 nvm安装 1.以管理员身份运行install.cmd文件,设置文件路径 root: C:\Users\Administrator\AppDa ...

  4. blob - 二进制文件流下载

    /** * 返回值文件类型为 blob 二进制流文件 * responseType: 'blob' * params 接口所需参数 * 命名文件名:依据时间戳命名文件名 * (导出时需要延迟,否则导出 ...

  5. MySQL常用命令符

    收集于网络!!!! 解决字符乱码问题:显示汉语而非乱码:set names utf8: 修改新密码:update user set password=PASSWORD('新密码') where use ...

  6. BBR在实时音视频领域的应用

    小议BBR算法 BBR全称Bottleneck Bandwidth and RTT,它是谷歌在2016年推出的全新的网络拥塞控制算法.要说明BBR算法,就不能不提TCP拥塞算法. 传统的TCP拥塞控制 ...

  7. 【Python】字符串切片

  8. Linux sed识别HTML标签

    在做Linux作业,遇到一题用sed替换掉文件中的特殊字符,其中HTML标签就是一大堆特殊字符. 先来说说sed的替换使用“s/待替换的字符/将替换成的字符/”. 其后还可以跟g,即“s///g”,表 ...

  9. 一看就会一做就废系列:说说 RECOVER UNTIL CANCEL

    这里是:一看就会,一做就废系列 数据库演示版本为 19.3 (12.2.0.3) 该系列涉及恢复过程中使用的 5 个语句: 1. recover database 2. recover databas ...

  10. Json.Net的介绍与简单实用(兼容2.0/3.0/3.5/4.5/RT)

    本文的前提是你已经熟悉Json,如果您还不知道什么是Json是什么,请自行查看维基百科. 一.Json.Net是什么? Json.Net是一个读写Json效率比较高的.Net框架.Json.Net 使 ...