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 ...
随机推荐
- 制作QQ空间的一些想法
新的项目开始了,这一次是做一个网站类似于QQ空间那样的,基本功能比如说写日志,说说之类的都要有(说说是要有楼中楼嵌套的,应该能够上传图片),还要可以修改个人信息.登录注册之类的更不用说了,还要有一定的 ...
- 第一次接触servlet的知识
什么是Servlet?① Servlet就是JAVA 类② Servlet是一个继承HttpServlet类的类③ 这个在服务器端运行,用以处理客户端的请求 Servlet相关包的介绍--javax. ...
- weboffice控件使用不能嵌入网页
var s = ""s += "<object id=WebOffice1 height=586 width='100%' style='LEFT: 0px; TO ...
- [课程设计]Scrum 3.3 多鱼点餐系统开发进度(下单详细信息页面设计)
Scrum 3.3 多鱼点餐系统开发进度(下单详细信息页面设计) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅到店点 ...
- 对C++虚函数的理解
关于类不断被继承的过程,从整体上看,是一个从抽象到逐渐具体化的过程,基类可以是非常非常抽象的东西,而最终实例化的派生类就非常具体了. 虚函数的意义,就在于定义了一个从最早的基类到最终的派生类都可能会用 ...
- 20161014001 DataGridView 单元格内容 自动计算
private void T_Form_CY_CBD_D_CellValueChanged(object sender, DataGridViewCellEventArgs e) { ...
- C#委托,事件理解入门 (译稿)
原文地址:http://www.codeproject.com/Articles/4773/Events-and-Delegates-Simplified 引用翻译地址:http://www.cnbl ...
- T-SQL编程 —— 用户自定义函数(内嵌表值函数)
内嵌表值函数 接上 <T-SQL编程 -- 用户自定义函数(标量函数)> http://www.cnblogs.com/viusuangio/p/6212072.html 内嵌表值函数可以 ...
- 16-网易-intership
1.多选 //HTML <p>很长的一段文字,很长的一段文字,很长的一段文字,特别长的文字</p> //CSS p{ width:100px; white-space:nowr ...
- Selenium碰到的异常记录
.markdown-preview:not([data-use-github-style]) { padding: 2em; font-size: 1.2em; color: rgb(171, 178 ...