[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 ...
随机推荐
- c#无标题窗体点击任务栏图标正常最小化或还原
FormBorderStyle等于System.Windows.Forms.FormBorderStyle.None的窗体,点击任务栏图标的时候,是不能象标准窗体那样最小化或还原的. protecte ...
- Oracle创建触发器实现主键自增
CREATE OR REPLACE TRIGGER "trigger_empl" before insert on extjsTest1.t_empl for each row b ...
- spoj 394
每段可以连续的串的可能性是个Fibonacci数列 但是直接dp更好吧~~ #include <cstdio> #include <cstring> using names ...
- C++转换unicode utf-8 gb2312编码
windows开发环境下用VC++6.0 对unicode .utf-8. gb2312 三种编码格式之间的转换方法: #include <iostream> #include <s ...
- C++ 嵌套类使用(二)
C++嵌套类 1. 嵌套类的名字只在外围类可见. 2. 类的私有成员只有类的成员和友元可以访问,因此外围类不可以访问嵌套类的私有成员.嵌套类可以访问外围类的成员(通过对象.指针或者引用). 3 ...
- 在ECLIPSE中用MAVEN和TOMCAT来建立WEBAPP
找了很多示例,结合以下两个URL,比较简单的测试了一下. http://blog.csdn.net/clj198606061111/article/details/20221133 http://ww ...
- 一个比较全面的DJANGO_REST_FRAMEWORK的CASE
验证啊,过滤啊,hypermedia as the engine of ap‐plication state (HATEOAS)啊都有的. urls.py __author__ = 'sahara' ...
- 简单Sql语句统计每年每个月的数据,每个月为数据的每列,简单SQL练习
有一张表,数据如下 请写出结果为以下的SQL语句. 在mysql中创建表 CREATE TABLE `aa` ( `id` int(10) NOT NULL AUTO_INCREMENT COMME ...
- Netcat for Windows
April 10, 2009 Netcat is a simple networking utility which reads and writes data across network conn ...
- PHP 的面向方面编程
面向方面编程(AOP)对于PHP来说是一个新的概念.现在PHP对于 AOP 并没有官方支持,但有很多扩展和库实现了这个特性.本课中,我们将使用 Go! PHP library 来学习 PHP 如何进行 ...