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

参考博客:

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. Android_ViewPager+Fragment实现页面滑动和底部导航栏

    1.Xml中底部导航栏由一个RadioGroup组成,其上是ViewPager. <?xml version="1.0" encoding="utf-8" ...

  2. 【转】idea远程调试

    适用于web服务,thrift服务 对于分布式系统的调试不知道大家有什么好的方法.对于我来说,在知道远程调试这个方法之前就是在代码中打各种log,然后重新部署,上线,调试,这样比较费时.今天咱们来了解 ...

  3. jsp报错java.io.IOException: Stream closed

    在使用jsp的时候莫名其妙的抛出了这个异常,经过反复检查 去掉了网友们说的jsp使用流未关闭,以及tomcat版本冲突等原因,最后发现是书写格式的原因. 当时使用的代码如下 <jsp:inclu ...

  4. Docker+JMeter+InfluxDB+Grafana从容器内部发起压测

    1.自由定制JMeter镜像: Dockerfile文件: FROM java:8# 基础镜像 MAINTAINER yangjianliang <526861348@qq.com># 作 ...

  5. 2.restEasy中@PathParam和@QueryParam的区别

    例如代码: @GET @Path("/{id}") @Produces(MediaType.APPLICATION_JSON) public T query(@PathParam( ...

  6. 小匠第二周期打卡笔记-Task04

    一.机器翻译及相关技术 机器翻译(MT):将一段文本从一种语言自动翻译为另一种语言,用神经网络解决这个问题通常称为神经网络翻译(NMT). 主要特征:输出是单词序列而不是单个单词.输出序列的长度可能与 ...

  7. 【网站】i新媒上线了!

    [New]i新媒上线了! i新媒,是新媒体人常用和必备的工具导航,我们整合了自媒体平台.行业资讯.运营营销.学习创业等常用的网站,让新媒体人更快地获取有用的知识. 访问链接:https://ixm.h ...

  8. Linux 开启orcale服务

    su - oracle  //切换到oracle用户模式下 sqlplus /nolog  //登录sqlplus connect /as sysdba; //连接orcale startup;   ...

  9. Tomcat解压版-环境配置

    [问题]Tomcat解压版在本地后,双击双击startup.bat,闪退 [解决办法]    1.在已解压的tomcat的bin文件夹下找到startup.bat,右击->编辑.在文件头加入下面 ...

  10. java Map 迭代key,value 最简洁的方法

    import java.util.HashMap; import java.util.Map; public class EntrySets { public static void main(Str ...