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. 查询多列得到map与查询得到po对象

    import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; i ...

  2. 2.2 Go 常量与枚举

    package main import ( "fmt" "math" ) //常量 func cons(){ , //常量的数字在不明确指定类型的情况下,即可以 ...

  3. 15-----BBS论坛

    BBS论坛(十五) 15.1.登录界面完成 (1)front/signbase.html {% from 'common/_macros.html' import static %} <!DOC ...

  4. SQL Server Reporting Service(SSRS) 第五篇 自定义数据处理扩展DPE(Data Processing Extension)

    最近在做SSRS项目时,遇到这么一个情形:该项目有多个数据库,每个数据库都在不同的服务器,但每个数据库所拥有的数据库对象(table/view/SPs/functions)都是一模一样的,后来结合网络 ...

  5. Mybatis学习笔记9 - 鉴别器discriminator

    鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为. 示例如下: DeptmentMapper接口定义: package com.mybatis.dao ...

  6. Magnum基本介绍

    Magnum is an OpenStack API service developed by the OpenStack Containers Team making container orche ...

  7. 小萝卜控机大师录制脚本(手机app自动化)

    手机自动化测试 之前发布过小萝贝控机大师与按键精灵结合实现手机自动化测试的功能,小萝贝控机大师升级了实现了更多手机自动化测试的功能,如下: l 手机功能自动化测试:录制脚本,检查点时点击小萝贝控机大师 ...

  8. python 钩子函数

    python 在windows下监听键盘按键 使用到的库 ctypes(通过ctypes来调用Win32API, 主要就是调用钩子函数) 使用的Win32API SetWindowsHookEx(), ...

  9. 小程序 页面到详情的id传递

    比如电影列表页跳转到电影详情页 在movie.js获取movieId; processDoubanData: function (moviesDouban, settedKey, categoryTi ...

  10. EF生成的表被更改后的错误

    1.在Global.ascs设置 public class MvcApplication : System.Web.HttpApplication { protected void Applicati ...