[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 ...
随机推荐
- 第三方登录过程—OAuth2.0协议
---恢复内容开始--- 理清思路 1.在第三方注册成为开发者,拿到第三方给的client_id(app_id---就像你的身份证.QQ号)和client_secret(就像你的QQ密码): 2.填写 ...
- Linux学习笔记(2)-用户和用户组
用户(user)和用户组(group)概念 linux是一个多用户操作系统,他允许多个用户登录linux系统进行各自不同的操作.为了方便管理用户不同的权限,组的概念应用而生,一个组可以包含多个用户,共 ...
- [转]用Python做一个自动生成读表代码的小脚本
写在开始(本片文章不是写给小白的,至少你应该知道一些常识!) 大家在Unity开发中,肯定会把一些数据放到配置文件中,尤其是大一点的项目,每次开发一个新功能的时候,都要重复的写那些读表代码.非常烦.来 ...
- 1037: [ZJOI2008]生日聚会Party - BZOJ
Description 今天是hidadz小朋友的生日,她邀请了许多朋友来参加她的生日party. hidadz带着朋友们来到花园中,打算坐成一排玩游戏.为了游戏不至于无聊,就座的方案应满足如下条件: ...
- 3.7 spring-property 子元素的使用与解析
1.0 Property子元素的使用 property 子元素是再常用不过的了, 在看Spring源码之前,我们先看看它的使用方法, 1. 实例类如下: public class Animal { p ...
- 团体程序设计天梯赛-练习集L1-020. 帅到没朋友
L1-020. 帅到没朋友 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为 ...
- Spring MVC Checkbox And Checkboxes Example
In Spring MVC, <form:checkbox /> is used to render a HTML checkbox field, the checkbox values ...
- CURL与PHP-CLI的应用【CLI篇】
CLI的普通应用 什么是PHP-CLI php-cli是php Command Line Interface的简称,即PHP命令行接口,在windows和linux下都是支持PHP-CLI模式的; 为 ...
- netstat命令查看服务器运行情况
netstat -n|grep 80出现大量time_wait 在运行netstat -n|grep 80 | awk '/^tcp/ {++S[$NF]} END {for(a in S) prin ...
- python string 连接test
def strTest(): name = "" for i in range(10): name += "hello" #print name def str ...