Leetcode001 two sum
/* 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的更多相关文章
- LeetCode-001 Two Sum
[题目] Given an array of integers, find two numbers such that they add up to a specific target number. ...
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
随机推荐
- SparkSql官方文档中文翻译(java版本)
1 概述(Overview) 2 DataFrames 2.1 入口:SQLContext(Starting Point: SQLContext) 2.2 创建DataFrames(Creating ...
- PLSQL_Oracle PLSQL内置函数大全(概念)
2014-06-20 Created By BaoXinjian
- 【转】Action 、 RenderAction 、 Partial 、 RenderPartial 区别
1.Action . RenderAction Action 方法以字符串的形式返回子视图,可以直接被呈现.RenderAction方法在适当的地方呈现子视图. //One alternative ( ...
- ylbtech-LanguageSamples-XMLdoc
ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-XMLdoc 1.A,示例(Sample) 返回顶部 “XML 文档”示例 本示例演示如 ...
- JAVA数据类型自动转换,与强制转换
一.数据类型自动转换 public class Test{ public static void main(String[] args){ int a = 1; double b = 1.5; dou ...
- Laravel 部署安装到虚拟主机的方法(折腾了一周,终于成功部署,原来是虚拟机不加载.env,谢谢莫回首http://lxl520.com/index.php/archives/88/!)
作者:莫回首链接:https://www.zhihu.com/question/35497879/answer/111241182来源:知乎著作权归作者所有,转载请联系作者获得授权. 序 lara ...
- MySQL利用Navicat导出数据字典
这里算是一个小技巧 利用mysql的information_schema中的COLUMNS表 和navicat中的导出功能实现快速导出数据字典 CREATE TEMPORARYTABLE `COLUM ...
- 关于 MySQL UTF8 编码下生僻字符插入失败/假死问题的分析
原文:http://my.oschina.net/leejun2005/blog/343353 目录[-] 1.问题:mysql 遇到某些中文插入异常 2.原因:此 utf8 非彼 utf8 3.解决 ...
- java泛型的使用
package com.wzh.test.generic; import java.util.ArrayList; import java.util.HashMap; import java.util ...
- 用block响应button的点击事件
1.继承UIButton : 2.在自己定义的button类中的方法 addTarget:(id)target action:(SEL)action forControlEvents:(UIContr ...