Description:

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, and you may not use the same element twice.

Example:

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

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

return [0, 1].

Attention:

同一个元素不可重复使用,如 6 = 3 + 3:两个3必须是下标不同的元素。

Answer:

Review:

1、暴力遍历 时间复杂度O(n2),空间复杂度O(1)

2、哈希查找 时间复杂度O(n),空间复杂度O(n),空间换取时间

3、C++数组长度:参考 https://www.cnblogs.com/liutongqing/p/7282528.html

4、STL   vector<> 参考 https://www.cnblogs.com/aminxu/p/4686332.html

5、STL map<type,type> 参考 https://www.cnblogs.com/empty16/p/6395813.html

6、迭代器使用 参考 https://www.cnblogs.com/maluning/p/8570717.html

1-TwoSum(简单)的更多相关文章

  1. JavaScript的two-sum问题解法

    一个很常见的问题,找出一个数组中和为给定值的两个数的下标.为了简单一般会注明解只有一个之类的. 最容易想到的方法是循环遍历,这里就不说了. 在JS中比较优雅的方式是利用JS的对象作为hash的方式: ...

  2. twoSum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  3. [LeetCode] TwoSum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  4. leetcode-【简单题】Two Sum

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  5. LeetCode初体验—twoSum

    今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...

  6. LeetCode #1 TwoSum

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

  7. Leetcode 1——twosum

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

  8. leetcode — two-sum

    package org.lep.leetcode.twosum; import java.util.Arrays; import java.util.HashMap; import java.util ...

  9. leetcode刷题--两数之和(简单)

    一.序言 第一次刷leetcode的题,之前从来没有刷题然后去面试的概念,直到临近秋招,或许是秋招结束的时候才有这个意识,原来面试是需要刷题的,面试问的问题都是千篇一律的,只要刷够了题就差不多了,当然 ...

  10. TwoSum:两数相加得0

    在一个不重复的数组中,统计有多少组两个元素相加得0. 这里使用三种方式实现,并统计他们各自花费的时间: import java.util.Arrays; import java.util.HashMa ...

随机推荐

  1. rpm包与 yum 安装与卸载

    rpm包的安装:      1.安装一个包 # rpm -ivh 2.升级一个包 # rpm -Uvh 3.移走一个包 # rpm -e 4.安装参数 --force 即使覆盖属于其它包的文件也强迫安 ...

  2. Linux环境部署SVN服务器

    1. 安装SVN服务器: 检查是否已安装 # rpm -qa subversion 安装SVN服务器 # yum install httpd httpd-devel subversion mod_da ...

  3. RC4加密解密

    package util; public class RC4Util { public static String decry_RC4(byte[] data, String key) { if (d ...

  4. P2598 [ZJOI2009]狼和羊的故事(网络流)

    P2598 [ZJOI2009]狼和羊的故事 源点和所有狼连 $inf$ 的边 所有羊和汇点连 $inf$ 的边 所有点向四周连 $1$ 的边 这样所有狼和羊之间的边都被割掉了 统计最小割就好辣 #i ...

  5. id、class等各种选择器总结

    1.  id              选择器       #     class        选择器        .     标签         选择器       标签名     群组   ...

  6. shell实现rpm -e 一键卸载所有相关包以及依赖

    原理也比较简单, 刚好用到就稍微写了一下, 做个笔记 #!/bin/bash #************************************************************ ...

  7. Mac提醒休息软件Stretchly(很好用)

    github地址: https://github.com/hovancik/stretchly 安装就不介绍了,他的自定义时间目前还是有点麻烦,介绍一下. 配置文件是 ~/Library/Applic ...

  8. Celery完成定时任务

    1.什么是Celery Celery是一个简单.灵活且可靠的,处理大量消息的分布式系统 专注于实时处理的异步任务队列 同时也支持任务调度 celery支持linux,如果windows使用celery ...

  9. asp.net 根据连接地址保存文件,图片

    第一种方式  下载图片,文件 WebClient my = new WebClient(); string url = "http://localhost:1015/resource/loa ...

  10. pycharm 序列号/行号 的宽度太宽了如何调整

    问题:行号宽度嫌宽: 解决:在settings里面,如下图顺序,取消“show gutter icons”的勾,即可. 调整后如下图: