/* 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. Service代码示例

    package com.homily.training.service; import android.app.Service; import android.content.Intent; impo ...

  2. Spark1.4启动spark-shell时initializing失败

    错误信息如下: 5/11/03 16:48:15 INFO spark.SparkContext: Running Spark version 1.4.1 15/11/03 16:48:15 WARN ...

  3. UVA1347 旅游(二维递归DP)

    旅游 [题目链接]旅游 [题目类型]DP &题解: 紫书P269 代码很简单,但思路很难.很难能想到要把一个圈分成2条线段,很难想到d(i,j)表示的是已经走过max(i,j)还需要的距离值, ...

  4. unity jiaoben

    transform.Translate(Input.GetAxis("Horizontal")*Time.deltaTime,0,0); 移动 transform.Translat ...

  5. Jmeter+jenkins接口性能测试平台实践整理(二)

    本篇为Jmeter+jenkins+Ant方式执行性能测试 1.设置JDK,ANT环境变量: 2.build.xml文件: <?xml version="1.0" encod ...

  6. 各式 Web 前端開發工具整理

    程式碼編寫工具 (Coding Tools) 工作流程/建置/組合 (Workflow/Builds/Assemblers) lumbar brunch grunt lineman yeoman Ta ...

  7. java 抽象类和接口总结

    1.抽象类和抽象方法必须使用abstract 关键字来修饰 2.抽象类不能实例化 3.抽象方法是为实现的方法,它与空方法时两个完全不同的概念 4.abstract 不能喝private static ...

  8. 在git上下载的Asp.Net MVC 4源码怎么编译?

    以本人的下载位置为例:E:\aspnetwebstack 1.win+r 输入cmd 打开dos 界面 2.e: 回车,定位到e 盘 3.cd E:\aspnetwebstack 进入e 盘aspne ...

  9. .NET调用Java写的WebServices(可能会碰到的问题)

    1)net中定义的的WebService(返回值和参数都是自定义对象)可以被Java识别并调用,可是在Java中定义的WebService(返回值和参数都是自定义对象),C#客户端可以识别到自定义对象 ...

  10. HTTP 500 的解决方案

    我们首先要确定IIS权限的问题,如下所示: 身份验证的具体项: 身份验证的基本设置: 注意:如果不需要Form验证的情况下,那么把Form身份验证禁止掉,否则会出现在每一次请求的过程中都要有一个默认的 ...