当前位置:网站首页>Sum of two numbers
Sum of two numbers
2022-04-21 10:27:00 【Gxy_ w】
subject : 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 .
Test cases :
Input :nums = [2,7,11,15], target = 9
Output :[0,1]
explain : because nums[0] + nums[1] == 9 , return [0, 1] .
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
std::unordered_map <int,int> map;
for(int i = 0; i < nums.size(); i++) {
auto iter = map.find(target - nums[i]);
if(iter != map.end()) {
return {
iter->second, i};
}
map.insert(pair<int, int>(nums[i], i));
}
return {
};
}
};
版权声明
本文为[Gxy_ w]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211024199829.html
边栏推荐
猜你喜欢
随机推荐
ConvNeXt
JS initial practice -- an example of dealing with the collision between a pinball and a wall
Jeecgboot: the difference between online form control drop-down box and drop-down search box
24张图攻克border-image
DNS域名系统-因特网的目录服务
L1-047 装睡 (10 分)
Zsh: segmentation fault solution
Microsoft updates the verifier application for Android / IOS to support the generation of more secure strong passwords
HMS Core 6.4.0版本发布公告
阿里云移动研发平台EMAS,3月产品动态
微信公众号网页分享设置及问题
Construction of mobile communication platform (voice visual screen, sending and receiving SMS)
【并发编程044】CAS循环时间太长,会有什么问题?
SAP ABAP FOR ALL ENTRIES 的用法
jeecgboot:online表单控件下拉框和下拉搜索框的区别
Pytorch学习笔记(2)一元线性回归、计算图示例
MySQL8.0学习记录07 - 数据类型之JSON
我再测试下定时的
ROS and MATLAB network connection
VS 2019中使用qt









