[算法练习]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.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
程序代码:
#include <gtest/gtest.h>
#include <map>
#include <vector>
#include <algorithm>
using namespace std; // 暴力遍历
vector<int> twoSum1(vector<int>& nums, int target)
{
vector<int> results;
for (int i=0; i< nums.size(); ++i)
{
for (int j=i+1; j < nums.size(); ++j)
{
if (nums[i]+nums[j] == target)
{
results.push_back(i);
results.push_back(j);
break;
}
}
} return results;
}; // 缓存映射
vector<int> twoSum2(vector<int>& nums, int target)
{
vector<int> results;
map<int, vector<int> > cache;
bool find = false; for (unsigned int i=0; i<nums.size(); ++i)
{
cache[nums[i]].push_back(i);
} for (unsigned int i=0; i<nums.size(); ++i)
{
map<int, vector<int> >::const_iterator it = cache.find(target-nums[i]);
if (it != cache.end())
{
for (unsigned int j = 0; j< it->second.size(); ++j)
{
if (i != it->second[j])
{
results.push_back(i);
results.push_back(it->second[j]);
find = true;
break;
}
} if (find)
{
break;
}
}
} return results;
}; // 排序后查找
struct Node
{
int index;
int value; Node(int i, int v)
{
index = i;
value = v;
} bool operator < (Node& ref)
{
return value < ref.value;
}
}; vector<int> twoSum(vector<int> &nums, int target)
{
vector<int> results;
vector<Node> cache; for (int i=0; i<nums.size(); ++i)
{
cache.push_back(Node(i, nums[i]));
} sort(cache.begin(), cache.end()); for (int i = 0, j = cache.size() - 1; i < j;)
{
int value = cache[i].value + cache[j].value;
if (value == target)
{
results.push_back(min(cache[i].index, cache[j].index));
results.push_back(max(cache[i].index, cache[j].index));
break;
}
else if (value > target)
{
--j;
}
else
{
++i;
}
} return results;
}; TEST(Pratices, tTwoSum)
{
// [3, 3, 1, 2] 3 => [0, 1]
// [2, 7, 11, 15] 9 => [0, 1]
// [-1, 8, 10, 12, 40, 11] 10 => [0,5] vector<int> data;
data.push_back(3);
data.push_back(3);
data.push_back(1);
data.push_back(2);
vector<int> results = twoSum(data,6); data.clear();
data.push_back(2);
data.push_back(7);
data.push_back(11);
data.push_back(15);
results = twoSum(data,9); data.clear();
data.push_back(-1);
data.push_back(8);
data.push_back(10);
data.push_back(12);
data.push_back(40);
data.push_back(11);
results = twoSum(data,10);
}
[算法练习]Two Sum的更多相关文章
- LeetCode算法题-Two Sum IV - Input is a BST(Java实现)
这是悦乐书的第280次更新,第296篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第148题(顺位题号是653).给定二进制搜索树和目标数,如果BST中存在两个元素,使得 ...
- LeetCode算法题-Path Sum III(Java实现)
这是悦乐书的第227次更新 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第94题(顺位题号是437).您将获得一个二叉树,其中每个节点都包含一个整数值.找到与给定值相加的路径数 ...
- LeetCode算法题-Range Sum Query Immutable(Java实现)
这是悦乐书的第204次更新,第214篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第70题(顺位题号是303).给定整数数组nums,找到索引i和j(i≤j)之间的元素之 ...
- LeetCode算法题-Two Sum II - Input array is sorted
这是悦乐书的第179次更新,第181篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第38题(顺位题号是167).给定已按升序排序的整数数组,找到两个数字,使它们相加到特定 ...
- LeetCode算法题-Path Sum(Java实现)
这是悦乐书的第169次更新,第171篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第28题(顺位题号是112).给定二叉树和整数sum,确定树是否具有根到叶路径,使得沿路 ...
- 每日一算法之two sum
题目如下:首先准备一个数组,[1,2,8,4,9] 然后输入一个6,找出数组两项之和为6的两个下标. 啥也不想,马上上代码,这个太简单了, static int[] twoSum(int[] num ...
- 【LeetCode 1】算法修炼 --- Two Sum
Question: Given an array of integers, find two numbers such that they add up to a specific target nu ...
- 【算法】LeetCode算法题-Two Sum
程序 = 数据结构 + 算法. 算法是每一位程序员学习成长之路上无法避开的重要一环,并且越早接触越好.今后会每天做些算法题,至少每天做一道题目,同时会记录自己的解题思路和代码,通过[算法]专题来分享. ...
- 算法(10)Subarray Sum Equals K
题目:在数组中找到一个子数组,让子数组的和是k. 思路:先发发牢骚,这两天做题是卡到不行哇,前一个题折腾了三天,这个题上午又被卡住,一气之下,中午睡觉,下午去了趟公司,竟然把namespace和cgr ...
- 1192: 零起点学算法99——The sum problem(C)
一.题目 http://acm.wust.edu.cn/problem.php?id=1192&soj=0 二.分析 要求从序列1,2,3,,,N,中截取一部分使他们的和为M 输入多组数据 输 ...
随机推荐
- 「Nosql」Redis小记-内存解析&内存消耗篇
*博客搬家:初版发布于 2017/08/12 18:32 原博客地址:https://my.oschina.net/sunqinwen/blog/1507171 Redis内存消耗分析 注:本文 ...
- [原创]SpringBoot上传图片踩的坑
最近项目里面有个需求,要上传图片到阿里云的OSS服务.所以需要写个上传图片的接口给前端. 这个简单的接口本来就给分配了1个工时,感觉也蛮简单的.但编码过程中遇到了好几个问题,现在一一记录下来,避免再次 ...
- L1-2 倒数第N个字符串 (15 分)真坑
给定一个完全由小写英文字母组成的字符串等差递增序列,该序列中的每个字符串的长度固定为 L,从 L 个 a 开始,以 1 为步长递增.例如当 L 为 3 时,序列为 { aaa, aab, aac, . ...
- [转] Ansible 进阶 | facts 缓存
[From] https://blog.csdn.net/bruce_6/article/details/81328975 什么是 Ansible factsAnsible facts 是远程系统的信 ...
- Shiro源码解析-登录篇
本文以循序渐进的方式解析Shiro整个login过程的处理,读者可以边阅读本文边自己看代码来思考体会,如有问题,欢迎评论区探讨! 笔者shiro的demo源码路径:https://github.com ...
- border.css(解决移动端1px问题)
由于某些机型分辨率过高,会导致1px变成2-多px像素的问题,引用bordercss解决 @charset "utf-8"; .border, .border-top, .bord ...
- Python练习 | Web本质Socket
#--------------------------------客户端-------------------------------------- # 导入socket库 import socket ...
- unittest之装饰器
前面讲到 unittest 里面 setUp 可以在每次执行用例前执行,这样有效的减少了代码量,但是有个弊端,比如打开浏览器操作,每次执行用例时候都会重新打开,这样就会浪费很多时间.于是就想是不是可以 ...
- how to be an efficient man
"This Monday I was invited to do a presentation on this Friday, and today is Friday. I am going ...
- android actionbar viewpager 实现类微信主界面布局
1 Activity public class MainActivity extends FragmentActivity { private ViewPager pager; private Act ...