[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,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
随机推荐
- linux - python - 异常:error while loading shared libraries
问题描述 error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No s ...
- createElement(九)
Vue.js 利用 createElement 方法创建 VNode,它定义在 src/core/vdom/create-elemenet.js 中: // wrapper function for ...
- Spring-JDBCTemplate介绍
一.Spring对不同的持久化支持: Spring为各种支持的持久化技术,都提供了简单操作的模板和回调 ORM持久化技术 模板类 JDBC org.springframework.jdbc.c ...
- 126. 单词接龙 II
题目: 链接:https://leetcode-cn.com/problems/word-ladder-ii/ 给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出 ...
- Selenium原理
from selenium import webdriver:导入webdriver模块 当导入webdriver模块时,会执行\selenium\webdriver目录下的__init__.py ...
- 到头来还是逃不开Java - Java13核心类
Java13核心类 没有特殊说明,我的所有学习笔记都是从廖老师那里摘抄过来的,侵删 引言 兜兜转转到了大四,学过了C,C++,C#,Java,Python,学一门丢一门,到了最后还是要把Java捡起来 ...
- 记录 shell学习过程(9)正则表达式 转自树明聊运维
正则表达式 正则表达式介绍 特殊字符 POSIX特殊字符 一.正则表达式介绍 正则表达式是一种文本模式匹配,包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为"元字符") ...
- c#中的强类型、弱类型和泛型
强类型和弱类型的变量都有两个属性:类型和值. 强类型的变量类型是不能改变的,弱类型的变量类型是随需改变的,这是强弱的真正含义. 我们在编写c#代码时,变量类型是明确的,不可更改的,如string就是s ...
- Oracle Data Guard搭建 1.虚拟机安装linux
1.安装虚拟机 VMware 14 2.下载Linux镜像文件,创建虚拟机
- K-NN graph
tasks: 1. unsupervised knn https://scikit-learn.org/stable/modules/neighbors.html#unsupervised-neigh ...