[LeetCode] Two Sum水过
刷LeetCode的第一题,TwoSum,基本算是水过。
题目:https://leetcode.com/problems/two-sum/
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
分析:给出一个数组,和一个目标值target,请在数组中找出两个元素值的和等于该目标值的,并返回其数组索引(从1开始的索引)。题意相当简单。作为刷LC的第一道题,没有多想,这种与数组的某个特征值相关的问题,先排个序总是没错的。可以先从小到大sort一下,然后给扔两个指针到数组头尾,开始寻找满足要求的值。直接上代码。
typedef struct Node
{
int id,val;
}node;
bool cmp(const Node& n1,const Node& n2)
{
return n1.val < n2.val;
//定义的比较函数,按照节点值(也就是输入数组值)从小到大排列
}
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int len = nums.size();
Node nodes[len];
for(int i = ; i<len ; i++ )
{
nodes[i].id = i+;//不是从0开始,而是从1开始的索引。
nodes[i].val = nums[i];
}
sort(nodes,nodes+len,cmp);
int ptr1 = ,ptr2 = len-;//设置头尾指针
std::vector<int> ans;
while(ptr1<ptr2)
{
if(nodes[ptr1].val+nodes[ptr2].val == target)
{
if(nodes[ptr1].id>nodes[ptr2].id)
swap(nodes[ptr1].id,nodes[ptr2].id);
ans.push_back(nodes[ptr1].id);
ans.push_back(nodes[ptr2].id);//将答案放进ans
return ans;
}
else if(nodes[ptr1].val+nodes[ptr2].val < target)
ptr1++;//向后移动头指针
else
ptr2--;//向前移动尾指针
}
}
};
runtime还可以,比98%的CPP提交代码要快。
这个
Your runtime beats 98.59% of cpp submissions.
[LeetCode] Two Sum水过的更多相关文章
- LeetCode:Path Sum I II
LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...
- LeetCode:柠檬水找零【860】
LeetCode:柠檬水找零[860] 题目描述 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向 ...
- 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)
剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...
- [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] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
- [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
随机推荐
- eclipse 书签
虽然eclipse有back to和forward两个功能帮助我们阅读代码,但有时候代码一层一层看下去后,会忘了自己最初的起点. 因此想到了eclipse的书签bookmark功能. 首先,添加书签. ...
- .NET 反射详解(转)
概述反射 通过反射可以提供类型信息,从而使得我们开发人员在运行时能够利用这些信息构造和使用对象. 反射机制允许程序在执行过程中动态地添加各种功能. 运行时类型标识 运行时类型标识(RTTI),可以在 ...
- flex toolTip样式设置
需要3个文件.一个是样式类,一个样式文件,一个是mxml文件. ●MyToolTip.as package{ import mx.core.UITextField; import mx.ski ...
- 深入理解SQL注入绕过WAF与过滤机制
知己知彼,百战不殆 --孙子兵法 [目录] 0x0 前言 0x1 WAF的常见特征 0x2 绕过WAF的方法 0x3 SQLi Filter的实现及Evasion 0x4 延伸及测试向量示例 0x5 ...
- oracle SQLserver 函数
1.绝对值 S:select abs(-1) value O:select abs(-1) value from dual 2.取整(大) S:select ceiling(-1.001) value ...
- Creating a new Signiant Transfer Engine because the previous transfer had to be canceled.
From: http://stackoverflow.com/questions/10548196/application-loader-new-weird-warning-about-signian ...
- C++: 单例模式和缺陷
C++: 单例模式和缺陷 实现一个单例模式 1 class Singleton { 2 private: 3 Singleton() { cout << " ...
- Android:使用ViewPager实现左右滑动切换图片(图上有点点)
在以下实例的基础上加上点点 Android:使用ViewPager实现左右滑动切换图片 (简单版) 效果预览: 因为要把点点放图片上,所以修改布局为相对布局: <?xml version=&qu ...
- 转:JavaScript中函数与对象的关系
来自:http://www.nowamagic.net/javascript/js_RelationOfFunctionAndObject.php 在ajax兴起以前,很多人写JavaScript可以 ...
- 安装Hadoop系列 — 新建MapReduce项目
1.新建MR工程 依次点击 File → New → Ohter… 选择 “Map/Reduce Project”,然后输入项目名称:mrdemo,创建新项目: 2.(这步在以后的开发中可能 ...