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

参考博客:

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. Unknown CMake command "check_symbol_exists".

    - Using these message generators: gencpp;geneus;genlisp;gennodejs;genpyCMake Error at CMakeLists.txt ...

  2. Sql注入之注入点类型和是否存在注入判断

    SQL注入之判断注入类型注入类型分为数字型和字符型和搜索型例如数字型语句:select * from table where id =3,则字符型如下:select * from table wher ...

  3. 【Python】表白程序

     程序链接:https://www.lanzous.com/i8xj5mh # 打包操作 # 安装pyinstaller # cmd输入 pip install pyinstaller # shift ...

  4. Selenium3+python自动化007-警告框

    警告框 alert = driver.switch_to.alert alert.text() alert.accpet() alert.dismiss() # 导selenium包 from sel ...

  5. 为什么 RMAN 控制文件自动备份的名称格式没有遵循 %F 规则

    在 Oracle 中越是简单的问题,往往越难找到答案,举个例子: 你是否留意观察过在 RMAN 进行备份的时候,自动生成的控制文件名称是否是按照 %F 规则来生成的? 关于控制文件自动备份路径格式,在 ...

  6. 打开UML类图的正确姿势

    UML(Unified Modeling Language) 统一建模语言,又称标准建模语言.是用来对软件密集系统进行可视化建模的一种语言.UML的定义包括UML语义和UML表示法两个元素.UML是在 ...

  7. JDBC没有导入驱动jar包

  8. Django教程(2)

    from Django official document; Django 最初被设计用于具有快速开发需求的新闻类站点,目的是要实现简单快捷的网站开发. from 编写你的第一个 Django 应用, ...

  9. Linux 查看是否安装 oracle

    查看是否用 oracle 的进程 ps -ef | grep ora 一般安装 oracle ,默认会有 oracle 的用户. id oracle

  10. ORA-01843: not a valid month

    问题描述 ORA-01843: not a valid month oracle数据库插入出现无效的月份