1. 数组numbers == null 及numbers.length == 0, 而不是用numbers[]

2. HashMap<Integer, Integer>而不是<int, int>

3. 先找有没有余数, 没有则将自身加入到哈希表里, 有的话直接返回自身的位置和余数的位置(如果先找有没有自身, 则不能确定有没有余数)

4. for循环里的return能跳出方法, 而break只能跳出循环, 方法里的剩余语句还要走(代码里for循环里的return若在则返回第一个符合的答案, 若不在则返回最后一个符合的答案)

5. if语句特别注意的情况, 若不写else而用if(存在余数), 则此时在同一个循环里就会用两次自身(若目标为自身两倍时)

public class Solution {
/*
* @param numbers : An array of Integer
* @param target : target = numbers[index1] + numbers[index2]
* @return : [index1 + 1, index2 + 1] (index1 < index2)
*/
public int[] twoSum(int[] numbers, int target) {
int[] arr = new int[2];
if(numbers == null) return arr;
if(numbers.length == 0) return arr;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for(int i = 0; i < numbers.length; i++){
if(!map.containsKey(target - numbers[i])){
map.put(numbers[i], i + 1);
} else{
arr[0] = map.get(target - numbers[i]);
arr[1] = i + 1;
//return arr;
} }
return arr;
// write your code here
}
}

LintCode Two Sum的更多相关文章

  1. lintcode: k Sum 解题报告

    K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...

  2. LintCode "4 Sum"

    4 Pointer solution. Key: when moving pointers, we skip duplicated ones. Ref: https://github.com/xbz/ ...

  3. Lintcode: Subarray Sum 解题报告

    Subarray Sum 原题链接:http://lintcode.com/zh-cn/problem/subarray-sum/# Given an integer array, find a su ...

  4. LintCode Subarray Sum

    For this problem we need to learn a new trick that if your start sum up all elements in an array. Wh ...

  5. [LintCode] Two Sum 两数之和

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  6. [LintCode] Submatrix Sum 子矩阵之和

    Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return ...

  7. Lintcode: Interval Sum II

    Given an integer array in the construct method, implement two methods query(start, end) and modify(i ...

  8. Lintcode: Interval Sum

    Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...

  9. Lintcode: Subarray Sum Closest

    Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first nu ...

  10. LintCode "Submatrix Sum"

    Naive solution is O(n^4). But on 1 certain dimension, naive O(n^2) can be O(n) by this well-known eq ...

随机推荐

  1. php发送邮件

    <?php header("content-Type: text/html; charset=utf-8"); require("class.phpmailer.p ...

  2. java.net.UnknownHostException: Unable to resolve host "api102.meishi.cc": No address associated with hostname

    有三个原因: 清单文件Mei加网络访问权限 在主线程中执行了耗操作 你没开wifi

  3. NGUI屏幕自适应

    NGUI确实是非常棒的一个做界面的插件,比起U3D自带的GUI要好很多,当然也有一些不好之处,毕竟什么都不可能那么完美. 最近在用Unity写游戏使用NGUI遇到了一个很多人都在遇到的问题,就是关于屏 ...

  4. Work around by " Due to heavy load, the latest workflow operation has been queued. " 分类: Sharepoint 2015-07-08 00:19 3人阅读 评论(0) 收藏

    I hope most of the users and developers might have come across above note and worried about it. Ther ...

  5. java基础之 多态

    在面向对象编程(Object-Oriented Programming, OOP)中,多态机制无疑是其最具特色的功能,甚至可以说,不运用多态的编程不能称之为OOP.这也是为什么有人说,使用面向对象语言 ...

  6. 常用CSS样式

    1.line-height:行高.默认normal normal:允许内容顶开或溢出制定的容器边界; length:15px,可以为负数; ... 2.overflow:滚动条设置 overflow- ...

  7. 使用Mulesoft建立webservice, simple方式,POJO

    Mulesoft是使用CXF来支持web service,有三种方式 1.JAX-WS 2.Simple, POJO 3. Proxy, pass-throught 本文介绍POJO,最简单的方式 1 ...

  8. 用Window Authentication的方式去连接SQLServer

    用Window Authentication的方式去连接SQLServer Connection String: jdbc:sqlserver://${serverName};databaseName ...

  9. 怎样用SQL语句查询一个数据库中的所有表?

    怎样用SQL语句查询一个数据库中的所有表?  --读取库中的所有表名 select name from sysobjects where xtype='u'--读取指定表的所有列名select nam ...

  10. VS.net中快捷键收缩和展开代码段 (转)

    i. Ctrl-M-O   折叠所有方法 ii. Ctrl-M-P   展开所有方法并停止大纲显示(不可以再折叠了) iii. Ctrl-M-M   折叠或展开当前方法 iv. Ctrl-M-L展开所 ...