package leetcode;

import java.util.HashMap;
import java.util.Map; /**
* @author mercy
*Example:
*Given nums = [2, 7, 11, 15], target = 9,
*Because nums[0] + nums[1] = 2 + 7 = 9,
*return [0, 1].
*/
public class TwoSum {
public static void main(String[] args) {
int[] nums={2,0,4,9,5,7,10,9};
int target=12;
int[] arr=twoSum1(nums,target);
System.out.println(arr[0]+"--"+arr[1]);
}
public static 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 other=target-nums[i];
if(map.containsKey(other)&&map.get(other)!=i){
return new int[] { i, map.get(other) };
}
}
throw new IllegalArgumentException("No two sum solution");
}
/**
* @param nums
* @param target
* @return
* 用Map方法
* @author mercy
*/
public static int[] twoSum1(int[] nums, int target) {
Map<Integer,Integer> map=new HashMap<>();
for(int i=0;i<nums.length;i++){
int other=target-nums[i];
if(map.containsKey(other)){
return new int[]{map.get(other),i};
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("No two sum solution");
} /**
* @param nums
* @param target
* @return
* 传统的方法
* @author mercy
*/
public static int[] twoSum2(int[] nums, int target) {
for(int i=0;i<nums.length;i++){
for(int j=i+1;j<nums.length;j++){
if(nums[j]==target-nums[i]){
return new int[] {i,j};
}
}
}
throw new IllegalArgumentException("No two sum solution");
} }

1:TwoSum(如果两个和为某个数,找出这俩数的位置)的更多相关文章

  1. 剑指offer40:一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字

    1 题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 2 思路和方法 (1)异或:除了有两个数字只出现了一次,其他数字都出现了两次.异或运算中,任 ...

  2. 两个有序数组长度分别为m,n,最多m+n次查找找出相同的数

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. 两个大数组foreach,找出相同的key数量,所用的时间对比

    <?php function microtime_float() { list($usec, $sec) = explode(" ", microtime()); retur ...

  4. 九度OJ 1256:找出两个只出现了一次的数字 (位运算)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:568 解决:186 题目描述: 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 输入: 输入的 ...

  5. 九度oj 题目1256:找出两个只出现了一次的数字

    题目描述: 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 输入: 输入的第一行包括一个整数N(1<=N<=1000). 接下来的一行包括N个 ...

  6. 2018.3.12 Leecode习题 给定一个整数数列,找出其中和为特定值的那两个数。

    给定一个整数数列,找出其中和为特定值的那两个数. 你可以假设每个输入都只会有一种答案,同样的元素不能被重用. 示例: 给定 nums = [2, 7, 11, 15], target = 9; 因为 ...

  7. js-FCC算法Smallest Common Multiple。找出两个参数和它们之间的连续数字的最小公倍数。

    存档. 找出能被两个给定参数和它们之间的连续数字整除的最小公倍数. function smallestCommons(arr) { //分解质因数法,分解为若干个质数相乘 var arrratio=[ ...

  8. FZU みねちゃんの修罗场(从一堆出现三次的数中找出出现两次的数)

    みねちゃんの修罗场 Time Limit: 5000 mSec     Memory Limit: 1024 KB Description みねちゃん是个成绩优秀大学二年级学生,本来是和像自己妹妹一般 ...

  9. Java - Collection 高效的找出两个List中的不同元素

    如题:有List<String> list1和List<String> list2,两个集合各有上万个元素,怎样取出两个集合中不同的元素? 方法1:遍历两个集合 public ...

随机推荐

  1. ocr 识别 github 源码

    参考 [1] https://github.com/eragonruan/text-detection-ctpn [2] https://github.com/senlinuc/caffe_ocr [ ...

  2. js ~取非运算符的妙用,将-1转为0(或假值)

    典型的运用场景就是indexOf

  3. DDMS android 开发工具-----dump View Hierarchy for UI automator

    今天又发现一个好工具  dump View Hierarchy 对学习UI布局非常有优点,操作也非常easy的.直接上图说话了 watermark/2/text/aHR0cDovL2Jsb2cuY3N ...

  4. Linux下解压tar.xz

    tar xvJf  ***.tar.xz 注意零散文件,最好放到文件夹里

  5. 通过CLR API实现C++调用C#代码交互

    对于某些跨平台程序,这也就意味着只能在windows下使用了 不过最近.Net开源或许以后可以跨平台 之前花了一些时间研究COM方式调用,太繁琐不推荐. COM方式调用总结 后来尝试使用CLR C++ ...

  6. Atitit. 软件---多媒体区---- jmf 2.1.1 Java Media Framework 支持的格式

    Atitit. 软件---多媒体区---- jmf 2.1.1 Java Media Framework 支持的格式 JMF,全名为Java Media Framework,它可以在java appl ...

  7. 微软七届MVP桂素伟:移动互联网与职业规划

    原文地址:http://student.csdn.net/mcd/topic/163587/955481 2014年10月19日在哈尔滨工业大学举办了CSDN高校俱乐部全国巡讲. 此次邀请到了微软七届 ...

  8. JS四种方法去除字符串最后的逗号

    <script> window.onload=function() { var obj = {name: "xxx", age: 30, sex: "fema ...

  9. DOM节点的三个属性

    在文档对象模型 (DOM) 中,每个节点都是一个对象.DOM 节点有三个重要的属性 : 1. nodeName : 节点的名称 2. nodeValue :节点的值 3. nodeType :节点的类 ...

  10. CSS学习笔记(9)--详解CSS中:nth-child的用法

    详解CSS中:nth-child的用法 前端的哥们想必都接触过css中一个神奇的玩意,可以轻松选取你想要的标签并给与修改添加样式,是不是很给力,它就是“:nth-child”. 下面我将用几个典型的实 ...