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

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

示例:

给定 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. The python debugger(PDB)的简介

    转自:http://www.cnblogs.com/wei-li/archive/2012/05/02/2479082.html 学习Python调试,最好的资料当然是官方文档和(pdb)help了, ...

  2. CMDB开发

    浅谈ITIL TIL即IT基础架构库(Information Technology Infrastructure Library, ITIL,信息技术基础架构库)由英国政府部门CCTA(Central ...

  3. @OnetoOne @OnetoMany @ManyToOne(2)

    在班主任(id,name,bjid) 班级(id name) 学生(id name bjid)的 关系中 班主任一对一关联班级 班级一对多关联学生 @OnetoOne @joinColumn(bjid ...

  4. WPF 自定义TreeView控件样式,仿QQ联系人列表

    一.前言 TreeView控件在项目中使用比较频繁,普通的TreeView并不能满足我们的需求.因此我们需要滴对TreeView进行改造.下面的内容将介绍仿QQ联系人TreeView样式及TreeVi ...

  5. Python 学习开篇

    前言 最近看到一张图,有点意思: 蓝色是(成长+付出),红色是回报.有多久可以达到这个红心,要看我们自已的努力,付出了多少专注与汗水.我想说的是,水平轴并不是时间,如果不能坚持努力,可能永远都到不了那 ...

  6. angular的时间指令 以及防止闪烁问题

    1.点击事件 <!doctype html><html lang="en"><head> <meta charset="UTF- ...

  7. 关于redis主从|哨兵|集群模式

    关于redis主从.哨兵.集群的介绍网上很多,这里就不赘述了. 一.主从 通过持久化功能,Redis保证了即使在服务器重启的情况下也不会损失(或少量损失)数据,因为持久化会把内存中数据保存到硬盘上,重 ...

  8. for-each的坑(Hollis)

    直接用代码来说明: public class ForEach { public static void main(String[] args) { List<String> list = ...

  9. Java IO(四)

    在文件操作流中,输入输出的目标都是文件,但是有时候,我们并不需要写入文件,只是需要中转一下而已,这样就会显得很麻烦,所以我们就可以使用内存操作流.在内存操作流中,输入输出目标都是内存. 内存输出流:B ...

  10. [SHOI2014]概率充电器

    Description 著名的电子产品品牌 SHOI 刚刚发布了引领世界潮流的下一代电子产品——概率充电器: “采用全新纳米级加工技术,实现元件与导线能否通电完全由真随机数决定!SHOI 概率充电器, ...