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)的更多相关文章

  1. Leetcode: Path Sum III

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  2. Java7与Java8中的HashMap和ConcurrentHashMap知识点总结

    JAVA7 Java7的ConcurrentHashMap里有多把锁,每一把锁用于其中一部分数据,那么当多线程访问容器里不同数据段的数据时,线程间就不会存在锁竞争,从而可以有效的提高并发访问效率呢.这 ...

  3. Spark读取Hbase的数据

    val conf = HBaseConfiguration.create() conf.addResource(new Path("/opt/cloudera/parcels/CDH-5.4 ...

  4. LeetCode解题录-1~50

    [leetcode]1. Two Sum两数之和 Two Pointers, HashMap Easy [leetcode]2. Add Two Numbers两数相加 Math, LinkedLis ...

  5. 2016京东Android研发校招笔试题

    一.选择题汇总,具体的记不住啦.. 1.计网:ip的网络前缀.SNMP(报文组成):http://blog.csdn.net/shanzhizi/article/details/11606767 参考 ...

  6. ID3决策树的Java实现

    package DecisionTree; import java.io.*; import java.util.*; public class ID3 { //节点类 public class DT ...

  7. HQueue:基于HBase的消息队列

    HQueue:基于HBase的消息队列   凌柏   ​1. HQueue简介 HQueue是一淘搜索网页抓取离线系统团队基于HBase开发的一套分布式.持久化消息队列.它利用HTable存储消息数据 ...

  8. 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 ...

  9. LeetCode第五天

    leetcode 第五天 2018年1月6日 22.(566) Reshape the Matrix JAVA class Solution { public int[][] matrixReshap ...

随机推荐

  1. 9.ORM数据访问

    1.Spring对ORM的支持 ORM : 对象关系映射(Object Relational Mapping)是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术基于ORM的数据持久层框架有: ...

  2. PHP rand 和 mt_rand

    PHP mt_rand() 函数 定义和用法 mt_rand() 使用 Mersenne Twister 算法返回随机整数. 语法 mt_rand(min,max) 说明 如果没有提供可选参数 min ...

  3. 爬虫(GET)——爬取多页的html

    工具:python3 目标:将编写的代码封装,不同函数完成不同功能,爬取任意页数的html 新学语法:with open as 除了有更优雅的语法,with还可以很好的处理上下文环境产生的异常. # ...

  4. Could not read settings.xml

    这个问题为什么会发生? 其实不要想太多, 1.文件格式是utf-8 2.其中的报文格式非常重要,千万不能弄错,如果多了一处注释,就会发生以上问题,拼写的时候多注意语义

  5. PHP jsonencode unicode 存储问题

    首先是这样的,因为输入的字符串的里面有德语的字符,如下: 当我存储到数据库之后,再用json_encode获取到数据库内的这些字符时,出问题了. 直接encode一个字符串"püüäöä&q ...

  6. Head First 设计模式笔记(适配器)

    1.定义: 将一个类的接口转换成客户期望的另外一个接口.适配器让原来不兼容的类可以合作无间. 例子:插座转接头. 2.类图: 3.说明: 埋坑 4.例子 埋坑

  7. sql server 2017安装

    下载: 1. 2. 3. 安装步骤: https://www.cnblogs.com/ksguai/p/5869558.html 管理工具: Microsoft SQL Server Manageme ...

  8. Redis Intro - Dict

    https://segmentfault.com/a/1190000004850844

  9. .NET控制台程序监听程序退出

    There are mainly 2 types of Win32 applications, console application and window application. They hav ...

  10. 将应用图标添加到ubuntu dash中

    1 在appplications中添加一个desktop文件 sudo gedit /usr/share/applications/xdbe.desktop 2 在desktop文件中添加如下 [De ...