*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 ...
随机推荐
- caffe 日志保存以及matlab绘制方法(windows以及ubuntu下)
caffe 用matlab解析日志画loss和accuracy clc; clear; % load the log file of caffe model fid = fopen('log-prev ...
- ubuntu同时装有MXNet和Caffe框架
我阐述一下我遇到的问题:因为之前装过caffe,最近装了MXNet.MXNet可以运行,但import caffe就不行了,找不到模块. 那应该怎么处理呢??? 参考了一下这个网站:https://i ...
- Unity 为什么有时候播放音乐(音效)会没有声音
1.问题描述 昨晚,我遇到的情况如下: 1.MainCamera里有Audio Source,并且在循环播放音乐 2.在其他的GameObject中也新增一个Audio Source,在某个时机播放音 ...
- 牛客网Java刷题知识点之Java 集合框架的构成、集合框架中的迭代器Iterator、集合框架中的集合接口Collection(List和Set)、集合框架中的Map集合
不多说,直接上干货! 集合框架中包含了大量集合接口.这些接口的实现类和操作它们的算法. 集合容器因为内部的数据结构不同,有多种具体容器. 不断的向上抽取,就形成了集合框架. Map是一次添加一对元素. ...
- Zend Optimizer安装、配置
Zend Optimizer用优化代码的方法来提高php应用程序的执行速度.实现的原理是对那些在被最终执行之前由运行编译器(Run-Time Compiler)产生的代码进行优化.这里,我们下载最新版 ...
- Window WindowManager 和WindowManager.LayoutParams
<一> Window window是android中的窗口,表示顶级窗口的意思,也就是主窗口,它有两个实现类, PhoneWindow和MidWindow,我们一般的activity对应的 ...
- vscode好用的扩展及常用的快捷键
1.open-in-browser 或者view in browser 安装后右键即可快速打开浏览器 2.quokka调试工具插件,能对正在编写的代码提供实时反馈,并能预览变量的函数和计算结果 3. ...
- 为什么地址栏的快捷键是Alt D
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:为什么地址栏的快捷键是Alt D.
- 简简单单谈WCF
另一个系统去访问另一个系统,就是需要使用到分布式通讯咯.. 1. webService .netfromwork3.5中存在 2. webapi 3. Wcf scop通讯协议 以上三种都是 ...
- 拿到返回值,Callable示例