LintCode Two Sum
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的更多相关文章
- lintcode: k Sum 解题报告
K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...
- LintCode "4 Sum"
4 Pointer solution. Key: when moving pointers, we skip duplicated ones. Ref: https://github.com/xbz/ ...
- Lintcode: Subarray Sum 解题报告
Subarray Sum 原题链接:http://lintcode.com/zh-cn/problem/subarray-sum/# Given an integer array, find a su ...
- 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 ...
- [LintCode] Two Sum 两数之和
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- [LintCode] Submatrix Sum 子矩阵之和
Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return ...
- Lintcode: Interval Sum II
Given an integer array in the construct method, implement two methods query(start, end) and modify(i ...
- 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. ...
- Lintcode: Subarray Sum Closest
Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first nu ...
- 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 ...
随机推荐
- C#开发系统服务时用的定时器组件
写服务时,都需要为定时器写不少的代码,感觉很麻烦,今天把这些代码封装一下,希望能简化一下这方面的工作,把精力都集中在功能上 本定时器组件,每次只启动一个服务实例进行处理,而不会同时多次执行服务代码. ...
- Thinkphp上传文件
//1.文件上传Onethink使用$uploads->upload()方法会出现bug,使用$uploads->uploadOne()方法 $config = array( 'maxSi ...
- Python Day02
Python 代码执行流程: 编译 --> 执行 源代码 --> 字节码 --> 机器码 --> CPU执行 python 先将自己的源代码,编译成Python 字节 ...
- js动画之链式运动
链式运动就是当一个运动完,又启动另外一个运动,这个怎么实现呢?这里我们是用用回调函数实现一套链式动画 显示给div左移100像素,然后然后透明度变100 <!DOCTYPE html> & ...
- 手把手教你在Ubuntu上安装Apache、MySql和PHP
1:首先安装apache:打开终端(ctrl+Alt+t), 输入命令:sudo apt-get install apache2即可安装, 安装完后,打开浏览器,在地址栏输入:localhost或者h ...
- 使用BCrypt算法加密存储登录密码用法及好处
//导入import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; /** *使用BCrypt算法加密存储登录密码 ...
- string的实现
面试常常用到string类的实现,自己总结了一下: #pragma once #include <iostream> #include <cassert> #include & ...
- HTML <label> 标签
定义:<label> 标签为 input 元素定义标注(标记). 用法: label 元素不会向用户呈现任何特殊效果.不过,它为鼠标用户改进了可用性.如果您在 label 元素内点击文本, ...
- js⑧
window对象表示浏览器中打开的窗口, 它是JavaScript浏览器对象模型中的顶层对象.其中还包括了 - Document: 使我们可以从脚本中对 HTML 页面中的所有元素进行访问. - Hi ...
- 自定义一个只显示年月的DatePicker(UIDatePicker无法实现年月显示)
HooDatePicker 介绍(introduction) ==================================================项目需要一个DatePicker,只显 ...