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. SQLServer导出查询结果带表头(标题行)

    SQLServer导出查询结果带表头(标题行) 平时我们经常会需要将SQLSERVER查询的结果复制到EXCEL文档中进行分析处理,但是有一件事很头痛,就是复制结果网格的数据到EXCEL之后,都是没有 ...

  2. FireFox浏览器的about:config参数大全及其具体用途介绍

    FireFox浏览器的about:config参数大全及其具体用途介绍,注意:这还远不是所有的about:config参数,由于设置参数太多,官方也只提供英文版本的说明,这里提供的FireFox ab ...

  3. Apache 安装概要

    1.apache下载参照百度 bin文件夹下命令行:  httpd -k install 2.安装完成后排错记录 服务无法启动,到bin目录下运行  httpd.exe  查看输出,然后百度一下输出即 ...

  4. Big research problems (1)

    1. how to measure the uncertainty of prediction model or data analysis? 2.

  5. 终极教程【zhong】

    just for a better future! 资源教程               aiim                   综合类 前端知识体系 前端知识结构 Web前端开发大系概览 We ...

  6. 常见python面试题

    1,简述列举了解的编程语言及语言间的区别? Python 解释型语言,代码简洁,易懂 C语言 编译型语言,底层语言 c++ 编译型语言,在C语言基础上加了面向对象 Java 混合型语言,可拓展性高 G ...

  7. STM32F103之定时器学习记录

    /==============翻译STM32F103开发手册定时器部分========================/ 14 高级控制计时器(TIM1和TIM8) 14.1 TIM1和TIM8介绍 ...

  8. bzoj 4827: [HNOI2017]礼物 (FFT)

    一道FFT 然而据说暴力可以水70分 然而我省选的时候看到了直接吓傻了  连暴力都没打 太弱了啊QAQ emmmm 详细的拆开就看其他题解吧233 最后那一步卷积其实我一直没明白 后来画画图终于懂了 ...

  9. 题解【洛谷P1514】[NOIP2010]引水入城

    题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个 \(N\) 行 \(M\) 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城市 ...

  10. 总结下抽象类Abstract和虚方法Virtual(易混点)

    1.普通类中可以有普通方法和虚方法,在被继承时,虚方法可以选择重写(override)或者不重写 2.抽象类中可以有普通方法和虚方法和抽象方法,当被继承时 如下 1.普通方法不能被子类重写 2.虚方法 ...