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. HTML初步入门

    标签元素 标签介绍 html元素包括一个或一对标签定义的包含范围.而标签就是由两个字符串"<"和">"号组成,标签包括开始标签"<& ...

  2. Computed read-only property vs function in Swift

    In the Introduction to Swift WWDC session, a read-only property description is demonstrated: class V ...

  3. AChecker + Selenium2对需要登录的页面进行自动化可访问性测试

    前言:这段时间还算比较空闲,我准备把过去做过的有些形形色色,甚至有些奇怪的研究总结一下,也许刚好有人用的着也不一定,不枉为之抓耳挠腮的时光和浪费的电力.   名词解释: 网站可访问性测试:国内基本没有 ...

  4. dmidecode查看设备硬件信息

    在bash里输入:dmidecode -s system-product-name 或者lshw -class system 在Linux系统环境下(CentOS .4和Ubuntu .04已确认), ...

  5. OAuth2.0说明文档

    OAuth2.0说明文档 1.OAuth 2.0 简介 OAuth为应用提供了一种访问受保护资源的方法.在应用访问受保护资源之前,它必须先从资源拥有者处获取授权(访问许可),然后用访问许可交换访问令牌 ...

  6. finish()在dialog中的作用

    finish()在dialog中销毁的是dialog所在的活动:

  7. Mapreduce体系架构

    Mapreduce也采用master和slave的架构设计.Jobtracker负责作业的初始化和分配 与任务节点进行通信,协调整个作业的执行. 一个job分为两种task(map/reduce),包 ...

  8. Qt设计师学习笔记--Sharping-Changing Dialogs

    1.pushbutton->default属性为true,按回车相当于点击该按钮. 2.选中checkable后,Button变成切换按钮(toggle button),可以有两种状态:按下/弹 ...

  9. EL表达式语言总结

    EL介绍 Expressive Language, JSP2.0引入,简化jsp开发中对对象的引用. 基本语法 ${表达式} 常见用法 根据其访问对象,可大体分成三类:访问数据及其各种表达式,访问EL ...

  10. 关于 jquery和js获取宽度时只能取整数,取不到小数点

    最近在改版自已的一个网站的时候,遇到了一个问题. 用jquery的width()函数获取元素宽度的时候,返回得到的是整数,而不是小数. 如下图,谷歌上显示的宽度为1078.89px 而我用控制台输出了 ...