Leet Code 1.两数之和
给定一个整数nums和一个目标值target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。
可以假设每种输入只会对应一个答案。但是,不能重复利用这个数组中同样的元素。
题解
提交代码
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++) {
if(map.containsKey(target - nums[i])) {
return new int[] {map.get(target - nums[i]), i};
}
map.put(nums[i],i);
}
throw new IllegalArgumentException("No two sum solution");
}
}
本题最简易方法:暴力法
- 循环遍历两次,对于每个元素x遍历整个数组,判断是否存在target-x。
提交代码的思路:是用HashMap存储每个元素,每次存入元素的同时,判断是否存在target-x。
- 两个方法不同之处是用空间换时间。
- 熟悉了HashMap。
Leet Code 1.两数之和的更多相关文章
- Leet Code 2.两数相加
2.两数相加 题目描述 给出两个非空的链表用来表示两个非负的整数.其中,它们各自的位数是按照逆序的方式存储的,并且它们的每个节点只能存储一位数字.如果,我们将这两个数相加起来,则会返回一个新的链表来表 ...
- LintCode-56.两数之和
两数之和 给一个整数数组,找到两个数使得他们的和等于一个给定的数 target. 你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标.注意这里下标的范围是 1 到 n, ...
- 【LeetCode】 两数之和 twoSum
两数之和 (简单) 题目描述 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数: 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 例如: 给定 nums = [2,7,11, ...
- 给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X
题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的 ...
- LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$
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 ...
- [LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
- [LeetCode] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
随机推荐
- 在js中把json中的 key去掉双引号的方法
方法一: //数据格式是这样的: var data = '[{"id":30348079,"name":"表1","score&q ...
- Core Animation笔记(特殊图层)
1.shapeLayer: 渲染快速,内存占用小,不会被图层边界裁掉(可以在边界之外绘制),不会像素化(当做3D变化如缩放是不会失真) CGRect rect = self.containerView ...
- Oracle 限制行的子句
12c新特性 限制行的子句允许限制查询返回的行.可以指定偏移量,以及要返回的行数或百分比.您可以使用此子句实现Top-N报告.要获得一致的结果,请指定ORDER_by子句以确保具有确定性的排序顺序. ...
- 更新yum源并重建缓存
原文连接 1)下载wget yum install -y wget 2)备份默认的yum mv /etc/yum.repos.d /etc/yum.repos.d.backup 3)设置新的yum目录 ...
- Android笔记(四十八) Android中的资源访问——SDCard
访问存储在SD卡中的文件 使用 Environment.getExternalStorageState(); 判断是否存在内存卡 使用 Environment.getExternalStorageDi ...
- shell脚本:Syntax error: Bad for loop variable错误解决方法(转)
Linux Mint中写了一个简单的shell脚本,利用for..do..done结构计算1+2+3......+100的值,结果执行"sh -n xxx.sh"检测语法时总是报错 ...
- php图形图像处理技术
图形图像处理技术,gd库的强大支持,PHP的图像可以是PHP的强项,PHP图形化类库,jpgraph是一款非常好用的强大的图形处理工具. 在PHP中加载GD库 gd官方网址下载: http://www ...
- 手写一个简易版Tomcat
前言 Tomcat Write MyTomcat Tomcat是非常流行的Web Server,它还是一个满足Servlet规范的容器.那么想一想,Tomcat和我们的Web应用是什么关系? 从感性上 ...
- 一种使用gitlab的CI/CD功能实现Nginx配置更新的方法
至于nginx的docker制作,前面已介绍过. 现在使用gitlab在线编辑的方式,可实现Nginx的自定义配置并更新. .gitlab-ci.yml内容如下: variables: project ...
- Java8 Stream 流使用场景和常用操作
JAVA8内置的函数式编程接口应用场景和方式 pojo类对象和默认创建list的方法 import lombok.AllArgsConstructor; import lombok.Data; imp ...