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

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

示例:

给定 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. 分享java常用技术教程:dubbo、zookeeper、ActiveMq、多线程、Redis、PowerDesigner等

    游戏是自己整理的邮箱来源于网上,下面是我的有道云的分享地址: https://note.youdao.com/share/?id=c5f258fa9efd1b28b2d8f0d37e59b194&am ...

  2. CSS优先级和定位

    overflow属性 hidden scroll auto hidden 超出隐藏 scroll 滚动条 Auto 自动 display属性 block inline inline-block non ...

  3. Hadoop MR编程

    Hadoop开发job需要定一个Map/Reduce/Job(启动MR job,并传入参数信息),以下代码示例实现的功能: 1)将一个用逗号分割的文件,替换为“|”分割的文件: 2)对小文件合并,将文 ...

  4. SpringMVC(六):@RequestMapping下使用@RequestHeader绑定请求报头的属性值、@CookieValue绑定请求中的Cookie值

    备注:我本地浏览器的报头(Request Header)信息如下: Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image ...

  5. scrapy批量下载图片

    # -*- coding: utf-8 -*- import scrapy from rihan.items import RihanItem class RihanspiderSpider(scra ...

  6. hasattr(obj,attr) 判断前面是否有后面的属性

    hasattr(obj,attr) 判断前面是否有后面的属性

  7. js中获取元素的样式兼容性的写法

    1:设计元素的样式:el.style.color="red"||el.style["color"]="red"  获取元素的样式:el.st ...

  8. Delphi X10.2 + FireDAC 使用 SQL 语句 UPDATE

    MainForm.Conn.StartTransaction; UserManagerQuery.SQL.Clear; UserManagerQuery.SQL.Text := 'UPDATE tab ...

  9. 前端之旅HTML与CSS篇之IE6常见BUG

    1.IE6怪异解析之padding与border算入宽高原因:未加文档声明造成非盒模型解析解决方法:加入文档声明<!doctype html> 2.IE6在块元素.左右浮动.设定marin ...

  10. thinphp验证码的简单实现

    index.html <!DOCTYPE html><html lang="en"><head> <meta charset=" ...