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:

  1. Given nums = [2, 7, 11, 15], target = 9,
  2.  
  3. Because nums[0] + nums[1] = 2 + 7 = 9,
  4. return [0, 1].

incompleted solution: need to remember the index after sorting

  1. class Solution {
  2.  
  3. int bSearch(int[] nums, int left, int right, int comple){ // []
  4. while(right < nums.length && right >= left){
  5. int m = (right-left)/2 + left;
  6. if(nums[m] == comple) return m;
  7. if(nums[m] > comple) right = m-1;
  8. else if(nums[m] < comple) left = m+1;
  9. }
  10. return -1;
  11. }
  12. public int[] twoSum(int[] nums, int target) {
  13. //binary search
  14. int[] res = new int[2];
  15. Arrays.sort(nums);
  16. for(int i = 0; i<nums.length; i++){
  17. int complement = target - nums[i];
  18. //System.out.println(complement);
  19. int val = bSearch(nums, i+1, nums.length-1, complement);
  20. System.out.println(val);
  21. if(val!=-1){
  22. res[0] = i;res[1] = val;
  23. break;
  24. }
  25.  
  26. }
  27. return res;
  28. }
  29. }

-------------comparator and comparable

SOlution: hash map, one scan or two scans

<Integer, Integer>, store the array's value and index  <value, index>

  1. class Solution {
  2. public int[] twoSum(int[] nums, int target) {
  3. int[] res = new int[2];
  4. Map<Integer, Integer> hashmap = new HashMap<>(); // number , index
  5. for(int i = 0; i<nums.length; i++){
  6. int comp = target - nums[i];
  7. if(hashmap.containsKey(comp)){
  8. res[0] = hashmap.get(comp); res[1] = i;
  9. break;
  10. }else {
  11. hashmap.put(nums[i],i);
  12. }
  13. }
  14. return res;
  15. }
  16. }

*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. Java的观察者

    class Teacher extends Observable { public void startLesson() { System.out.println(String.format(&quo ...

  2. Java8 流的使用示例

    foreach遍历处理 dataList.stream().forEach(index -> sb.append(dataList.get(index) + "',")); ...

  3. MetricStatTimer

    package org.apache.storm.metric.internal; import java.util.Timer; /** * Just holds a singleton metri ...

  4. WIN2008R2 asp.net core的配置

    配置IIS Windows Server上通过“添加角色和功能”,桌面Windows上通过“启用和关闭Windows功能”来安装和配置IIS.确保勾选Web服务和“IIS 管理控制台”: Window ...

  5. jvm问题定位:cpu持续25%

    某次代码提交后审核,观察应用CPU占用持续25%, 感觉应该是某个线程写的有问题,  在linux服务器上查看cpu却是正常 windows平台线程查看工具: Process Explorer,    ...

  6. 修改ssh远程登录配置

    cp /etc/ssh/sshd_config /etc/ssh/sshd_config.ori  #更改配置前进行备份,是vim /etc/ssh/sshd_config  #编辑sshd_conf ...

  7. 【ACM】N皇后问题

    N皇后问题 #include <iostream> #include <cmath> using namespace std; ; //判断当前位置的皇后加入是否成立 bool ...

  8. 前端面试题 ---- html篇

    想要换工作了,转载自https://www.cnblogs.com/zhangshuda/p/8464772.html,感谢原博主. 一.html 1.html和xhtml区别 1. html:超文本 ...

  9. JAVA高并发秒杀API项目的学习笔记

    一步一步的搭建JAVA WEB项目,采用Maven构建,基于MYBatis+Spring+Spring MVC+Bootstrap技术的秒杀项目学习的视频:http://www.imooc.com/l ...

  10. 用spring的 InitializingBean 的 afterPropertiesSet 来初始化

    void afterPropertiesSet() throws Exception; 这个方法将在所有的属性被初始化后调用. 但是会在init前调用. 但是主要的是如果是延迟加载的话,则马上执行. ...