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. HDU 4507 求指定范围内与7不沾边的所有数的平方和 (数位DP)

    题意:求区间[l,r]内所有与7无关的数的平方和(取模)定义与7无关的数:                                      1.数字的数位上不能有7              ...

  2. Flask&&人工智能AI -- 7 MongoDB

    MongoDB初识,增删改查操作,数据类型,$关键字以及$修改器.“$”的奇妙用法,Array Object的特殊操作,选取跳过排序,客户端操作 一.MongoDB初识 什么是MongoDB Mong ...

  3. 解决IDEA卡顿问题及相关基本配置

    https://blog.csdn.net/u013068377/article/details/54316965 https://blog.csdn.net/u014527619/article/d ...

  4. sql CET实现循环

    表结构 CREATE TABLE city( id INT IDENTITY(1,1) PRIMARY KEY, NAME NVARCHAR(100), ParentID INT , Parents ...

  5. 基于wireshark抓包分析TCP的三次握手

    1. TCP的三次握手 在TCP/IP协议通讯过程中,采用三次握手建立连接,从而保证连接的安全可靠. 所有基于TCP的通信都需要以两台主机的握手开始.这个握手过程主要是希望能达到以下不同的目的.[1] ...

  6. C++ opencv 滑动条 Trackbary以及处理三通道和单通道图像

    #include <opencv2\core.hpp> #include <opencv2\highgui.hpp> #include <opencv2\imgproc. ...

  7. CSS background 属性全家桶

    介绍我们都知道css的background属性是一个复合属性,可以简写成一行代码,也可以将每个属性分开来写. background 简写属性在一个声明中设置所有的背景属性.如:body{ backgr ...

  8. java——调用一个静态方法的时候有没有执行这个类的构造方法,以及这个类中的静态常量?

    尝试一下: public class Try { final static int a = 1; public Try() { System.out.print("构造方法"); ...

  9. javascript里label语句的简单示例

    在javascript中,我们可能很少会去用到 Label 语句,但是熟练的应用 Label 语句,尤其是在嵌套循环中熟练应用 break, continue 与 Label 可以精确的返回到你想要的 ...

  10. Troubleshooting ORA-201 and ORA-202 Error

    ---- 3. When lowering the value of COMPATIBLE: You cannot start the database with lower compatibilit ...