/* 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. Report_客制化以PLSQL输出HTML标记实现WEB报表(案例)

    2014-05-31 Created By BaoXinjian

  2. c++学习-特殊类成员

    静态变量: #include<iostream> #include<string> #include <typeinfo> using namespace std; ...

  3. java finally中含return语句

    <java核心技术卷一>中提到过:当finally子句包含return 语句时(当然在设计原则上是不允许在finally块中抛出异常或者 执行return语句的,我不明白为何java的设计 ...

  4. sql中实现split()功能

    http://www.cnblogs.com/yangyy753/archive/2011/11/23/2260618.html 数据库中,总是遇到一些字段内容,想根据某个标识截取一下字符串,可是都想 ...

  5. ylbtech-LanguageSamples-Struct(结构)

    ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Struct(结构) 1.A,示例(Sample) 返回顶部 “结构”示例 本示例演示结 ...

  6. Session 与cookies 的区别

    两个都可以用来存私密的东西,同样也都有有效期的说法. 区别在于:session是放在服务器上的,过期与否取决于服务期的设定,cookie是存在客户端的,过去与否可以在cookie生成的时候设置进去. ...

  7. Mac 终端常用命令备忘

    Tab 补全 pwd 显示路径 一 .ls ls -lh   查看当前路径详细文件 ls ..     返回上级目录 ls -a   显示隐藏文件 ls -a -l 以详细列表显示 ls ../../ ...

  8. 容易导致outofmemoryException内存泄漏异常的编码问题

    1.System.Drawing方面的类使用问题 System.Drawing用到了很多系统的资源和非托管代码,所以使用的时候要特别小心,注意内存泄漏(Memory Leak) 2.new byte[ ...

  9. 关于在页面总嵌入iframe,ifram中发起请求,服务器端的session为空问题解决

    本文抄袭:http://blog.csdn.net/ray_adon/article/details/6960724 在做项目是 是用了iframe,iframe发起ajax请求,服务器端报sessi ...

  10. Django下载中文名文件:

    Django下载中文名文件: from django.utils.http import urlquote from django.http import HttpResponse content = ...