[算法练习]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 输入多组数据 输 ...
随机推荐
- php7 中?? 和 ?:的区别
$b = $a?? $c ;相当于$b= isset($a)?$a:$c; $b = $a?: $c ;则是 $b = !empty($a) ? $a:$c;
- JavaScript对象基础知识总结
1.什么叫JavaScript对象? 定义:名值对的集合.简单的讲就是容纳属性值和属性值的容器,这些属性可以是无序的,基本上JavaScript中所有的事物都可以看成对象. 拓展:我们经常说,数组也是 ...
- 论文阅读 | FoveaBox: Beyond Anchor-based Object Detector
论文阅读——FoveaBox: Beyond Anchor-based Object Detector 概述 这是一篇ArXiv 2019的文章,作者提出了一种新的anchor-free的目标检测框架 ...
- vue-cli 启动过项目步骤
一. 安装 node.js 安装完成后,可以命令行工具中输入 node -v 和 npm -v,如果能显示出版本号,就说明安装成功. 二.安装webpack npm install webpack - ...
- Python编程中报过的错
一.TypeError: not all arguments converted during string formatting def max(*args): print('max2:%s' % ...
- spark第九篇:Spark操作ES
spark操作es需要elasticsearch-hadoop-xxx.jar,版本需同es版本.从5.0版本开始,支持spark2.0. 把elasticsearch-hadoop-xxx.jar放 ...
- Winform取消用户按下键盘的事件
1. 有时候针对某个控件,想取消它的所有键盘按下事件, 只需要为这个控件绑定keyDown事件即可,然后处理的代码如下: private void txtDeviceName_KeyDown(obje ...
- spring 3.0 整合redis
参考文章:https://blog.csdn.net/weixin_42184707/article/details/80361464 其中遇到了问题,第一,redis的xml配置文件中的,头部地址资 ...
- 九度oj 1032 ZOJ 2009年浙江大学计算机及软件工程研究生机试真题
题目1032:ZOJ 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4102 解决:2277 题目描述: 读入一个字符串,字符串中包含ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出,当 ...
- 一个骚气的前端JS代码生成网站
生成Javascript 颜文字代码 稍微试了试 原本的代码: alert("Hello, JavaScript") 转换后代码 ゚ω゚ノ= /`m´)ノ ~┻━┻ //*´∇`* ...