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的情况下 ...
随机推荐
- C++读取XML,tinyXml的使用
前言: 最近在开发的过程中,有个需求是对xml进行格式转化,从一种格式转化到另外一种格式.因此,就需要读取xml进行处理.原本打算写成工具在linux下运行,不过后来考虑到和系统结合,最后也就使用了前 ...
- Updatepanel 中使用 Timer 控件 失去焦点问题
在Update Panel 中 如果使用timer 定时刷新数据,会造成textbox 或者其他控件的焦点丢失问题. 所以 text box 不能和timer 放在同一个Updatepanel 中. ...
- 洛谷P1633 二进制
P1633 二进制 题目描述 有三个整数A.B.C,以下用N(2)表示N的二进制(没有前导0). 设A(2).B(2).C(2)的最大长度为L,你需要构造三个正整数X.Y.Z,满足以下条件: (1) ...
- 聊聊 Laravel 5.5 的 「自动发现」
ThinkSNS是什么? ThinkSNS(简称TS),一款全平台综合性社交系统,目前最新版本为ThinkSNS+.ThinkSNS V4 ThinkSNS[简]. 看了Taylor Otwell发表 ...
- MyEclipse 2016CI破解版for Mac
网上的教程很多,一开始怎么都不行,就是不行,也是如此操作,可能是注册机失效了还是怎样,数个小时过去了,我综合了网上的资源,终于OK啦!(我会在文后给出jar 包,注册机的破解文件,以及MyEclips ...
- 极客学院年VIP卡原价260的F码,200出售
F码是中国最大的IT职业在线教育平台——极客学院推出的VIP时间兑换码,凭此可在极客学院官网兑换年VIP,畅享平台上所有IT技术课程. 购买请点击 http://www.bejson.com/othe ...
- [Android]进程间通信的方法
一.管道 管道是进程间通信中最古老的方式,它包括 无名管道 和 有名管道两种,前者用于父进程和子进程间的通信,后者用于运行于同一台机器上的任意两个进程间的通信. 无名管道由pipe()函数创建. #i ...
- 牛客假日团队赛2 C.修围栏
链接: https://ac.nowcoder.com/acm/contest/924/C 题意: 农民 John 希望修复围绕农场的一小段围栏.他测量了一下,发现需要N (1 <= N < ...
- bzoj1822: [JSOI2010]Frozen Nova 冷冻波网络流
思路比较显然:二分答案,流流流 但是实现的时候感觉自己数学捉急.. 一开始算了个直线到点距离.... 应该是线段到点距离 #include <bits/stdc++.h> #define ...
- codeforces540E-树状数组求逆序对
1-1e9的数据范围 但有1e5个区间 所以可以考虑把没有涉及到的区间缩成一个点 然后树状数组求逆序对 #include<bits/stdc++.h> #define inf 0x3f3f ...