给定一个整数数组 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. LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式 的解决方法

    一.案例1,及解决方案: "LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式." ...

  2. 算法(Algorithms)第4版 练习 1.3.27 1.3.28

    代码实现: //1.3.27 /** * return the value of the maximum key in the list * * @param list the linked list ...

  3. Apache CGI 配置

    在/etc/apache2/apache2.conf末尾添加 ServerName lacalhost:80 然后启动CGI模块: sudo a2enmod cgi 3.重启Apache: syste ...

  4. C++(一)— stringstream的用法

    输入输出的头文件 <iostream>  string流的头文件 <sstream>  文件流的头文件   <fstream> 1.利用输入输出做数据转换 stri ...

  5. 如何设置android studio让程序运行在真机中

    1.Run——>Edit Configurations... 2.运行

  6. BZOJ_1119_[POI2009]SLO_置换+贪心

    BZOJ_1119_[POI2009]SLO_置换+贪心 Description 对于一个1-N的排列(ai),每次你可以交换两个数ax与ay(x<>y),代价为W(ax)+W(ay) 若 ...

  7. javacpp-FFmpeg系列之1:视频拉流解码成YUVJ420P,并保存为jpg图片

    javacpp-ffmpeg系列: javacpp-FFmpeg系列之1:视频拉流解码成YUVJ420P,并保存为jpg图片 javacpp-FFmpeg系列之2:通用拉流解码器,支持视频拉流解码并转 ...

  8. 消息队列:快速上手ActiveMQ消息队列的JMS方式使用(两种模式:Topic和Queue的消息推送和订阅)

    1.实现功能 希望使用一套API,实现两种模式下的消息发送和接收功能,方便业务程序调用 1.发送Topic 2.发送Queue 3.接收Topic 4.接收Queue 2.接口设计 根据功能设计公共调 ...

  9. Jenkins安装和配置FindBugs、PMD、CheckStyle等插件

    最近研究Jenkins的常用插件的使用,主要使用FindBugs.PMD.CheckStyle.Violations.Emma等插件,主要参考了http://blog.csdn.net/dc_726/ ...

  10. 机器学习、图像识别方面 书籍推荐 via zhihu

    机器学习.图像识别方面 书籍推荐 作者:小涛 链接:https://www.zhihu.com/question/20523667/answer/97384340 来源:知乎 著作权归作者所有.商业转 ...