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. Visro 应用的前端模板工具介绍 -JsRender

    1.什么是JsRender: JsRender是一款JavaScript模版引擎,是具有简单直观,功能强大,可扩展的,早期版本是基于JQUERY 写的,后来作者重构了,就不再依赖JQUERY了. 它的 ...

  2. 使用git删除远程仓库文件

    git rm -r -f --cached 文件或文件夹 git commit -m "移除文件或文件夹" git push origin master 注意:要删除的文件或文件夹 ...

  3. SpringMVC与MyBatis整合之日期格式转换

    在上一篇博客<SpringMVC与MyBatis整合(一)——查询人员列表>中遗留了日期格式转换的问题,在这篇记录解决过程. 对于controller形参中pojo对象,如果属性中有日期类 ...

  4. stella mccartney falabella foldover tote a few eye observed

    Lately, the particular Heyuan City Courtroom retrial, in order to commit the criminal offense of cou ...

  5. AngularJS身份验证:Cookies VS Tokens

    基于cookie的身份验证:Cookie-Based Authentication 基于token的身份验证:Token-Based Authentication 跨域:cross-domain 说明 ...

  6. CDS

    very nice artical talk about mergechangelog and cleardataset Delta and Data http://www.cnblogs.com/y ...

  7. HIS-DELPHI-读取数据库配置

    产品思维: 1.做成可配置的 2.模块化 医生会有自己熟悉的药品,数据里面药品太多,让医生选择不放便 所以可以让医生自己维护自己的药品模板数据 比如医生开了处方后,可以保存当前的处方到某个模板中,那么 ...

  8. 2014年6月份第2周51Aspx源码发布详情

    AMX高效自定义分页控件(WinForm)源码  2014-6-9 [VS2008]2014.6.9更新内容:   1. 更改用户自定义分页控件功能布局.大大精简了调用分页自定义控件的代码,和使用系统 ...

  9. 第二章 XHTML 基础

    元素与标签术语,HTML/XHTMLXHTML之间的联系区别在XHTML中,所有元素之间必须完成正确的嵌套,元素必须是闭合的,必须小写.必须有个跟元素HTML. 标题标<h1>语法:< ...

  10. ubuntu samba server 配置多用户访问

    [share] path = /home/share/ writeable = yes browseable = yes create mask = directory mask = guest ok ...