Array - Two Sum
import java.util.HashMap;
import java.util.Map;
/**
* 分析:
* 普通实现-嵌套循环两次,时间O(n2),空间O(1)
* 复杂实现-循环一次,时间O(n),空间O(n)
* 注意点:
* 使用HashMap的containsKey函数和get函数
* @param nums [2,7,11,15]
* @param target 9
* @return [0,1]
*/
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++) {
map.put(nums[i], i);
}
for(int i = 0; i < nums.length; i++) {
if(map.containsKey(target-nums[i]) && i != map.get(target-nums[i])) {
return new int[]{i, map.get(target-nums[i])};
}
}
return null;
}
Array - Two Sum的更多相关文章
- Leetcode: Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [Swift]LeetCode410. 分割数组的最大值 | Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- 动态规划——Split Array Largest Sum
题意大概就是,给定一个包含非负整数的序列nums以及一个整数m,要求把序列nums分成m份,并且要让这m个子序列各自的和的最大值最小(minimize the largest sum among th ...
- Split Array Largest Sum LT410
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- 410. Split Array Largest Sum 把数组划分为m组,怎样使最大和最小
[抄题]: Given an array which consists of non-negative integers and an integer m, you can split the arr ...
- [LeetCode] 410. Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- 【leetcode】410. Split Array Largest Sum
题目如下: Given an array which consists of non-negative integers and an integer m, you can split the arr ...
- 410. Split Array Largest Sum
做了Zenefits的OA,比面经里的简单多了..害我担心好久 阴险的Baidu啊,完全没想到用二分,一开始感觉要用DP,类似于极小极大值的做法. 然后看了答案也写了他妈好久. 思路是再不看M的情况下 ...
随机推荐
- Xilinx SDSoc 加载opencv库
Xilinx SDSoc 加载opencv库需要下载两个文件 xfopencv 和 Revision Platform, Revision Platform需要和具体的开发板型号对应,我用的是zcu1 ...
- JavaScript巩固篇(一)——数据类型与全局变量、局部变量、垃圾回收机制、存储方式、生命周期
知识要点 数据类型 存储方式 全局变量与局部变量 变量的生命周期 垃圾回收机制 知识概览 数据类型 JavaScript的数据类型分为:基本类型.引用类型 本质区别: 基本数据类型的变量实际值存储在栈 ...
- Unity 5.6 beta版本新特性
http://manew.com/thread-98549-1-1.html 最新发布的beta版改进了编辑器和2D功能,图形性能更佳,加入新的视频播放器,并添加了对Facebook Gameroom ...
- 程序员收藏必看系列:深度解析MySQL优化(二)
程序员收藏必看系列:深度解析MySQL优化(一) 性能优化建议 下面会从3个不同方面给出一些优化建议.但请等等,还有一句忠告要先送给你:不要听信你看到的关于优化的“绝对真理”,包括本文所讨论的内容,而 ...
- 阿里云ECS测试服务器部署
前序:为了提供一个干净的测试环境,更好地验证产品问题,也为了防止被开发人员频繁发布代码而打断测试工作,故测试团队搭建了一台阿里云ECS服务器,以下是具体的部署信息: 1. 安装JDK Java版本:J ...
- Jmeter (二十六)逻辑控制器 之 Module Controller and Include Controller
Module Controller ---模块控制器 测试计划设置“独立运行没每个线程组” 线程组2中使用Module Controller执行线程组1中的Sampler: 紧接着,将线程组1disa ...
- 双系统安装Linux的步骤以及一些误区
1.一次安装失败引发的思考 笔者安装双系统(Windows 7+ Ubuntu 16.01)时参考了如何安装win10和linux [ubuntu14]双系统这篇百度经验,却发现安装后并没有如期进入U ...
- P2152 [SDOI2009]SuperGCD
传送门 非常显du然liu的一道题 就是求GCD 因为数据范围... 所以要上压位高精+非递归的辗转相减 关于辗转相减: 如果 A是二的倍数,B是二的倍数 那么GCD(A,B)=2 * GCD(A ...
- Rebus消息总线
这里主要讲一下我基于Rebus写的一个ABP框架的模块 目录结构 对于Rebus网上的资料很少,其实我对于服务总线也不是很理解 ..个人理解的就是像ABP中的EventBus那样的,但是集成了一些 ...
- 这个匿名对象没有实现IComparable接口
https://www.cnblogs.com/felixnet/p/5193086.html https://docs.microsoft.com/zh-cn/dotnet/api/system.i ...