two Sum ---- LeetCode 001
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].
Solution 1:
class Solution
{
public:
vector<int> twoSum(vector<int>& nums, int target)
{
unordered_map<int, int> myMap;
vector<int> result; for(size_t i = ; i < nums.size(); ++i)
{
myMap[nums[i]] = i;
} for(size_t i = ; i < nums.size(); ++i)
{
const int gap = target - nums[i];
auto it = myMap.find(gap);
if(it != myMap.end() && it->second != i)
{
result.push_back(i);
result.push_back(myMap[gap]);
break; // Assume that each input would have
// exactly one solution
}
}
return result;
}
};
Solution 2: 暴力查找,超时
class Solution
{
public:
vector<int> twoSum(vector<int>& nums, int target)
{
vector<int> result;
for(size_t i = ; i < nums.size(); ++i)
{
for(size_t j = i + ; j < nums.size(); ++j)
{
if(nums[i] + nums[j] == target)
{
result.push_back(i);
result.push_back(j);
break;
}
}
}
return result;
}
};
Solution 3: 先排序,然后左右夹逼
class Solution
{
public:
vector<int> twoSum(vector<int>& nums, int target)
{
vector<int> result;
vector<Node> num;
for(size_t i = ; i < nums.size(); ++i)
{
Node temp;
temp.value = nums[i];
temp.pos = i;
num.push_back(temp);
}
sort(num.begin(), num.end(), cmp);
for(size_t i = , j = num.size() - ; i != j; )
{
int sum = num[i].value + num[j].value;
if(sum == target)
{
int smallPos = min(num[i].pos, num[j].pos);
int largePos = max(num[i].pos, num[j].pos);
result.push_back(smallPos);
result.push_back(largePos);
break; // 找到解后,中断
}
else if(sum > target) --j;
else ++i;
}
return result;
}
private:
struct Node
{
int value;
int pos;
};
static bool cmp(const Node &a, const Node &b)
{
return a.value < b.value;
}
};
two Sum ---- LeetCode 001的更多相关文章
- LeetCode #001# Two Sum(js描述)
索引 思路1:暴力搜索 思路2:聪明一点的搜索 思路3:利用HashMap巧解 问题描述:https://leetcode.com/problems/two-sum/ 思路1:暴力搜索 一个很自然的想 ...
- 【JAVA、C++】LeetCode 001 Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- [Leetcode][001] Two Sum (Java)
题目在这里: https://leetcode.com/problems/two-sum/ [标签]Array; Hash Table [个人分析] 这个题目,我感觉也可以算是空间换时间的例子.如果是 ...
- Path Sum [LeetCode]
Problem Description: http://oj.leetcode.com/problems/path-sum/ Pretty easy. /** * Definition for bin ...
- Leetcode 001. 两数之和(扩展)
1.题目要求 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 2.解法一:暴力法(for*for,O(n*n)) ...
- leetcode 001
1 Two Sum Difficulty: Easy The Link: https://leetcode.com/problems/two-sum/description/ Description ...
- 39. Combination Sum - LeetCode
Question 39. Combination Sum Solution 分析:以candidates = [2,3,5], target=8来分析这个问题的实现,反向思考,用target 8减2, ...
- Minimum Path Sum [LeetCode]
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- Nested List Weight Sum -- LeetCode 339
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
随机推荐
- STL中vector的用法
vector是标准模板库的一种容器,是可存放各种类型的动态数组. #include<iostream> #include<vector> using namespace std ...
- web基础之Structs(一篇)
为什么有 struts 框架 Struct 的优点之处: 1. struct的好处 2. 程序更加规范化 3. 程序的可读性提 ...
- hdu----(5056)Boring count(贪心)
Boring count Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- Reorder List [LeetCode]
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- URL和搜索引擎优化
URL要短:谷歌的算法是给5个以后的单词更少的权重 URL不含和少含问号:防止死循环 小写字母:搜索引擎遵守HTTP规范,区分大小写.
- Spring使用jdbcJdbcTemplate和三种方法配置数据源
三种方法配置数据源 1.需要引入jar包:spring-jdbc-4.3.2.RELEASE.jar <!-- spring内置,springJdbc,配置数据源 --> <bean ...
- VoltDB介绍——本质:数据保存在内存,充分利用CPU,单线程去锁,底层数据结构未知
转自:http://blog.csdn.net/ransom0512/article/details/50440316 简介 VoltDB数据库是一个分布式,可扩展,shared-nothing的内存 ...
- Windows内核对象
1. 内核对象 Windows中每个内核对象都只是一个内存块,它由操作系统内核分配,并只能由操作系统内核进行访问,应用程序不能在内存中定位这些数据结构并直接更改其内容.这个内存块是一个数据结构,其成员 ...
- PULL解析
PULL解析类似于SAX解析,都采用事件驱动(利用getEventType()方法)方式进行解析,当PULL解析器开始解析之后,可以不断地调用PULL解析器的next()方法获取下一个解析事件(开始文 ...
- Query的选择器中的通配符[id^='code']或[name^='code']
1.选择器 (1)通配符: $("input[id^='code']");//id属性以code开始的所有input标签 $("input[id$='code'] ...