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, 无相同元素

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.

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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. combination sum(I, II, III, IV)

    II 简单dfs vector<vector<int>> combinationSum2(vector<int>& candidates, int targ ...

  5. LeetCode: Combination Sum I && II && III

    Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...

  6. Leetcode 137. Single Number I/II/III

    Given an array of integers, every element appears twice except for one. Find that single one. 本题利用XO ...

  7. 买卖股票的最佳时机I II III IV

    I 假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格.如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润. II 假设有一个数组,它的第i个元素是一个给定的股票 ...

  8. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  9. 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 ...

随机推荐

  1. maven插件

    sql-maven-plugin: http://www.mojohaus.org/sql-maven-plugin/ 常用插件: http://www.trinea.cn/android/maven ...

  2. 使用jQuery解析JSON数据

    我们先以解析上例中的comments对象的JSON数据为例,然后再小结jQuery中解析JSON数据的方法. 上例中得到的JSON数据如下,是一个嵌套JSON: {"comments&quo ...

  3. 基于webrtc的资源释放问题(二)

    基于webrtc的资源释放问题(二) ——建立连接的过程中意外中断 应用背景: 我们在打电话的时候会不会遇到这种情况?打电话的时候未接通之前挂掉了电话,或者在接通之后建立的连接的过程中挂掉电话? 特别 ...

  4. Code4App地址

    http://www.code4app.com/ 一个 ios 很多sample的网站

  5. python学习之迭代器与生成器

    1.迭代器省内存 迭代器只允许往后读数据,不允许回读数据 迭代器不能跳着读文件,因为他是一点一点加载文件内容到内存的,读完了可以销毁或丢掉 2.生成一个迭代器 a = iter(["fd&q ...

  6. EPSON LQ610K 设置税控盘打印发票的格式

    问题困扰 之前一直是手动调试发票打印格式,浪费发票纸张不说,而且还浪费时间.今天在Parrells Desktop里利用Bonjour设置打印机的时候,找到了EPSON网站有这方面的介绍,根据上面的提 ...

  7. Netsuite > Employee Record Name维护规则

    Employee Record Name 维护规则 - 在NS系统设计中,默认的Field展现是:First Name, Middle Name, Last Name - 在General Prefe ...

  8. python 学习笔记二十 django项目bbs论坛

    项目:开发一个简单的BBS论坛 需求: 整体参考“抽屉新热榜” + “虎嗅网” 实现不同论坛版块 帖子列表展示 帖子评论数.点赞数展示 在线用户展示 允许登录用户发贴.评论.点赞 允许上传文件 帖子可 ...

  9. EXCEL表格实现万位分隔符效果!

    单击单元格右键 选择自定义单元格格式 选择数字标签 选择自定义 在输入框中输入:###","#### 单击确定即可! 格式刷可以对其他单元格实行同样效果!

  10. vs快捷键

    原文地址 ctrl是强制功能键,shift有给项目增加功能作用 1. 窗口快捷键 : window Ctrl+W,W:  浏览器窗口 (window shopping ) Ctrl+W,S: 解决方案 ...