/* c++ STL is much nore than what i think before in these aspects:
* initializer for node element in struct node;
* compare function in struct : overload operation
* index not iterator used in this function.
*/
class Solution {
public:
struct node{
int value;
int order;
node(int v,int index):value(v), order(index){}
};
struct {
bool operator()(node a, node b){ return a.value<=b.value; }
}compare;
vector<int> twoSum(vector<int>& nums, int target) { vector<int>re;
vector<node>sor;
for(int i=;i<nums.size();i++)sor.push_back(node(nums[i],i));
sort(sor.begin(),sor.end(),compare);
for(int tmp,i=,j=sor.size()-;i<j;)
{
tmp=sor[i].value+sor[j].value;
if(tmp== target)
{
re.push_back(sor[i].order);
re.push_back(sor[j].order);
break;
}
else if(tmp < target)i++;
else j--;
}
return re;
} };

look the solution in java from explantation on leetcode

/* so beautiful and clean
* hashmap..........
*/
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[] { map.get(complement), i };
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("No two sum solution");
}

Leetcode001 two sum的更多相关文章

  1. LeetCode-001 Two Sum

    [题目] Given an array of integers, find two numbers such that they add up to a specific target number. ...

  2. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  3. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  4. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  5. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  6. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  7. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  8. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  9. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

随机推荐

  1. ThreadPoolExecutor使用介绍

    private static ExecutorService exec = new ThreadPoolExecutor(8, 8, 0L,TimeUnit.MILLISECONDS, new Lin ...

  2. DG_Oracle DataGuard作用和概念(概念)

    2014-06-03 Created By BaoXinjian  

  3. PLSQL_低效SQL的识别和查询汇总(案例)

    2014-12-18 Created By BaoXinjian

  4. 控制WIFI状态

    1.控制WIFI public class MainActivity extends Activity { private Button startButton = null; private But ...

  5. windows service的作成

    http://jingyan.baidu.com/article/fa4125acb71a8628ac709226.html

  6. js中使用s(c)标签

    在js或者jquery中使用s标签,其实并不难理解,s标签也只是一个标签而已,当你想象成js+s标签=js+html标签就理解了 例如: <script type="text/java ...

  7. Ubuntu设置环境变量并立即生效

    Ubuntu Linux系统包含两类环境变量:系统环境变量和用户环境变量.系统环境变量对所有系统用户都有效,用户环境变量仅仅对当前的用户有效. 修改用户环境变量 用户环境变量通常被存储在下面的文件中: ...

  8. JVM加载class文件的原理机制

    Java中的所有类,都需要由类加载器装载到JVM中才能运行.类加载器本身也是一个类,而它的工作就是把class文件从硬盘读取到内存中.在写程序的时候,我们几乎不需要关心类的加载,因为这些都是隐式装载的 ...

  9. [Flex] ButtonBar系列——皮肤和外观设置

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  10. MyEclipse运行时自动保存

    今天第一次用MyEclipse,我发现我的代码明明修改了,但运行结果发现总是修改前的代码结果.后来发现,是代码修改后必须保存,再点运行.这个功能明显不合适,所以需要更改MyEclipse的配置.红框是 ...