题目:

LeetCode:1. Add Two Numbers

描述:

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

样例:

Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

分析:

  • 思路如下:

    1) 利用hashmap来存储数组元素以及其对应索引值,提高检索效率

    2) 通过nTarget - vecNum[i]来获取对应的检索目标元素

    3) 返回满足条件的索引值

代码:

vector<int> twoSum(vector<int>& vecNum, int nTarget) {
vector<int> vecTemp;
unordered_map<int, int> hashMapTemp; for (int i = 0; i < vecNum.size(); i++)
{
hashMapTemp[vecNum[i]] = i;
} for (int i = 0; i < vecNum.size(); i++)
{
const int nPart = nTarget - vecNum[i];
if (hashMapTemp.find(nPart) != hashMapTemp.end() && hashMapTemp[nPart] > i)
{
vecTemp.push_back(i);
vecTemp.push_back(hashMapTemp[nPart]);
break;
}
} return vecTemp;
}

备注:

LC上大神的 0ms 的代码:

vector<int> twoSum(vector<int> &numbers, int target)
{
//Key is the number and value is its index in the vector.
unordered_map<int, int> hash;
vector<int> result;
for (int i = 0; i < numbers.size(); i++) {
int numberToFind = target - numbers[i]; //if numberToFind is found in map, return them
if (hash.find(numberToFind) != hash.end()) {
//+1 because indices are NOT zero based
result.push_back(hash[numberToFind] + 1);
result.push_back(i + 1);
return result;
} //number was not found. Put it in the map.
hash[numbers[i]] = i;
}
return result;
}

LeetCode:1. Add Two Numbers的更多相关文章

  1. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  2. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  3. LeetCode 面试:Add Two Numbers

    1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  4. LeetCode #002# Add Two Numbers(js描述)

    索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...

  5. [Leetcode Week15] Add Two Numbers

    Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...

  6. [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

    [LeetCode] Add Two Numbers 两个数字相加   You are given two non-empty linked lists representing two non-ne ...

  7. [LeetCode] 2. Add Two Numbers 两个数字相加

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  8. LeetCode之Add Two Numbers

    Add Two Numbers 方法一: 考虑到有进位的问题,首先想到的思路是: 先分位求总和得到 totalsum,然后再将totalsum按位拆分转成链表: ListNode* addTwoNum ...

  9. LeetCode 2. add two numbers && 单链表

    add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...

随机推荐

  1. 关于js参数传递矛盾新理解

    之前看了很多人的解释,说js中,函数的参数传递都是值传递中不理解. 他们无非举了两个例子 在这两个例子中,第二个例子可以看出参数是由值传递的.因为函数内对象的变化没有影响到函数外对象的变化.但是在第一 ...

  2. unity插件开发——一个例子:简单的svn集成

    在unity开发过程中,通常我们习惯性地在Windows操作系统下使用svn进行版本管理,而每次提交更新,都需要回到文件夹下的这种操作让人无法忍受.是不是可以集成svn到unity中呢?查了一圈uni ...

  3. Redis中的master-slave&sentinel

    redis安装 解压完成后可以看到INSTALL和README.md文件,查看以获取更多有用信息. 在README文件中可以获取到软件的安装步骤.以下安装步骤基于此. #step1 进入文件夹,执行编 ...

  4. div内部元素居中

    要让div内部元素垂直居中,则给div加上此css样式: .div-vertical-middle{  height:200px;  width:304px;  line-height:50px;  ...

  5. 【Egret】Lakeshore 使用中的一些疑难解决技巧!

    用Lakeshore 1.2.1版本发布的html,会出现一些用户不想要的东西,下面讲讲如何去掉: 一.问题:游戏或者动画在PC端也能跟随游览器自适应. 解决方法:①找到发布文件下的  egret_l ...

  6. 手机自动化测试:搭建appium手机自动化测试开发环境

    手机自动化测试:搭建appium手机自动化测试开发环境   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大 ...

  7. 什么东西那么吸引别人的眼球!! -----------------------------------for循环

    认识for循环结构 在编码过程中,把一些重复执行代码采用循环结构进行描述,可以大大减化编码工作, 使得代码更加简洁.宜都... 1.     为什么要用for? 比如: 老师叫小明统计全班人的编号,小 ...

  8. 原生JS跨浏览器事件封装处理

    引子:用javascript给元素绑定事件,我们可以用addEventListener这个方法,然而这个方法有兼容问题,比如在IE浏览器上面就无效,在IE上面要用attachEvent这个方法 一.a ...

  9. 一个简单的jquery左右列表内容切换应用

    选中左边某个选项点击添加,即可将选中项添加到右边文本框中,点击选中全部即可将全部选项移到右边,移除按钮功能相同. html代码: <div id="main"> < ...

  10. 最新的css3动画按钮效果

    效果演示     插件下载