LeetCode1 Two Sum
题目 :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. (Easy)
解法1: Two pointers
拷贝一份,将数组排序,两根指针分别从前后向中间扫描,找到解为止。再遍历原数组寻找下标添加到结果内。
复杂度: O(nlogn)
注意:比如0,3,4,0 target = 0这组数据,需要让result第二个参数从后向前扫描才能保证答案正确。
代码:
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> v(nums);
vector<int> result;
sort(nums.begin(), nums.end());
int i = ,j = nums.size() - ;
while (nums[i] + nums[j] != target) {
if (nums[i] + nums[j] > target) {
j--;
}
else {
i++;
}
}
for (int k = ; k < nums.size(); ++k) {
if (v[k] == nums[i]) {
result.push_back(k);
break;
}
}
for (int k = nums.size() - ; k >= ; --k) { //从后向前扫描
if (v[k] == nums[j]) {
result.push_back(k);
break;
}
}
return result;
}
};
解法2:
利用hash表建立数值与下标的一一对应,扫描一遍即得解。
注: 本以为是O(n),但运行时间比解法1居然慢,查看discuss得知没有注意find()方法在最差情况下执行效率是O(n)的。
代码:
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
//数值 与 下标一一对应
unordered_map<int,int> hash;
vector<int> result;
for (int i = ; i < nums.size(); ++i){
if (hash.find(target - nums[i]) != hash.end()) {
result.push_back(hash[target - nums[i]]);
result.push_back(i);
return result;
}
hash[nums[i]] = i;
}
return result;
}
};
LeetCode1 Two Sum的更多相关文章
- leetcode-1 Two Sum 找到数组中两数字和为指定和
问题描写叙述:在一个数组(无序)中高速找出两个数字,使得两个数字之和等于一个给定的值.如果数组中肯定存在至少一组满足要求. <剑指Offer>P214(有序数组) <编程之美& ...
- Leetcode--1. Two Sum(easy)
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- [Swift]LeetCode1 .两数之和 | Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- LeetCode-1:Two Sum
[Problem:1-Two Sum] Given an array of integers, return indices of the two numbers such that they add ...
- LeetCode1:Two Sum
题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...
- leeetcode1171 Remove Zero Sum Consecutive Nodes from Linked List
""" Given the head of a linked list, we repeatedly delete consecutive sequences of no ...
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
随机推荐
- gradle gradlew 的使用
jcenter() 仓库比 mavenCentral() 仓库快,因此最好将jcenter 放前面,这样下载速度最快. 使用本地软件仓库:repositories { flatDir { dirs ' ...
- c/c++,输入一个字符 2014-11-20 07:00 30人阅读 评论(0) 收藏
getch().getche()和getchar()函数 (1) getch()和getche()函数 这两个函数都是从键盘上读入一个字符.其调用格式为: getch(); ...
- c语言中用宏定义一个常量,数字后面带个U, L, F的含义
转: c语言中数字后面带个U是什么意思?#define F_CPU 12000000U答:U表示该常数用无符号整型方式存储,相当于unsigned int;L表示该常数用长整型方式存储,相当于long ...
- JS制作的简单的三级及联
前台: <form id="form1" runat="server"> <div> 省 <select id="Pro ...
- win2008 64位 + oracle11G 64位 IIS7.5 配置WEBSERVICE
第一个错误: 安装过程依旧是那样简单,但在配好IIS站点,准备连接数据库的时候出错了,以下是错误提示:System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更 ...
- UVaLive 6801 Sequence (计数DP)
题意:给定一个序列,有 n 个数,只有01,然后你进行k次操作,把所有的1变成0,求有多种方法. 析:DP是很明显的,dp[i][j] 表示进行第 i 次操作,剩下 j 个1,然后操作就两种,把1变成 ...
- Unity3d:加载Gif格式图片
unity里不支持Gif格式的图片,网上搜索也没有相关资料,殊不知我们已经太相信度娘了,而没有了自己的分析,我们知道Gif图是由多个静态图做成的,那我们就回归本土,第一步:把gif拆成n个静态图放在集 ...
- JS:中文GB2312编码
今天开发遇到了个问题,有点纳闷.... 在ajax的时候要传递一个中文值,不管我在js中是否使用了encodeURI.encodeURIComponent编码,但是在后台request获取的值是始终是 ...
- Use jQuery to hide a DIV when the user clicks outside of it
http://stackoverflow.com/questions/1403615/use-jquery-to-hide-a-div-when-the-user-clicks-outside-of- ...
- 检测一个DOM对象是否为空
我们时常要检测一个DOM对象是否为空. var $jObject = $('#btn'); alert($jObject ); 我们会发现,$jObject 永远不会为空.为什么呢?$ 方法查找对象, ...