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. unity, 用脚本创建mesh

    创建一个空gameObject,添加Mesh Filter和Mesh Renderer两个component,再添加一个脚本createMeshScript: using UnityEngine;us ...

  2. (Spring Boot框架)快速入门

    Spring Boot 系列文章推荐 Spring Boot 入门 Spring Boot 属性配置和使用 Spring Boot 集成MyBatis Spring Boot 静态资源处理 今天介绍一 ...

  3. MySQL错误代码大全(史上最全)

    用任何主机语言调用MySQL时可能出现的错误.首先,列出了服务器错误消息.其次列出了客户端程序消息. B.1. 服务器错误代码和消息  服务器错误信息来自下述源文件: · 错误消息信息列在share/ ...

  4. vuex使用 实现点击按钮进行加减

    //store.js /** * vuex配置 */ import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex); //定义属性(数据) v ...

  5. 80X86寄存器介绍

    80X86寄存器介绍  32位CPU所含有的寄存器有: 4个数据寄存器(EAX.EBX.ECX和EDX)2个变址和指针寄存器(ESI和EDI) 2个指针寄存器(ESP和EBP)6个段寄存器(ES.CS ...

  6. windows下wim配置成IDE

    1.配置文件_wimrc set fileencodings=utf-,ucs-bom,cp936,big5 set fileencoding=utf- source $VIMRUNTIME/vimr ...

  7. Servlet请求转发RequestDispatcher接口

    在Servlet中,利用RequestDispatcher对象,可以将请求转发给另外一个Servlet或JSP页面,甚至是HTML页面,来处理对请求的响应. 一,RequestDispatcher接口 ...

  8. CMake Error at cmake/boost.cmake:76 (MESSAGE):

    编译mysql5.7.9的时候报错 CMake Error at cmake/boost.cmake:76 (MESSAGE): You can download it with -DDOWNLOAD ...

  9. iis 导入和导出配置——iis管理

    首先我们打开服务器管理器,一般服务器都在左下角的任务栏中,直接点击即可打开 2 打开WEB服务器(IIS),选择IIS根目录,找到右边的共享管理 3 打开共享管理后,我们在右侧的操作中找到导出配置,选 ...

  10. EasyUI 表单 tree

    第一步:创建HTML标记 <divid="dlg"style="padding:20px;">     <h2>Account Info ...