当前位置:网站首页>Force buckle: 1-sum of two numbers
Force buckle: 1-sum of two numbers
2022-07-24 06:12:00 【It's really all right, duck】
Title Description
Given an array of integers nums And an integer target value target, Please find... In the array And is the target value target the Two Integers , And return their array subscripts .
You can assume that each input corresponds to only one answer . however , The same element in the array cannot be repeated in the answer .
You can return the answers in any order .
Example 1:
Input :nums = [2,7,11,15], target = 9
Output :[0,1]
explain : because nums[0] + nums[1] == 9 , return [0, 1] .
Example 2:Input :nums = [3,2,4], target = 6
Output :[1,2]
Example 3:Input :nums = [3,3], target = 6
Output :[0,1]Tips :
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
There will only be one valid answer
Their thinking
The first thing I think of when I see this problem is violence , Direct two layers for Circulation can solve the problem , The time complexity is O(n²), Violence can generally be done , I'm not going to repeat it here . So here is the second idea , Use hash table to do this problem , The time complexity of hash table is only O(1), First floor for Circulation can solve , stay O(n) You can solve this problem with the complexity of
We know that hash table is a mapping relationship , That is, key value pairs , So let's go through the array , Take the value of the array as the key of the hash table , The subscript of the array is mapped as the value of the hash table . Then traverse the array again , We need to determine whether there is a corresponding value in the hash table plus the value of the current array equals target, And pay attention to one condition : The answer cannot appear in the same element , So this requires us to exclude the current element .
Code implementation
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int a,b;
// Create hash table
unordered_map<int,int>q;
for(int i=0;i<nums.size();i++)
q[nums[i]]=i;
for(int i=0;i<nums.size();i++)
{
// Judge whether there is a corresponding value and exclude the value of the current array
if(q.count(target-nums[i])!=0&&q[target-nums[i]]!=i)
{
a=i,b=q[target-nums[i]];
break;
}
}
return {a,b};
}
};边栏推荐
猜你喜欢

使用Qt连接MySql并创建表号、写入数据、删除数据

The detailed process of connecting MySQL with QT and outputting data to QT window tablewidget.

MySQL foundation - constraints

unity2D横版游戏跳跃实时响应

碰壁记录(持续更新)

Typora installation package in November 2021, the last free version of the installation package to download v13.6.1

day-7 jvm完结

day2-WebSocket+排序

简单却好用:使用Keras 2实现基于LSTM的多维时间序列预测

【数据库系统原理】第五章 代数和逻辑查询语言:包、扩展操作符、关系逻辑、关系代数与Datalog
随机推荐
通道注意力与空间注意力模块
unity2D横版游戏跳跃实时响应
day2-WebSocket+排序
Unity 3D帧率统计脚本
day5-jvm
Vscode multiline comments always expand automatically
Raspberry pie is of great use. Use the campus network to build a campus local website
STM32 DSP library MDK vc5\vc6 compilation error: 256, (const float64_t *) twiddlecoeff64_ 256, armBitRevIndexTableF64_ 256,
Jupyter notebook select CONDA environment
Opencv reads avi video and reports an error: number < Max_ number in function ‘icvExtractPattern
day1-jvm+leetcode
机器学习&深度学习 入门资料分享总结
常见十大漏洞总结(原理、危害、防御)
Openpose Unity 插件部署教程
MySQL基础---约束
Dameng database_ Various methods of connecting databases and executing SQL and scripts under disql
STM32 standard peripheral Library (Standard Library) official website download method, with 2021 latest standard firmware library download link
JVM system learning
Dameng database_ User password policy
MySql下载,及安装环境设置