LeetCode1---两数之和
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---两数之和的更多相关文章
- Leetcode1——两数之和 详细解析
Leetcode1--两数之和 题目分类:数组.哈希表的应用 1. 题目描述 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数 ...
- LeetCode1——两数之和
最近在家拧三阶魔方,把初级的玩法掌握了,也就是可以还原六个面了,速度不快,但是也很兴奋.三阶魔方的初级玩法按照套路拧就可以了,每一步需要完成的任务,该步骤转动的方法基本都是固定的,而且变化也并不是特别 ...
- Leetcode-1.两数之和
题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数 ...
- [Swift]LeetCode1 .两数之和 | Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- LeetCode1.两数之和 JavaScript
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 实例: 给定 nums = [2, 7, 11, 15], target ...
- Leetcode1.两数之和——简洁易懂
> 简洁易懂讲清原理,讲不清你来打我~ 输入一个数组和一个整数,从数组中找到两个元素和为这个整数,输出下标$
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- 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 ...
- 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 ...
随机推荐
- 【POJ - 1458】Common Subsequence(动态规划)
Common Subsequence Descriptions: A subsequence of a given sequence is the given sequence with some e ...
- Six degrees of Kevin Bacon
转自:https://blog.csdn.net/a17865569022/article/details/78766867 Input* Line 1: Two space-separated in ...
- WPF-DataGrid(数据表格)美化
我们不多哔哔先上图: 数据表格使用背景: 当我们在做二次开发发现我我们的表格无法向WEB的表格一样好看,这时我们就需要对数据表格进行美化和重构 表格美化思维引导: WPF数据表格是由表头和表体(内容) ...
- idea 启动时报 error:java 无效的源发行版11
编译的版本不符合,需要修改统一
- [Usaco2003 Open]Lost Cows
Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular di ...
- 图论/暴力 Codeforces Beta Round #94 (Div. 2 Only) B. Students and Shoelaces
题目传送门 /* 图论/暴力:这是个连通的问题,每一次把所有度数为1的砍掉,把连接的点再砍掉,总之很神奇,不懂:) */ #include <cstdio> #include <cs ...
- 题解报告:hdu 1576 A/B(exgcd、乘法逆元+整数快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n ...
- bootstrap框架栅格系统使用
使用的前端框架 bootstrap框架 Bootstrap是一个响应式的框架 我们在使用的时候主要使用的是它的网格系统, 1.bootstrap布局 布局容器:.container(用于固定宽度并支 ...
- 原生开发之css样式问题(持续更新)
·移动端开发将div高度设置为设备高度 div{ Height:100vh; } · select选择器文字设置: /*select文字右对齐*/ select{ direction: rtl; } ...
- ASP.NET控件的ID,ClientID,UniqueId的区别
一般情况下三者相同(没有父控件) ID:获取或设置分配给服务器控件的编程标识符.分配给控件的编程标识符. (可写) 设置服务器控件上的此属性可提供对服务器控件的属性.事件和方法的编程访问.Web 开发 ...