【LeetCode】001. TwoSum
题目:
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.
Example:
Given nums = [, , , ], target = , Because nums[] + nums[] = + = ,
return [, ].
题解:
首先想到暴力解,TLE(Time Limit Exceeded)问题先不考虑。从数组头开始,依次与其他元素比较,然后向后移动开始位置,需要注意的是返回的位置是从0开始的
Solution 1 (TLE)
class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
vector<int> result;
int n = numbers.size();
for (int i = ; i < n; i++) {
for (int j = i + ; j < n; j++) {
if (numbers[i] + numbers[j] == target) {
result.push_back(i);
result.push_back(j);
}
}
}
return result;
}
};
那怎样才能降低时间复杂度呢?这里有个经验准则,数组及字符串问题往往与(unordered)map、(unordered)set有关。这里使用unordered_map,用于查找;首先想到的思路是先遍历一遍数组,建立map映射表(元素值和位置的映射),然后再遍历一遍查找与当前元素之和为目标值的元素。建立m(数组元素)与m的位置的映射。
Solution 2
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> m;
int n = nums.size();
vector<int> result;
for (int i = ; i < n; ++i) {
m[nums[i]] = i;
}
for (int i = ; i < n; ++i) {
int t = target - nums[i];
//m.find(t) != m.end() 可以用 m.count(t)代替
//you may not use the same element twice.
if (m.find(t) != m.end() && m[t] != i) {
result.push_back(i);
result.push_back(m[t]);
break;
}
}
return result;
}
};
有一种优化方法,思路是从数组头开始,依次将元素对应的加数加入map中,在建立映射的过程中,若发现map中已经存在了一个元素,由于这个元素的存在是之前的操作才加入到map中,及此元素对应的加数在之前已经存在,故直接返回即可。题设 each input would have exactly one solution。建立target-m与m的位置的映射。
Solution 3
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> m;
vector<int> result;
for(int i=; i<nums.size(); i++)
{
if (m.find(nums[i])==m.end() )
{
m[target - nums[i]] = i;
}
else
{
result.push_back(m[nums[i]]);
result.push_back(i);
break;
}
}
return result;
}
};
【LeetCode】001. TwoSum的更多相关文章
- 【LeetCode】001. Two Sum
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
随机推荐
- Swift中字典解析后的问题,!?两种拆包的差别
给出一个json,使用SwiftyJSON解析传给model,传进去是个字典,字典里有String,NSNumber,NSDoctionary,和NSArray. 正常情况下直接使用下面的解析方法即可 ...
- $Java设计模式之——观察者模式(Observer)
(一)观察者模式简介 1.定义:定义对象间一种一对多的依赖关系,一个对象状态发生改变时,所有依赖它的对象都会接到通知并作出相应的响应. 2.应用场景: (1)GUI系统 (2)订阅-发布系统 (3)事 ...
- Linux Shell基础 Shell基本知识
概述 在 Linux 的脚本中,只要是基于 Bash语法写的Shell脚本第一行必须是"#!/bin/bash",用来声明此文件是一个脚本. 运行方式 Shell 脚本的运行主要有 ...
- python对象类型----数字&字符串
一数据类型: float: 1.3e-3 1.3*10的负三次方 print (1.3e-3) bin() #转换为二进进制 oct() #转换为8进制 hex()#转 ...
- 登陆weblogic后页面控制台卡主
输入http://localhost:7001/console进入控制页面,能登陆进去,但是登陆进去后页面就马上卡死,可以看到页面头部,其余都显示不出来. 重启后启动访问,能够正常进入,关闭weblo ...
- uiwebview 加载本地js、css、img,html从网站加载
资源文件都是放在根目录下 1.index.html <html> <head> <title>My test Page</title> <link ...
- Ubuntu 12.04下安装OpenCV 2.4.2
http://sourceforge.net/projects/opencvlibrary/files/ Ubuntu 12.04下安装OpenCV 2.4.2 http://blog.csdn.ne ...
- MongoDB快速入门(七)- Save() 方法
MongoDB Save() 方法 save() 方法取代,通过新文档到 save()方法 语法 mongodb 的 save()方法如下所示的基本语法: >db.COLLECTION_NAME ...
- Spring的AOP面向切面编程
什么是AOP? 1.AOP概念介绍 所谓AOP,即Aspect orientied program,就是面向方面(切面)的编程. 功能: 让关注点代码与业务代码分离! 关注点: 重复代码就叫做关注点: ...
- Apache Phoenix的序列
序列作为标准SQL特性,允许生成递增的序列并应用在典型的ID中.为了创建一个序列,可以使用: 0:jdbc:phoenix:SZB-L0023780:2181:/hbase114> CREATE ...