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. <<= 什么意思?|=什么意思?

    <<= 什么意思?|=什么意思?   x <<= 2; // x = x << 2; y |= 8; // y = y | 8;   这些都是被缩写了!位运算-- ...

  2. SVN遇到Can't convert string from 'UTF-8' to native encoding(转)

    svn: Can't convert string from 'UTF-8' to native encoding: svn: platform/console-framework/portal/im ...

  3. ABP框架EF6链接Oracle数据库手动迁移

    环境:VS2017 + ABP官方模板(不含Zero) +Oracle 11Gx64DB  + Oracle 11Gx32Client(PLSQL工具访问) 一.Abp项目的下载以及运行 1.创建ab ...

  4. spring异常 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderServlet

    spring异常 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderServlet   情况 ...

  5. 《STL源代码剖析》学习笔记系列之七、八——仿函数和配接器

    1. 仿函数 仿函数又名函数对象.具有函数性质的对象.就是传入一些參数.然后对參数进行某些运算,然后返回一个值. 为了可以使行为类似函数,须要在类别定义中必须自己定义function call 运算子 ...

  6. 在多点环境下使用cas实现单点登陆及登出

    CAS 介绍 CAS 是 Yale 大学发起的一个开源项目,旨在为 Web 应用系统提供一种可靠的单点登录方法,CAS 在 2004 年 12 月正式成为 JA-SIG 的一个项目.CAS 具有以下特 ...

  7. Python鸡汤

    标准库 很正确 外部库 有一些风险,可能有bug,可能文档不全,可能长时间未更新. ipython 1 pip 这应该是安装Python后第一个需要的命令 pip install -i -i, --i ...

  8. cobbler pxe-menu

    对应的文件在 /var/lib/tftpboot/pxelinux.cfg下 如果profile的pxe-menu设置为1的话,就可以默认显示在menu上了.可以手动选择要下发哪一个profile. ...

  9. ios NavigationViewController跳转以及返回传值

    (一)使用NavigationViewController进行页面跳转时,应该使用pushViewController方法来跳转至下一页面.这种话.下一页面相同在NavigationViewContr ...

  10. ASP.NET动态网站制作(23)-- ADO.NET(2)

    前言:这节课老师请高级班的E老师过来代课,还是接着老师讲的内容继续深入,修改了上节课老师写的部分代码. 内容: 1.数据库本质就是一个软件,这个软件帮助我们把数据有序地存储起来,当我们需要数据的时候帮 ...