*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 ...
随机推荐
- Java的观察者
class Teacher extends Observable { public void startLesson() { System.out.println(String.format(&quo ...
- Java8 流的使用示例
foreach遍历处理 dataList.stream().forEach(index -> sb.append(dataList.get(index) + "',")); ...
- MetricStatTimer
package org.apache.storm.metric.internal; import java.util.Timer; /** * Just holds a singleton metri ...
- WIN2008R2 asp.net core的配置
配置IIS Windows Server上通过“添加角色和功能”,桌面Windows上通过“启用和关闭Windows功能”来安装和配置IIS.确保勾选Web服务和“IIS 管理控制台”: Window ...
- jvm问题定位:cpu持续25%
某次代码提交后审核,观察应用CPU占用持续25%, 感觉应该是某个线程写的有问题, 在linux服务器上查看cpu却是正常 windows平台线程查看工具: Process Explorer, ...
- 修改ssh远程登录配置
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.ori #更改配置前进行备份,是vim /etc/ssh/sshd_config #编辑sshd_conf ...
- 【ACM】N皇后问题
N皇后问题 #include <iostream> #include <cmath> using namespace std; ; //判断当前位置的皇后加入是否成立 bool ...
- 前端面试题 ---- html篇
想要换工作了,转载自https://www.cnblogs.com/zhangshuda/p/8464772.html,感谢原博主. 一.html 1.html和xhtml区别 1. html:超文本 ...
- JAVA高并发秒杀API项目的学习笔记
一步一步的搭建JAVA WEB项目,采用Maven构建,基于MYBatis+Spring+Spring MVC+Bootstrap技术的秒杀项目学习的视频:http://www.imooc.com/l ...
- 用spring的 InitializingBean 的 afterPropertiesSet 来初始化
void afterPropertiesSet() throws Exception; 这个方法将在所有的属性被初始化后调用. 但是会在init前调用. 但是主要的是如果是延迟加载的话,则马上执行. ...