1. Two Sum I & II & III
1. Given an array of integers, return indices of the two numbers such that they add up to a specific target.
问题:
1.数组是否有序
2. 数组排序
// sorting array
Arrays.sort(iArr)
方法1:O(n^2)
public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
for (int i = 0; i < nums.length; i++){
for (int j = i+1; j < nums.length; j++){
if(nums[i] + nums[j] == target){
result [0] = i;
result [1] = j;
}
}
}
return result;
}
}
2. Two sum II Input array is sorted (HashMap, 无相同元素
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
public class Solution {
public int[] twoSum(int[] nums, int target) {
int [] result = new int[2];
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
for(int i = 0; i < nums.length; i++){
hm.put(nums[i], i);
}
for(int i = 0; i < nums.length; i++){
if(hm.containsKey(target-nums[i]) && target != 2 * nums[i]){
result[0] = i;
result[1] = hm.get(target-nums[i]);
break;
}
}
return result;
}
}
3. Two sum III
1. Two Sum I & II & III的更多相关文章
- Leetcode 39 40 216 Combination Sum I II III
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- Path Sum I && II & III
Path Sum I Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that ad ...
- Two Sum I & II & III & IV
Two Sum I Given an array of integers, find two numbers such that they add up to a specific target nu ...
- combination sum(I, II, III, IV)
II 简单dfs vector<vector<int>> combinationSum2(vector<int>& candidates, int targ ...
- LeetCode: Combination Sum I && II && III
Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...
- Leetcode 137. Single Number I/II/III
Given an array of integers, every element appears twice except for one. Find that single one. 本题利用XO ...
- 买卖股票的最佳时机I II III IV
I 假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格.如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润. II 假设有一个数组,它的第i个元素是一个给定的股票 ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- Two Sum I & II
Two Sum I Given an array of integers, find two numbers such that they add up to a specific target nu ...
随机推荐
- 设置myeclipse新建jsp文件默认编码为UTF-8
有三个地方需要改编码设置: 1. window-->preference-->general-->contenttype 然后在content types中展开每一个子项,并在Def ...
- Navicat for MySQL安装之后不知道登录密码
1,关闭你现在正在运行的mysql数据库,关闭mysql服务器. 2,关闭数据库后,运行点击开始运行,输入cmd进入命令行窗口,在这个命令行中操作进入到你数据库所在的安装路径,一般默认安装的话都会在e ...
- java 调用axis2 webservice
import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apach ...
- 关于Java占用内存的研究
最近对程序占用内存方面做了一些优化,取得了不错的效果,总结了一些经验简要说一下,相信会对大家写出优质的程序有所帮助下面的论述针对32位系统,对64位系统不适用,后叙 经常你写了一个程序,一测试,功能没 ...
- Java轻量级业务层框架Spring两大核心IOC和AOP原理
IoC(Inversion of Control): IOC的基本概念是:不创建对象,但是描述创建它们的方式.在代码中不直接与对象和服务连接,但在配置文件中描述哪一个组件需要哪一项服务.容器负责将这些 ...
- Javascript学习笔记:闭包题解(4)
代码: var val1=0; var val2=0; var val3=0; for(var i1=1;i1<=3;i1++){ var i2=i1; (function(){ var i3= ...
- 剑指offer五:
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. /* public class ListNode { int val; ListNode next = n ...
- java基本数据类型的字面量
java的基本数据类型一共有8种.其中:(每种类型后面列出了java中的该类型的字面量) 四种整型: int 4字节: 12 +12 -12 077 0xFF 0b101(JDK7中支持的二 ...
- mySQL数据库Sql语句执行效率检查--Explain命令
mysql性能的检查和调优方法 Explain命令在解决数据库性能上是第一推荐使用命令,大部分的性能问题可以通过此命令来简单的解决,Explain可以用来查看SQL语句的执行效 果,可以帮助选择更好的 ...
- winform右下角弹窗
网页是否经常在电脑右下角弹窗显示消息?其实Winform也是可以实现的.下面介绍两种方法. 第一步:设计窗体 第二步:实现代码 第一种方法 引用user32 声明常量 窗体Load事件 窗体FormC ...