两数之和LeetCode
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的更多相关文章
- [Java]1.两数之和 - LeetCode
1 题目 2 思路与代码 思路一:暴力法(两层For循环) 时间复杂度:O(n^2) 对于每个元素,我们试图通过遍历数组的其余部分来寻找它所对应的目标元素,这将耗费 O(n) 的时间.因此时间复杂度为 ...
- 1. 两数之和 LeetCode
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [, , , ], target = 因为 n ...
- 两数之和 [ leetcode ]
原题地址:https://leetcode-cn.com/articles/two-sum/ 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元 ...
- LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- 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 ...
- 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 ...
- [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 ...
- [LeetCode] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- 两数之和,两数相加(leetcode)
我们都知道算法是程序员成长重要的一环,怎么才能提高算法呢, 出来在网上看视频之外,动手练习是非常重要的.leetcode 就是一个非常好的锻炼平台. 1. 两数之和,在 leetcode 里面是属于 ...
随机推荐
- IDA调试android so的.init_array数组
参考: http://www.itdadao.com/articles/c15a190757p0.html 一. 为什么要调试init_array init_array的用途 1. 一些全局变量的初始 ...
- Mac OS访问Windows共享文件夹
原文地址:http://blog.csdn.net/jinhill/article/details/7246922 最近开始研究Mac OS,遇到的第一个问题就是如何在Mac OS中访问Windows ...
- 检测UTF-8编码
在PHP检测字符串是否是UTF-8编码的时候,很多人在使用mb_detect_encoding的时候,经常遇到检测不准的问题,下面的方法可以准确检测编码是否是UTF-8 function check_ ...
- IDEAL葵花宝典:java代码开发规范插件 Rainbow Brackets 插件
前言: 最近在Jetbrains IDEA插件网站逛发现了 Rainbow Brackets这款插件,非常棒,推荐给大家. 可以实现配对括号相同颜色,并且实现选中区域代码高亮的功能. 对增强写代码的有 ...
- C语言中的指针(一)
指针也是一种数据类型,占用内存空间,内存中存储的只能是变量的地址. *p是操作内存的意思,在声明成为指针变量的时候使用*,在使用指针的时候,*表示操作内存. *p放在等号的左边,相当于是从内存中取值, ...
- Linux网络编程socket错误分析
socket错误码: EINTR: 阻塞的操作被取消阻塞的调用打断.如设置了发送接收超时,就会遇到这种错误. 只能针对阻塞模式的socket.读,写阻塞的socket时,-1返回,错误号为INTR.另 ...
- oracle Instant Client install
Installation See the Instant Client Home Page for more information. Installation of ZIP files: 1. Do ...
- Shell读取文件内容【转】
while read wOne wTwo wThreedo [ -z $wOne ] && continue #测试此行内容是否为空 xxx=$wOne ...
- python之系统编程 --进程
1.调试(PDB) 代码: [root@master gaoji]# vim test2.py 1 #!/usr/local/bin/python3 2 # -*- coding:utf-8 -*- ...
- 51Nod 1362 搬箱子 —— 组合数(非质数取模) (差分TLE)
题目:http://www.51nod.com/Challenge/Problem.html#!#problemId=1362 首先,\( f[i][j] \) 是一个 \( i \) 次多项式: 如 ...