LeetCode 之 TwoSum
题目:
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
C++解答一(传统方法,Runtime: 19 ms):
struct Node
{
int num;
int pos;
}; bool cmp(Node a,Node b)
{
return a.num<b.num;
} class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) { vector<int> result;
vector<Node> tmp;
Node a; for(int i = ;i<numbers.size();i++)
{
a.pos = i+;
a.num = numbers[i];
tmp.push_back(a);
} sort(tmp.begin(),tmp.end(),cmp);
int j = tmp.size()-;
int tmpvalue = ; for(int i = ;i<tmp.size();)
{
tmpvalue = tmp[i].num+tmp[j].num;
if(tmpvalue==target)
{
if(tmp[i].pos<tmp[j].pos)
{
result.push_back(tmp[i].pos);
result.push_back(tmp[j].pos);
}
else
{
result.push_back(tmp[j].pos);
result.push_back(tmp[i].pos);
} break;
}
else if(tmpvalue>target)
{
j--;
}
else
{
i++;
}
} return result;
}
};
C++ 解答二(巧妙方法,Runtime: 35 ms):
class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
int i, sum;
vector<int> results;
map<int, int> hmap;
for(i=; i<numbers.size(); i++){
if(!hmap.count(numbers[i])){
hmap.insert(pair<int, int>(numbers[i], i));
}
if(hmap.count(target-numbers[i])){
int n=hmap[target-numbers[i]];
if(n<i){
results.push_back(n+);
results.push_back(i+);
return results;
} }
}
return results;
}
};
LeetCode 之 TwoSum的更多相关文章
- LeetCode #1 TwoSum
Description Given an array of integers, return indices of the two numbers such that they add up to a ...
- Leetcode 1——twosum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- leetcode之twosum
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { vector& ...
- leetcode ----ARRAY TWOSUM
代码的(判断nums[i]或者是target-nums[i]都可以):
- [LeetCode_1] twoSum
LeetCode: 1. twoSum 题目描述 Given an array of integers, return indices of the two numbers such that the ...
- LeetCode 算法题解 js 版 (001 Two Sum)
LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...
- ANDROID学习书单
Skip to content PersonalOpen sourceBusinessExplore Sign upSign in PricingBlogSupport This reposito ...
- Android技能树
第一部分:Android(安卓)Android基础知识Android内存泄漏总结Handler内存泄漏分析及解决Android性能优化ListView详解RecyclerView和ListView的异 ...
- LeetCode初体验—twoSum
今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...
随机推荐
- 170221、浅谈mysql的SQL的四种连接
例子: ------------------------------------------------- a表 id name b表 id job pare ...
- Openstack创建镜像
如何创建生产用的Openstack镜像 参考官方文档https://docs.openstack.org/image-guide/centos-image.html 1,创建虚拟机硬盘 qemu-im ...
- jsp 通用获取所有表单值传后台
新建一个js文件,自定义一个jquery 函数. 在jsp页面引用 下面为:自定义函数 $.fn.GetDivJson = function (prifix,orgModel) { var $oute ...
- 三、直播整体流程 五、搭建Nginx+Rtmp直播流服务
HTML5实现视频直播功能思路详解_html5教程技巧_脚本之家 https://m.jb51.net/html5/587215.html 三.直播整体流程 直播整体流程大致可分为: 视频采集端:可以 ...
- <2014 05 21> 互联网时代的C语言——Go
Go希望成为互联网时代的C语言. 多数系统级语言(包括Java和C#)的根本编程哲学来源于C++,将C++的面向对象进一步发扬光大.但是Go语言的设计者却有不同的看法,他们认为C++ 真的没啥好学的, ...
- CNI插件实现框架---以loopback为示例
以最简单的loopback插件作为实例,来分析CNI plugin的执行流程 // cni/plugins/loopback/loopback.go 1.func main() main函数只是简单地 ...
- Django页面重定向
重定向分为永久性重定向和暂时性重定向,在页面上体现的操作就是浏览器会从一个页面自动跳转到另外一个页面.比如用户访问了一个需要权限的页面,但是该用户当前并没有登录,因此我们应该给他重定向到登录页面. 永 ...
- Python高级教程-sorted
Python中的排序算法 排序是程序中经常用到的算法.通常规定,对于两个元素x和y,如果认为x<y,则返回-1,如果认为x == y,则返回0,如果认为x > y,则返回1,这样,排序算法 ...
- Flask 请求源码分析
执行app.run()方法: def run(self, host=None, port=None, debug=None, **options): from werkzeug.serving imp ...
- pytharm提示过期 License Activation 解决办法
遇到如下问题: 打开网站: http://idea.lanyus.com/ next next ok