给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

" class="notranslate">

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
/**
* 无脑暴力:
* 时间复杂度 : n^2
* 空间复杂度 : 1
* 70ms
* 38.5MB
*/
public static int[] traverseTwoSum(int[] nums, int target){
int len = nums.length;
for (int i = 0; i < len; i++) {
for (int j = i+1; j < len; j++) {
if(nums[i]+nums[j]==target){
return new int[]{i,j};
}
}
}
return null;
}
/**
* 因为哈希表的查找速度非常的快,我们可以采用hash表去解决问题.
* 时间: n
* 空间: n
* 4ms
* 36.2MB
*
*/
public static int[] secondHashTwoSum(int[] nums, int target){
Map<Integer, Integer> map = new HashMap<>();
int len = nums.length;
for (int i = 0; i < len; i++) {
// 根据键去找 值
map.put(nums[i],i);
}
int findNum;
for (int i = 0; i < len; i++) {
findNum = target - nums[i];
if(map.containsKey(findNum)&&map.get(findNum)!=i){
return new int[]{i,map.get(findNum)};
}
}
return null;
}
/**
* 改进为一次hash
* 时间: n
* 空间: n
* 4ms
* 36.2MB
*
*/
public static int[] firstHashTwoSum(int[] nums, int target){
Map<Integer, Integer> map = new HashMap<>();
int len = nums.length,findNum;
for (int i = 0; i < len; i++) {
findNum = target-nums[i];
if(map.containsKey(findNum)){
return new int[] {map.get(findNum),i};
}
map.put(nums[i],i);
}
return null;
}

两数之和LeetCode的更多相关文章

  1. [Java]1.两数之和 - LeetCode

    1 题目 2 思路与代码 思路一:暴力法(两层For循环) 时间复杂度:O(n^2) 对于每个元素,我们试图通过遍历数组的其余部分来寻找它所对应的目标元素,这将耗费 O(n) 的时间.因此时间复杂度为 ...

  2. 1. 两数之和 LeetCode

    给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [, , , ], target = 因为 n ...

  3. 两数之和 [ leetcode ]

    原题地址:https://leetcode-cn.com/articles/two-sum/ 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元 ...

  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. gcc error - "iostream: No such file or directory"

    #include <iostream> using namespace std; int main(void) { cout<<"Hello World!\n&quo ...

  2. git format-patch的使用【转】

    本文转载自:http://blog.chinaunix.net/uid-28621021-id-3487102.html git format-patch的使用   1.在dev1分支上,打出所有de ...

  3. 51nod 80分算法题

    1537:见前几篇. 1627:题意:给定n,m的网格(10^5),初始状态为(1,1),你每次可以瞬移到右下方(不可以同行同列逗留)任何一个方格里,求移动到n,m的方案数. 一句话题解:首先很容易想 ...

  4. Hadoop- 流量汇总程序之如何实现hadoop的序列化接口及代码实现

    流量汇总程序需求 统计每一个用户(手机号)锁耗费的总上行流量.下行流量.总流量. 流程剖析 阶段:map 读取一行数据,切分字段, 抽取手机号,上行流量,下行流量 context.write(手机号, ...

  5. mybatis传递多个参数值(转)

    Mybatis传递多个参数   ibatis3如何传递多个参数有两个方法:一种是使用Map,另一种是使用JavaBean. <!--      使用HashMap传递多个参数      para ...

  6. Quartz.Net初探

    想必大家在工作中经常会遇到这样类似的需求,在某个时间或者需要不间断的执行某个业务动作去满足任务需求.例如,我们写了一个job,定时去处理一些任务,在没有了解到Quartz.Net之前,我是这样做的,进 ...

  7. svn 操作手册

    1.执行下列指令安装    sudo apt-get install subversion   2.  创建版本库       sudo mkdir /home/svn    sudo mkdir / ...

  8. BZOJ3065:带插入区间K小值

    浅谈树状数组与主席树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://www.lydsy.com/JudgeOnline/prob ...

  9. TX-

    NVIDIA Jetson TX2刷机 TX1 Gsteramer 环境配置 TX1 ssh配置

  10. java.lang.ClassCastException:android.widget.Button cannot be cast to android.widget.ImageView

    今天遇到一个错误也不知道怎么回事,上网搜了一下: 出现的问题是:java.lang.ClassCastException:android.widget.Button cannot be cast to ...