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 ...
随机推荐
- 题目描述: k一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
时间限制:1秒 空间限制:32768k 斐波那契数列指的是这样一个数列: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,9 ...
- Java8闭包
闭包在很多语言中都存在,例如C++,C#.闭包允许我们创建函数指针,并把它们作为参数传递,Java编程语言提供了接口的概念,接口中可以定义抽象方法,接口定义了API,并希望用户或者供应商来实现这些方法 ...
- Android ProgressBar分析及自定义ProgressBar
ProgressBar是在执行耗时操作时的一种人性化设计.分为两种形式:转圈的,能显示进度的. 而能取决于是什么样式的PregressBar,当然就是PregressBar的样式啦~ Widget.P ...
- asp.net 微信开发失效汇总
1.验证控件 在Iphone 5以上版本不兼容(改为js验证)
- Java 自定义客户端与服务器
一:浏览器如何请求数据基本原理 基本原理: 当浏览器输入地址向服务器请求数据时,实际上浏览器会在内部建立一个Socket对象,把http请求报文转变成byet[]字节,然后调用Socket的方法把这些 ...
- 非默认安装目录下mysql数据的导出与导入
系统:Centos 6.5 1.首先确定msyql是否安装以及安装目录: [root@localhost ~]# service mysqld status mysqld (pid ) 正在运行... ...
- BASH比较大小
- 7. Swift 基于Xmpp和openfire实现一个简单的登录注册
1. 基本步骤:首先导入Xmpp框架,配置环境 ->由于我们使用的是OC的Xmpp框架,再进行Swift开发时需要进行桥接. 具体方法就是创建一个基于c的.h的头文件,然后将我们需要编译OC的语 ...
- centos6.5 安装linux 环境
准备工作 安装make yum -y install gcc automake autoconf libtool make 安装g++ yum install gcc gcc-c++下面正式开始--- ...
- Mysql 5.7.12 配置
打算用express+mysql写一个博客.本来在公司电脑已经配置好了的,但是为了方便在家里也能修改,所以在自己的电脑里也安装好环境. 公司电脑是win7系统32位的,安装的是5.5的mysql,用的 ...