leetcode 练习1  two sum

            whowhoha@outlook.com

问题描述

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,

return [0, 1].
解法1:暴力破解法: O(n^2) runtime, O(1) space – Brute force: The brute force approach is
simple. Loop through each element x and find if there is another value that
equals to target – x. As finding another value requires looping through

the rest of array, its runtime complexity
is O(n^2).

解法2:使用HashMap。把每个数都存入map中,然后再逐个遍历,查找是否有 target – nums[i]。  O(n) runtime  O(n) space,

vector<int> twoSum(vector<int>&
nums, int target){

vector<int>
vec;

map<int,int> m;

for (int i = 0; i < nums.size(); i++)

{

if(m.find(target
- nums[i]) != m.end())

{

vec.push_back(m[target -
nums[i]] );

vec.push_back(i);

break;

}

m[nums[i]] = i;

}

return
vec;

}

调用:

int a[6]={2,7,1,8,9};

vector<int>
vec(a,a+5);

vector <int>
vect= twoSum(vec,15);

return 1;

leetcode 练习1 two sum的更多相关文章

  1. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  2. [LeetCode] Minimum Size Subarray Sum 解题思路

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  3. 【一天一道LeetCode】#113. Path Sum II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【一天一道LeetCode】#40. Combination Sum II

    一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...

  5. 乘风破浪:LeetCode真题_040_Combination Sum II

    乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...

  6. 乘风破浪:LeetCode真题_039_Combination Sum

    乘风破浪:LeetCode真题_039_Combination Sum 一.前言     这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...

  7. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  8. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  9. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

随机推荐

  1. jquery之css()改变字体大小,颜色,背景色

    转: <script type="text/javascript"> $(document).ready(function() {         $("#f ...

  2. Javascript之计时

    // <!DOCTYPE html> <meta http-equiv="Content-Type" content="text/html; chars ...

  3. 强大的数据恢复软件--EasyRecovery专业版

    EasyRecovery 是世界著名数据恢复公司 Ontrack 的技术杰作,它是一个威力非常强大的硬盘数据恢复工具.能够帮你恢复丢失的数据以及重建文件系统.EasyRecovery不会向你的原始驱动 ...

  4. ArcMap 10.2 crashes during Loading Document launch stage

    问题描述: ArcMap unexpectedly exits during the "Loading Document..." stage on startup. No erro ...

  5. 在win下面使用cdt+cygwin+cmake

    在cygwin终端下面, cmake -G"Eclipse CDT4 - Unix Makefiles" -D CMAKE_BUILD_TYPE=Debug 当收获警告 Could ...

  6. JDBC(用Eclipse操作数据库Oracle)的基础操作集合

    JDBC: JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编 ...

  7. JAVA生成EXCEL图表

    跟据客户的要求,需要开发一套包括图形的报表,还需要导出WORD 图表需要这样: 这样: 这样: 还有这样: 接下来是实现思路: 以往用的最多的就是JFreechart,手上也有实现各种图形的资源,但是 ...

  8. OpenGL8-直接分配显存-极速绘制(Opengl1.5版本才有)

    视频教程请关注 http://edu.csdn.net/lecturer/lecturer_detail?lecturer_id=440 /** * 这个例子介绍如何使用显卡内存进行绘制 下载地址 : ...

  9. sqlserver 大文件脚本执行

    sqlserver2008中需要执行大文件的脚本,查询分析器中打不开,需要用到sql命令,开始使用osql命令,不过提示对应sqlserver2008中的驱动找不到(具体原因未分析-空闲时在处理),使 ...

  10. windbg远程调试

    1, A,调试机. B,被调试机. 2, 在B机上安装windbg,公共符号文件,程序的PDB都要复制过来. 公共符号文件位置设置在于A机相同的位置. windbg–server tcp:port=5 ...