[LeetCode]1.Two Sum 两数之和&&第一次刷题感想
---恢复内容开始---
参考博客:
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 两数之和&&第一次刷题感想的更多相关文章
- [LeetCode] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- [LeetCode] 1.Two Sum 两数之和分析以及实现 (golang)
题目描述: /* Given an array of integers, return indices of the two numbers such that they add up to a sp ...
- 【LeetCode】Two Sum(两数之和)
这道题是LeetCode里的第1道题. 题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会 ...
- [leetcode]1. Two Sum两数之和
Given an array of integers, return indices of the two numbers such that they add up to a specific t ...
- [LeetCode]1.Two Sum 两数之和(Java)
原题地址:two-sum 题目描述: 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标. 你可以假设每 ...
- 【LeetCode】1. Two Sum 两数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:two sum, 两数之和,题解,leetcode, 力 ...
- 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 ...
- Leetcode:0002(两数之和)
LeetCode:0002(两数之和) 题目描述:给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表.你可以假设除了数字 0 之外,这两 ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
随机推荐
- 关于Oracle内存分配-解决实际运行时最大Session数不一致远小于系统配置最大的Session数目
一.相关的技术准备 1. 关于内存的介绍:https://blog.csdn.net/u013641333/article/details/82732526 2. PGA_AGGREGATE_TARG ...
- linux - 异常:安装包冲突 conflicts with
问题描述 解决方案 删除冲突的包 命令格式:yum -y remove 包名 yum -y remove httpd24u yum -y remove httpd24u-tools
- NOI2005 维护数列 lg2042
这道题据说是noi题目中算是比较毒瘤的数据结构题了,5k多的代码加上随手写挂细节,我调了两天 题面见https://www.luogu.org/problemnew/show/P2042 (歪个题,这 ...
- BOM笔记
目录 BOM (浏览器对象模型) 简介 window对象 子对象 history 子对象 lacation 子对象 navigator 子对象 screen 子对象 frames BOM (浏览器对象 ...
- 二分-B - Dating with girls(1)
B - Dating with girls(1) Everyone in the HDU knows that the number of boys is larger than the number ...
- 回环屏障CyclicBarrier
上一篇说的CountDownLatch是一个计数器,类似线程的join方法,但是有一个缺陷,就是当计数器的值到达0之后,再调用CountDownLatch的await和countDown方法就会立刻返 ...
- Python读取execl表格
读取execl表格 import xlrd Execl = xlrd.open_workbook(r'Z:\Python学习\python26期视频\day76(allure参数.读excel.发邮件 ...
- 【网站】 简单通用微信QQ跳转浏览器打开代码
使用方法: 将代码全部复制 粘贴到 网站根目录下index.php文件的顶端 注意:不要覆盖了 index.php里面的原代码,原代码保留 使用说明: 手机QQ内打开,会自动跳转浏览器: 微信内打开, ...
- Spring整合Mybatis错误解决方案
ERROR:java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransactionFactory. ...
- C#中获取时间戳
{ 注意:下面是以毫秒为单位的13位 UTC 时间戳(非正规) }//先取得当前的UTC时间,然后转换成计算用的周期数(简称计时周期数),每个周期为100纳钞(ns)=0.1微秒(us)=0.00 ...