leetcode 练习1 two sum
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的更多相关文章
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- [LeetCode] Minimum Size Subarray Sum 解题思路
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 【一天一道LeetCode】#113. Path Sum II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【一天一道LeetCode】#40. Combination Sum II
一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...
- 乘风破浪:LeetCode真题_040_Combination Sum II
乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...
- 乘风破浪:LeetCode真题_039_Combination Sum
乘风破浪:LeetCode真题_039_Combination Sum 一.前言 这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 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 ...
随机推荐
- Java之绘制艺术图案
前面已经介绍过绘制方法.这里不再赘述. package com.caiduping; import java.awt.Color; import java.awt.Graphics; import j ...
- SQL Server查看表信息
1. sp_spaceused 计算数据和索引使用的磁盘空间量以及当前数据库中的表所使用的磁盘空间量.如果没有给定 objname,sp_spaceused 则报告整个当前数据库所使用的空间. 语法 ...
- DOS批处理命令-注释
注释是每个程序中不可或缺的(不是对计算机来说,而是对我们这些程序员阅读代码来说) 语法: ①rem 这是批处理的注释命令,rem后面的内容全部是注释 例:rem 这是一行注释 ②:: 批处理遇到以冒号 ...
- [转]WCF 4 安全性和 WIF 简介
转自:http://www.cnblogs.com/WizardWu/archive/2010/10/04/1841793.html 本帖简介 .NET 新一代的 Windows Identity ...
- 第二十九篇、UICollectionView瀑布流
1.实现思路 >第一种方案:UIScrollView 镶嵌三个UITableView (不推荐使用) >第二种方案:UIScrollView 镶嵌UIImageView (需要解决循环利用 ...
- Quartz2D 图像处理
首先感谢一片枫叶总结出这么好的文章,文章出处:http://www.cnblogs.com/smileEvday/archive/2013/05/25/IOSImageEdit.html 本文将为大家 ...
- asp.net 文件操作小例子(创建文件夹,读,写,删)
静态生成要在虚拟目录下创建文件夹 来保存生成的页面 那么就要对文件进行操作 一.创建文件夹 using System.IO; string name = "aa"; strin ...
- Sublime Text 3初体验之Package Control
http://www.imooc.com/article/12616 下面介绍几款Sublime Text 常用Package 1.Emmit 2.JavaScript & NodeJS Sn ...
- Javascript中的函数
Javascript中的函数 1.什么是函数 函数是被命名的,独立的,完成特定功能的代码段.其可能给调用它的程序返回值,我们把这个代码段就称之为"函数". 被命名的:函数大部分都是 ...
- SpringInAction读书笔记--第1章Spring之旅
1.简化Java开发 Spring是一个开源框架,它的根本使命在于简化java开发.为了降低java开发的复杂性,Spring采取了以下4种关键策略: 基于POJO的轻量级和最小侵入性编程 ...