[算法练习]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 输入多组数据 输 ...
随机推荐
- vue.js学习笔记(一)——vue-cli项目的目录结构
vue.js是一套构建用户界面的渐进式框架.vue采用自底向上增量开发的设计.vue的核心库只关心视图层,非常容易学习,非常容易与其它库和已有项目整合.vue完全有能力驱动采用单文件组件和vue生态系 ...
- sql2008R2新建链接服务器。
1:用sql新建链接服务器对象: /****** Object: LinkedServer [pad] Script Date: 10/23/2018 15:47:45 ******/ EXEC ma ...
- centsos 查看系统版本信息
[root@hostuser gitlab]# lsb_release -a LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cx ...
- 使用Django-environ管理多个配置
使用Django-environ管理多个配置 https://django-environ.readthedocs.io/en/latest/
- 关于halo博客系统的使用踩坑——忘记登录密码
踩坑: halo系统可以直接通过运行jar -jar halo-0.0.3.jar跑起来,也可以通过导入IDE然后运行Application的main方法跑起系统. h2数据库访问路径:http:// ...
- (转)最新版 nginx内置变量 大全
原文:http://www.cnphp.info/nginx-embedded-variables-lasted-version.html 在配置基于nginx服务器的网站时,必然会用到 nginx内 ...
- 资料整理:基于node push server实现push notification
chat example based on ionic/ socket.io/ redis https://github.com/jbavari/ionic-socket.io-redis-chat ...
- MySQL通过SQL语句来直接生成新表
1. 既复制表结构,也复制表数据 mysql> CREATE TABLE tmp_table SELECT * FROM dede_news; 说明:这种方法的缺点就是新表中没有了旧表的prim ...
- <数据挖掘导论>读书笔记7 Apriori算法
Apriori算法是一种最有影响的挖掘布尔关联规则频繁项集的算法.其核心是基于两阶段频集思想的递推算法.该关联规则在分类上属于单维.单层.布尔关联规则.在这里,所有支持度大于最小支持度的项集称为频繁项 ...
- Azure 上 Linux 虚拟机 Mac 地址的持久化
有些用户在使用 Azure Linux 虚拟机安装软件时,有些软件的 license 会和当前系统的 mac 地址绑定,那么在 Azure VM 重启,reszie(改变尺寸大小),停止然后再启动的时 ...