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] java代码:
 public class Solution {
public int[] twoSum(int[] nums, int target) {
for(int i=0;i<nums.length;i++){
for(int j=1;j<nums.length;j++){
if(target==nums[i]+nums[j]){
return new int[]{i,j};
}
}
}
throw new RuntimeException("No such this indices");
}
}
 public class Solution {
public int[] twoSum(int[] numbers, int target) {
int[] result = new int[2];
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < numbers.length; i++) {
if (map.containsKey(target - numbers[i])) {
result[1] = i;
result[0] = map.get(target - numbers[i]);
return result;
}
map.put(numbers[i], i + 1);
}
return result;
}
}
 public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i);
}
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement) && map.get(complement) != i) {
return new int[] { i, map.get(complement) };
}
}
throw new IllegalArgumentException("No two sum solution");
}

toSum的更多相关文章

  1. 项目杂记(MONTHS_BETWEEN,Having ,Spool)

    1,oracle中计算年龄: select FLOOR(MONTHS_BETWEEN(SYSDATE, to_date('20130728', 'yyyymmdd')) / 12), trunc(mo ...

  2. HDU4514(非连通图的环判断与图中最长链)

    题目:设计风景线 题意:给定一个无向图,图可能是非连通的,如果图中存在环,就输出YES,否则就输出图中最长链的长度. 分析:首先我们得考虑这是一个无向图,而且有可能是非连通的,那么就不能直接像求树那样 ...

  3. 2014联合三所学校 (HDU 4888 HDU 4891 HDU 4893)

    HDU 4891 The Great Pan 注册标题  他怎么说,你怎么样  需要注意的是乘法时,它会爆炸int 代码: #include<iostream> #include<c ...

  4. C语言-指针、数组、结构体、分支、循环混合使用

    1.写一个程序,输出如下内容: //############################################################# //### name number ma ...

  5. 准备:新V8即将到来,Node.js的性能正在改变

    V8的Turbofan的性能特点将如何对我们优化的方式产生影响 审阅:来自V8团队的Franziska Hinkelmann和Benedikt Meurer. **更新:Node.js 8.3.0已经 ...

  6. 【LSGDOJ 1351】关灯

    题目描述 多瑞卡得到了一份有趣而高薪的工作.每天早晨他必须关掉他所在村庄的街灯.所有的街灯都被设置在一条直路的同一侧. 多瑞卡每晚到早晨 5 点钟都在晚会上,然后他开始关灯.开始时,他站在某一盏路灯的 ...

  7. hdu 1556 涂气球 线段树(区间更新~对区间[x,y]更新,求任意节点被更新的次数)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. Data Structure Binary Tree: Convert a given tree to its Sum Tree

    http://www.geeksforgeeks.org/convert-a-given-tree-to-sum-tree/ #include <iostream> #include &l ...

  9. [LeetCode] 805. Split Array With Same Average 用相同均值拆分数组

    In a given integer array A, we must move every element of A to either list B or list C. (B and C ini ...

随机推荐

  1. 《Mongo权威指南》学习手记

    1.ObjectId: 是“_id”的默认类型.mongo没有用自增主键原因:多个服务器同步自动增加主键值费时费力. mongo初衷是作分布式数据库,所以能在分片环境中生成唯一的标示符非常重要. Ob ...

  2. Sql 语句常语法

    以前感觉在这个方面很欠缺,于是就找了些这方面的材料,自己也做了些总结,汇总到了一块.便于以后的查阅. --1.获取表的主键字段SELECT name FROM SysColumns WHERE id= ...

  3. LED Decorative Light Supplier - LED Neon Application: 5 Advantages

    In the past 100 years, lighting has gone a long way. LED decorative lighting is now designed to meet ...

  4. vector 牛逼 +lower_bound+ upper_bound

    vector 超级 日白 解决的问题空间问题,可以自由伸缩. 一下用法: 向量大小: vec.size(); 向量判空: vec.empty(); 末尾添加元素: vec.push_back(); / ...

  5. Linux 开启orcale服务

    su - oracle  //切换到oracle用户模式下 sqlplus /nolog  //登录sqlplus connect /as sysdba; //连接orcale startup;   ...

  6. SSL扫描工具

    工具: sslciphercheck sslscan sslciphercheck.exe -h ip -p 443 有些IP会报错:

  7. python3练习100题——021

    题目很容易,只要理清了数学思想就可以解出来,所以本来不是很喜欢这种题. 后来看到有大神用递归解,觉得还是很值得学习的. 原题链接:http://www.runoob.com/python/python ...

  8. Hibernate的基本工作原理

    Hibernate开发过程中会用到5个核心接口: 1.Configuration2.SessionFactory3.Session4.Transaction5.QueryHibernate就是通过这些 ...

  9. JFrog推出全球首个支持混合云架构,端到端的通用DevOps平台 ——JFrog Platform

            JFrog Platform,基于屡获殊荣的JFrog Artifactory制品仓库的独特能力,通过多合一的体验提供DevSecOps.CI / CD和软件分发的解决方案. 2020 ...

  10. Apache Kafka(二)- Kakfa 安装与启动

    安装并启动Kafka 1.下载最新版Kafka(当前为kafka_2.12-2.3.0)并解压: > wget http://mirror.bit.edu.cn/apache/kafka/2.3 ...