LeeCode:两数之和【1】

题目描述

给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。

你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

题目分析

暴力解法

遍历所有数据对,判断是否等于 target,时间复杂度度 O(n^2)。

双索引对撞

先排序后,后使用双索引对撞,时间复杂度为:O(n log n) + O(n) = O(n log n), 可以试一试,也是可以 AC 的。

使用查找表

将所有元素放入查找表,之后对于每一个元素 a,查找 target - a 是否存在

Java题解

public class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer> map = new HashMap<>();
for(int i=0;i<nums.length;i++)
map.put(nums[i],i); int[] result={-1,-1};
for(int i=0;i<nums.length;i++)
{
if(map.containsKey(target-nums[i]))
{
result[0]=i;
result[1]=map.get(target-nums[i]);
if(result[0]!=result[1])
return result;
}
}
return result;
}
}

  

LeeCode:两数之和【1】的更多相关文章

  1. leecode刷题(8)-- 两数之和

    leecode刷题(8)-- 两数之和 两数之和 描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输 ...

  2. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

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

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

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

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

  5. 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 ...

  6. 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 ...

  7. [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 ...

  8. [LeetCode] 1. Two Sum 两数之和

    Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...

  9. Leetcode(一)两数之和

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

随机推荐

  1. angularjs与server交互

    真正的应用须要和真实的server进行交互,移动应用和新兴的Chrome桌面应用可能是个例外,可是对于此外的全部应用来说,不管你是想把数据持久化到云端.还是须要与其它用户进行实时交互.都须要让应用与s ...

  2. tcpdump command

    工作中一直在用tcpdump,感觉非常方便,今天心血来潮百度了一下tcpdump的用法,才发现原来还有这么多强大的功能自己都不知道,那叫一个汗啊. 以此文作为备份,记录一些新知道的用法,各位网友谁有新 ...

  3. Spring Boot(六): Favicon配置

    1.默认的Favicon Spring Boot提供了一个默认的Favicon,每次访问应用的时候都能看到,如图所示. 2.关闭Favicon 我们可以在application.properties中 ...

  4. SD--怎样增强是同一类出库单使用不同号码段

    在现实的业务中,一个公司有多个销售组织,它们使用同一个出库类型,业务往往希望它们创建的出库单的号码採用不同号码范围.但在sap里出库单号码范围是在出库单类型里设置,也就是使用同样的出库单类型,也就使用 ...

  5. 前言和第一章.NET的体系结构

    前言 COM:组件对象模型(Component Object Model COM)源自对象链接和嵌入(Object Linking and Embedding )OLE. DCOM:(Distribu ...

  6. CAP原则 和BASE

    CAP原则又称CAP定理,指的是在一个分布式系统中,Consistency(一致性). Availability(可用性).Partition tolerance(分区容错性),三者不可得兼 [1]  ...

  7. u-boot README--Memory Management&initialize

    Memory Management:------------------ U-Boot runs in system state and uses physical addresses, i.e. t ...

  8. 配置Nginx防止直接用IP訪问Webserver

    看了非常多Nginx的配置,好像都忽略了ip直接訪问Web的问题.这样理论上不利于SEO优化,所以我们希望能够避免直接用IP訪问站点.而是域名訪问.详细怎么做呢.看以下. 官方文档中提供的方法: If ...

  9. GreenDao学习

    官方文档地址:http://greenrobot.org/greendao/documentation//introduction/ 英语不好的可以使用谷歌翻译,很轻松的看懂文档,不需要FQ. 看不懂 ...

  10. git 撤销已经push到远端的代码

    其实是没有直接让远端代码回复到某次的指令,实现撤销push的思路如下: 1.先让代码恢复到想要恢复的前一次提交记录 2.重新提交代码,覆盖端上的代码,就相当于撤销了push 的提交 实现方式如下: 1 ...