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. AJAX需要注意的

    当你写好了与数据库连接的时候,例如这段代码:xmlHttp.open("GET","check.php?user="+url,true); 你不要认为你段代码就 ...

  2. Jmail组件-----发送email

    jmail是一个第三方邮件操作组件,通常位于web服务器端,和站点程序紧密配合来接收及提交邮件到邮件服务器的控件,让网站拥有发送邮件既接收邮件的功能. 之所以想尝试它的理由呢 是因为----jmail ...

  3. Listener实现单态登陆

    MyEclipse中新建Web Project项目,完整目录如下: 需要的jar包为commons-logging-xxx.jar 1.singleton.jsp <%@ page langua ...

  4. IHttpHandler给图片加水印

    /// <summary> /// WaterMarkHandlher 的摘要说明 /// </summary> public class WaterMarkHandlher ...

  5. Bootstrap<基础十六> 导航元素

    Bootstrap 提供的用于定义导航元素的一些选项.它们使用相同的标记和基类 .nav.Bootstrap 也提供了一个用于共享标记和状态的帮助器类.改变修饰的 class,可以在不同的样式间进行切 ...

  6. 在一个老外微信PM的眼中,中国移动App UI那些事儿

    本文编译自Dan Grover的博客,他现在是腾讯微信的产品经理.以下是他从旧金山搬到广州后的近半年时间里,在试用过微信微博等中国主流移动App后,总结出的中美App在设计理念上的差异,并对中国移动A ...

  7. logistc regression练习(三)

    % Exercise 4 -- Logistic Regression clear all; close all; clc x = load('E:\workstation\data\ex4x.dat ...

  8. Win7 下IIS(7.5)发布 ASP.NET MVC

    操作系统 Win 7 旗舰版 开发工具 VS2015 使用技术 IIS7.5 + MVC4.0 一 . 在IIS上部署程序后出现错误-当前标识(NT AUTHORITY/NETWORK SERVICE ...

  9. Learning Bayesian Network Classifiers by Maximizing Conditional Likelihood

    Abstract Bayesian networks are a powerful probabilistic representation, and their use for classifica ...

  10. html input节点很多 json字符串提交解决方法

    遇到一个页面,38个input节点,页面前端写好的,不太容易改成 js框架 容易操作的样式,只能自己想办法一个一个id获取然后 setvalue getvalue(miniui): 38个一个一个写太 ...