给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。

你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。

示例:

给定 nums = [, , , ], target = 

因为 nums[] + nums[] =  +  =
所以返回 [, ]

AC代码    执行用时:196 ms

 class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> result;
for(int i = ; i < nums.size() ; i++) {
for(int j = i + ; j < nums.size(); j++) {
if(nums[i] + nums[j] == target) {
result.push_back(i);
result.push_back(j);
return result;
}
}
} }
};

范例AC代码  执行用时:4 ms

 class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> arr(nums);
sort(arr.begin(), arr.end());
int i = , j = arr.size() - ;
while (i < j)
{
if (arr[i] + arr[j] < target)
i++;
else if (arr[i] + arr[j] > target)
j--;
else
break;
}
vector<int> ret;
int a = distance(nums.begin(), find(nums.begin(), nums.end(), arr[i]));
reverse(nums.begin(),nums.end());
int b = nums.size() - - distance(nums.begin(), find(nums.begin(), nums.end(), arr[j]));
ret.push_back(a);
ret.push_back(b);
return ret;
}
};

继续加油~

1. 两数之和 LeetCode的更多相关文章

  1. [Java]1.两数之和 - LeetCode

    1 题目 2 思路与代码 思路一:暴力法(两层For循环) 时间复杂度:O(n^2) 对于每个元素,我们试图通过遍历数组的其余部分来寻找它所对应的目标元素,这将耗费 O(n) 的时间.因此时间复杂度为 ...

  2. 两数之和 [ leetcode ]

    原题地址:https://leetcode-cn.com/articles/two-sum/ 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元 ...

  3. 两数之和LeetCode

    给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元 ...

  4. LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  5. LeetCode 371. Sum of Two Integers (两数之和)

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  6. LeetCode 167. Two Sum II - Input array is sorted (两数之和之二 - 输入的是有序数组)

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  7. [LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

  8. [LeetCode] 1. Two Sum 两数之和

    Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...

  9. 两数之和,两数相加(leetcode)

    我们都知道算法是程序员成长重要的一环,怎么才能提高算法呢, 出来在网上看视频之外,动手练习是非常重要的.leetcode 就是一个非常好的锻炼平台. 1. 两数之和,在 leetcode 里面是属于 ...

随机推荐

  1. EFCore CodeFirst 连接MySql

    一.工具及环境 Visual Studio 2017 15.4.3 MySql Navicat for MySQL 二.Entity Framwork Core 2.0 MySql Code Firs ...

  2. 关于if后面直接加上参数名,不加条件的用法

    <template> <section> <p v-if="aa">{{aa}}</p> <p v-if="bb&q ...

  3. CentOS下安装yum

    在Linux里面依次输入下面的命令: 1,下载最新的yum-3.2.28.tar.gz并解压 wget http://yum.baseurl.org/download/3.2/yum-3.2.28.t ...

  4. 实战分享:如何成功防护1.2T国内已知最大流量DDoS攻击

    作者:腾讯云宙斯盾安全团队&腾讯安全平台部 引言: DDoS攻击势头愈演愈烈,除了攻击手法的多样化发展之外,最直接的还是攻击流量的成倍增长.3月份国内的最大规模DDoS攻击纪录还停留在数百G规 ...

  5. Python学习【第26篇】:Python系列- 多线程(threading)

    线程的调用方式:threanding模块 import threading import time def sayhi(num): #定义每个线程要运行的函数 print("running ...

  6. 爱奇艺2018春招Java工程师编程题题解

    字典序最大子序列 题目描述 对于字符串a和b,如果移除字符串a中的一些字母(可以全部移除,也可以一个都不移除)就能够得到字符串b我们就称b是a的子序列. 例如."heo"是&quo ...

  7. ML笔记:Classification: Logistic Regression

  8. [HNOI 2013]游走

    Description 题库链接 一个无向连通图,顶点从 \(1\) 编号到 \(N\) ,边从 \(1\) 编号到 \(M\) . 小Z在该图上进行随机游走,初始时小Z在 \(1\) 号顶点,每一步 ...

  9. ●BZOJ 1076 [SCOI2008]奖励关

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1076题解: 期望dp. (模糊的题意,2333) 题中的:"现在决定不吃的宝物以后 ...

  10. 洛谷P2388 阶乘之乘

    题目背景 不告诉你-- 题目描述 求出1!*2!*3!*4!*--*n!的末尾有几个零 输入输出格式 输入格式: n(n<=10^8) 输出格式: 有几个零 输入输出样例 输入样例#1: 复制 ...