import java.util.Arrays;
import java.util.HashMap;
import java.util.Map; /**
*功能描述 :两数之和
* @author lkr
* @date 2019/3/5
*/
public class Solution1 {
//暴力法
public static int[] twoSum(int[] nums, int target) {
int[] arr = new int[2];
for(int i=0;i<nums.length;i++){
for(int j=i+1;j<nums.length;j++){
int sum = nums[i] + nums[j];
if(target == sum){
arr[0] = i;
arr[1] = j;
}
}
}
return arr;
} //一遍哈希表
public static int [] oneHashtable(int[] nums,int target){
Map<Integer,Integer> map = new HashMap();
for (int i=0;i<nums.length;i++){
int value = target - nums[i];
if(map.containsKey(value)){
/*int[] arr = {map.get(value),i};
return arr;*/
return new int[] {map.get(value),i};//为什么map.get(value)在前面?
}
map.put(nums[i],i);
}
throw new IllegalArgumentException("NO TWO SUM SOLUTION");
} public static void main(String[] args){
int [] nums = {2,7,11,15,9};
int target = 20;
//int [] arr = twoSum(nums,target);
int [] arr = oneHashtable(nums,target);
System.out.println(Arrays.toString(arr));
}
}

LeetCode1---两数之和的更多相关文章

  1. Leetcode1——两数之和 详细解析

    Leetcode1--两数之和 题目分类:数组.哈希表的应用 1. 题目描述 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数 ...

  2. LeetCode1——两数之和

    最近在家拧三阶魔方,把初级的玩法掌握了,也就是可以还原六个面了,速度不快,但是也很兴奋.三阶魔方的初级玩法按照套路拧就可以了,每一步需要完成的任务,该步骤转动的方法基本都是固定的,而且变化也并不是特别 ...

  3. Leetcode-1.两数之和

    题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数 ...

  4. [Swift]LeetCode1 .两数之和 | Two Sum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  5. LeetCode1.两数之和 JavaScript

    给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 实例: 给定 nums = [2, 7, 11, 15], target ...

  6. Leetcode1.两数之和——简洁易懂

    > 简洁易懂讲清原理,讲不清你来打我~ 输入一个数组和一个整数,从数组中找到两个元素和为这个整数,输出下标![在这里插入图片描述](https://img-blog.csdnimg.cn/img ...

  7. 给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X

    题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的 ...

  8. LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  9. LeetCode 371. Sum of Two Integers (两数之和)

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  10. LeetCode 167. Two Sum II - Input array is sorted (两数之和之二 - 输入的是有序数组)

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

随机推荐

  1. 网站SEO优化如何让百度搜索引擎绝的你的网站更有抓取和收录价值呢?_孙森SEO

    今天孙森SEO为大家唠唠网站到底该如何优化才会让百度搜索引擎绝的你的网站更有抓取和收录价值呢? 第一方面:网站创造高品质的内容,可以为用户提供独特的价值. 1.百度作为搜索引擎,网站内容必须满足 搜索 ...

  2. 在Linux下使用linuxdeployqt发布Qt程序

    一.简介 linuxdeployqt 是Linux下的qt打包工具,可以将应用程序使用的资源(如库,图形和插件)复制到二进制运行文件所在的文件夹中. 二.安装linuxdeployqt 去github ...

  3. 条件DP UVA 672 Gangsters

    题目传送门 题意:n个歹徒进饭店,可变化宽度的门,范围[0, k],每个歹徒进门在ti时间进门,身材si,进去后有pi的成功值,问最大的成功值 分析:首先按照进门时间排序,dp[i][j] 表示第i个 ...

  4. VS打包后生成快捷方式:目标指向错误、Icon图标分辨率有误问题解决方案

    1.目标指向错误: 在安装***.msi文件后,对快捷方式-->右键-->属性: 发现目标并非指exe文件. 于是我新建了一个快捷方式,将目标-->指向exe文件,位置Ctrl+v. ...

  5. C#控件置于底层或顶层

    btn.BringToFront();//置于顶层 btn.SendToBack();//置于底层

  6. IO流的原理和概念

    在程序中如何读写文件?不同的编程语言有不同的方式,而 JAVA 则提出了“流”的概念,通过“流”来读写文件 什么是流: 流(Stream)是指一连串的数据(字符或字节),是以先进先出的方式发送信息的通 ...

  7. poj2718 Smallest Difference

    思路: 暴力乱搞. 实现: #include <iostream> #include <cstdio> #include <sstream> #include &l ...

  8. core\stm32f10x.h(244): error: #67: expected a "}"

  9. YOLO模型对图片中车辆的识别比对

    1,模型对比结果 ²        标准Yolo v3模型 ²        标准Yolo v3 tiny模型 ²        标准Yolo v2 tiny模型 ²        用户训练yolo ...

  10. mysql执行语句汇总

    插入select的数据 INSERT INTO `test1`( order_id, goods_id, goods_name, goods_sn, product_id, goods_number, ...