[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 ...
随机推荐
- 【数学/扩展欧几里得/线性求逆元】[Sdoi2008]沙拉公主的困惑
Description 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行编号与M!互质的钞票.房地产第一大户沙拉公主决定预测一下大富翁国现 ...
- python读写配置文件
#coding:utf-8 import ConfigParser class Conf(): def __init__(self,name): self.name = name self.cp = ...
- php的redis 操作类,适用于单台或多台、多组redis服务器操作
redis 操作类,包括单台或多台.多组redis服务器操作,适用于业务复杂.高性能要求的 php web 应用. redis.php: <?php /* redis 操作类,适用于单台或多台. ...
- ssh远程执行命令并自动退出(已测试通过)
转自:http://blog.csdn.net/fdipzone/article/details/23000201 ssh命令格式如下: usage: ssh [-1246AaCfgKkMNnqsTt ...
- jquery layout学习
1.官网:http://layout.jquery-dev.com/index.cfm 2.博客园:http://www.cnblogs.com/chen-fan/articles/2044556.h ...
- Qt:QT右键菜单
Qt QTableView 上加右键弹出菜单, 并复制选中的单元格内容到剪贴板中 http://wenku.baidu.com/view/c51cfb63cf84b9d528ea7a29.html h ...
- C++拷贝对象
简介 对象的创建中,常常有这样的需求,就是把对象复制一份. 而复制有三种方法: 1.通过初始化来复制 例如:Object o1(10); Object o2=o1; 2.通过赋值来复制 例如:Obje ...
- QPushButton 的checkable 属性
只有setCheckable(true),这个button才能发射 toggle(bool) 信号. 而toggle(bool)代表了button 按下,弹起的状态像0,1的切换开关.
- Android安全问题 程序锁
导读:本文介绍如何实现对应用加锁的功能,无须root权限 某些人有时候会有这样一种需求,小A下载了个软件,只是软件中的美女过于诱惑与暴露,所以他不想让别人知道这是个什么软件,起码不想让别人打开浏 览. ...
- P143、面试题25:二叉树中和为某一值的路径
题目:输入一棵二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.二叉树结点的定义如下:struct BinaryTreeNode ...