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. 杭电1597--find the nth digit--假设:S1 = 1,S1=12,S3=123,S4=1234...

    我是把它分层来求的,也就是说第一层是1,第二层是12,第三层是123......你们该懂的!! #include <iostream>#include <cmath>using ...

  2. Android中View的基础知识

    View的界限 View就是我们看到的界面,有四个界限范围分别是, Top/Left, Bottom/Right,坐标系从左上到右下.这四个值可以通过任何View的子类调用getTop()/get.. ...

  3. Single-page app(SPA)

    有哪些值得推荐的一页式网站(Single-page app)? http://pro.weltrade.com/en/ 最近开到一下国外网站,一页到底,感觉很高大上,到底是怎么做出来的呢?技术要点是什 ...

  4. ThinkPHP模板中如何操作session,以及如果session中保存的是数组的情况

    在ThinkPHP的模板中操作session时,可以参考ThinkPHP参考文档中的“模板—>系统变量”部分,在默认模板引擎中,语法如下: {$Think.session.user} //输出s ...

  5. js图片拖放原理(很简单,不是框架,入门基础)

    <html> <meta> <script src='jquery-1.8.3.min.js'></script> <script> /* ...

  6. CentOS 安装VNC Server

    环境 服务器:192.168.10.181 系统:CentOS 6.0 安装过程 1.切换至root用户 2.检测系统是否安装VNC [root@Nginx canyouNgx]# rpm -q vn ...

  7. 如何下载youtube上面的视频

    youtube做为全球最大的视频共享网站,其视频数量难以计数. 那么你是不是从上面发现了你中意MV,或者一些别的视频?但是你却为无法下载这些视频而苦恼? http://vixy.net/flv_con ...

  8. 【转】tomcat性能调优

    一.总结前一天的学习 从"第三天"的性能测试一节中,我们得知了决定性能测试的几个重要指标,它们是: ü   吞吐量 ü   Responsetime ü   Cpuload ü   ...

  9. Objective-C学习笔记-第四天(1)

    解决以下昨天遇到的问题 1.@class与import是怎么样的呢?参考:http://www.cnblogs.com/ios8/p/ios-oc-test.html 在头文件中, 一般只需要知道被引 ...

  10. mysql 大小写问题-sql-mode问题

    一.mysql 字段名 表名 数据库名 是否区分大小写 今天碰到数据库大小写问题,linux与windows下问题 同时又碰到保留字 http://www.cnblogs.com/lawdong/ar ...