两数之和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 里面是属于 ...
随机推荐
- 使用python转换编码格式
之前有写过一个使用powershell转换文档格式的方法,然而因为powershell支持不是很全,所以并不好用.这里使用python再做一个. 思路 检测源码格式,如果不是utf8,则进行转换,否则 ...
- spring 路径配置通配符是如何实现的
在spring的配置文件中.经常看见类似这样的配置路径: classpath:/com/module/**/*sql.xml 系统会根据配置路径自动加载符合路径规则的xml文件. Spring还提供了 ...
- math worksheet作业纸生成器
https://www.education.com/worksheet-generator/math/ https://www.mathgoodies.com/worksheets/generator ...
- CSS3实现3D木块旋转动画
CSS3实现3D木块旋转动画,css3特效,旋转动画,3D,立体效果,CSS3实现3D木块旋转动画是一款迷人的HTML5+CSS3实现的3D旋转动画. 代码下载:http://www.huiyi8.c ...
- 微信video和audio无法自动播放解决方案
//音频,写法一<audio src="music/bg.mp3" autoplay loop controls>你的浏览器还不支持哦</audio> // ...
- PyNLPIR python中文分词工具
官网:https://pynlpir.readthedocs.io/en/latest/ github:https://github.com/tsroten/pynlpir NLP ...
- 如何通过giihub下载软件
因为不懂英文, 所以找到了网站也不知道要怎么下载? 需求: 假设要下载的的一个jar包, mybatis-generator 1. 利用搜索引擎 2. 点进去, 看到那个release (rel ...
- 基类的两个派生类再派生一个派生类 用virtual避免二义性
class vehicle{ int MaxSpeed; int Weight;public: vehicle(int maxspeed, int weight) :MaxSpeed(maxspeed ...
- linux命令学习笔记(54):ping命令
Linux系统的ping命令是常用的网络命令,它通常用来测试与目标主机的连通性,我们经常会说“ping一下某机器, 看是不是开着”.不能打开网页时会说“你先ping网关地址192.168.1.1试试” ...
- ubuntu 安装cuda 成功
洗洗睡了