*1 Two Sum two pointers(hashmap one scan)
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
incompleted solution: need to remember the index after sorting
class Solution {
int bSearch(int[] nums, int left, int right, int comple){ // []
while(right < nums.length && right >= left){
int m = (right-left)/2 + left;
if(nums[m] == comple) return m;
if(nums[m] > comple) right = m-1;
else if(nums[m] < comple) left = m+1;
}
return -1;
}
public int[] twoSum(int[] nums, int target) {
//binary search
int[] res = new int[2];
Arrays.sort(nums);
for(int i = 0; i<nums.length; i++){
int complement = target - nums[i];
//System.out.println(complement);
int val = bSearch(nums, i+1, nums.length-1, complement);
System.out.println(val);
if(val!=-1){
res[0] = i;res[1] = val;
break;
}
}
return res;
}
}
-------------comparator and comparable
SOlution: hash map, one scan or two scans
<Integer, Integer>, store the array's value and index <value, index>
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
Map<Integer, Integer> hashmap = new HashMap<>(); // number , index
for(int i = 0; i<nums.length; i++){
int comp = target - nums[i];
if(hashmap.containsKey(comp)){
res[0] = hashmap.get(comp); res[1] = i;
break;
}else {
hashmap.put(nums[i],i);
}
}
return res;
}
}
*1 Two Sum two pointers(hashmap one scan)的更多相关文章
- Leetcode: Path Sum III
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- Java7与Java8中的HashMap和ConcurrentHashMap知识点总结
JAVA7 Java7的ConcurrentHashMap里有多把锁,每一把锁用于其中一部分数据,那么当多线程访问容器里不同数据段的数据时,线程间就不会存在锁竞争,从而可以有效的提高并发访问效率呢.这 ...
- Spark读取Hbase的数据
val conf = HBaseConfiguration.create() conf.addResource(new Path("/opt/cloudera/parcels/CDH-5.4 ...
- LeetCode解题录-1~50
[leetcode]1. Two Sum两数之和 Two Pointers, HashMap Easy [leetcode]2. Add Two Numbers两数相加 Math, LinkedLis ...
- 2016京东Android研发校招笔试题
一.选择题汇总,具体的记不住啦.. 1.计网:ip的网络前缀.SNMP(报文组成):http://blog.csdn.net/shanzhizi/article/details/11606767 参考 ...
- ID3决策树的Java实现
package DecisionTree; import java.io.*; import java.util.*; public class ID3 { //节点类 public class DT ...
- HQueue:基于HBase的消息队列
HQueue:基于HBase的消息队列 凌柏 1. HQueue简介 HQueue是一淘搜索网页抓取离线系统团队基于HBase开发的一套分布式.持久化消息队列.它利用HTable存储消息数据 ...
- LeetCode 15. 3Sum(三数之和)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- LeetCode第五天
leetcode 第五天 2018年1月6日 22.(566) Reshape the Matrix JAVA class Solution { public int[][] matrixReshap ...
随机推荐
- hdu6397 Character Encoding 隔板法+容斥原理+线性逆元方程
题目传送门 题意:给出n,m,k,用m个0到n-1的数字凑出k,问方案数,mod一个值. 题目思路: 首先如果去掉数字范围的限制,那么就是隔板法,先复习一下隔板法. ①k个相同的小球放入m个不同的盒子 ...
- Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot(二分或者尺取)
题目哦 题意:给出一个序列,序列有四个字母组成,U:y+1,D:y-1 , L:x-1 , R:x+1; 这是规则 . 给出(x,y) 问可不可以经过最小的变化这个序列可以由(0,0) 变到(x, ...
- vue学习—组件的定义注册
组件的定义注册 效果: 方法一: <div id="box"> <v-header></v-header> <hr /> <b ...
- 单元测试mock框架——jmockit实战
JMockit是google code上面的一个java单元测试mock项目,她很方便地让你对单元测试中的final类,静态方法,构造方法进行mock,功能强大.项目地址在:http://jmocki ...
- runlevel:启动运行级别(3-13)
0:halt 关机模式1:single user 单用户2:Multiuser 多用户3:Full multiuser mode 命令行模式4:unused 没有使用5:Xll 桌面模式6:reboo ...
- SQL Server Reporting Service(SSRS) 第四篇 SSRS 常见问题总结
1. 如何让表头在每页显示(译) A. 打开高级模式: 在分组栏中点击Column Goups右侧的箭头选择高级模式; B. 找到第一个Static组 在Row Groups区域中(注意不是Colu ...
- Django-1 简介
1.1 MVC与MTV模型 MVCWeb服务器开发领域里著名的MVC模式,所谓MVC就是把Web应用分为模型(M),控制器(C)和视图(V)三层,他们之间以一种插件式的.松耦合的方式连接在一起,模型负 ...
- Django-5.2 模型层 多表操作
7.3 多表操作 一.创建模型 实例:我们来假定下面这些概念,字段和关系作者模型:一个作者有姓名和年龄.作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之 ...
- Easy Touch 摇感控制人物移动
Easy Touch 摇感控制人物移动 public class joystick : MonoBehaviour { public float Speed; //定义速度 p ...
- 创建weblogic domain
做成了PDF文档 这里可下载可以看 config.sh 命令目录 weblogic/Oracle/Middleware/wlserver_10.3/common/bin├── commEnv.sh├─ ...