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 ...
随机推荐
- JQuery 常用方法基础教程
本文转自(http://www.cnblogs.com/Leo_wl/archive/2010/06/22/1762401.html) 对于学习使用jquery 的朋友,能用的到,简单的了解下jque ...
- linux下sublime配置c++11编译环境
Tools->Build System->New Build System { "cmd": ["g++", "-std=c++11&qu ...
- adb 工具学习
adb (android debug bridge)简单介绍: 1.adb 是 Android SDK中所带工具.使用adb,可以在PC上操作Android设备或者模拟器 2.主要功能有: 将本地ap ...
- 十一、jdk命令之Jstatd命令(Java Statistics Monitoring Daemon)
目录 一.jdk工具之jps(JVM Process Status Tools)命令使用 二.jdk命令之javah命令(C Header and Stub File Generator) 三.jdk ...
- Struts2 Annotation 注解配置
也叫Zero Configuration(零配置),它省去了写xml文件的麻烦,可以直接在类叫进行配置,不用在java文件和xml文件中来回切换. 必须导入struts2-convention-plu ...
- Tomcat启动过程原理详解
基于Java的Web 应用程序是 servlet.JSP 页面.静态页面.类和其他资源的集合,它们可以用标准方式打包,并运行在来自多个供应商的多个容器.Web 应用程序存在于结构化层次结构的目录中,该 ...
- 设置presentVC跟PushVC一样的效果即从右到左的动画
SettingViewController *VC = [[SettingViewControlleralloc]init]; VC.view.backgroundColor = [UIColorwh ...
- 破解ckfinder2.3 去除版本号和标题提示
1.找到ckfinder.js 2.找到这样的字符串 <h4 class='message_content'></h4> 3.改成这样 <h4 style='displa ...
- .NET 集合操作性能
如果单元格的内容是na(not applicatable),就表示这个操作不能应用于这种集合类型.
- HDU 5808[数位dp]
/* 题意: 给你l和r,范围9e18,求l到r闭区间有多少个数字满足,连续的奇数的个数都为偶数,连续的偶数的个数都为奇数. 例如33433符合要求,44不符合要求.不能含有前导零. 思路: 队友说是 ...