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

You may assume that each input would have exactly one solution, and you may not use the same element twice.

英文不好,简单翻译一下,能自己翻译的跳过,看不懂我的翻译自行结合翻译网站。

给定一个整数数组,返回两个数相加等于目标值的两个数的下标值。

您可以假设每个输入都有一个解决方案,您可能不会使用相同的元素两次。

Example:

Given nums = [, , , ], target = ,

Because nums[] + nums[] =  +  = ,
return [, ].

这道题最简单的解决方法当然是双循环找到符合要求的结果。

     public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
return new int[]{nums[i], nums[j]};
}
}
}
return null;
}

首先这样写在LeetCode上可以通过,刚才试了一下,但是这个题在笔试中出现的话,应该是这个公司不想招人或者是不想通过笔试招人。

如果是在面试中出现,面试官肯定让你写出时间复杂度更低的代码,因为这个代码的时间复杂度O(n²)。下面介绍另一种解法:

public int[] twoSum(int[] nums, int target) {
if (nums == null) {
return null;
}
HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
if (hashMap.containsKey(nums[i])) {//当map中有值需要当前值时,说明找到结果。
return new int[]{hashMap.get(nums[i]), i};
}
hashMap.put(target - nums[i], i);
//存入当前值所需要的数值,比如target为5,当前值为1,他需要4,才能得到目标值。
}
return null;
}

以空间换时间HashMap底层是hash表,查找的时间复杂度为O(1),所以上述代码的时间复杂度为O(n),额外空间复杂度为O(n)。以空间换时间。

  

LeetCode第一题:Two Sum的更多相关文章

  1. leetcode第一题--two sum

    Problem:Given an array of integers, find two numbers such that they add up to a specific target numb ...

  2. LeetCode第一题—— Two Sum(寻找两数,要求和为target)

    题目描述: Given an array of integers, return indices of the two numbers such that they add up to a speci ...

  3. LeetCode算法题-Two Sum II - Input array is sorted

    这是悦乐书的第179次更新,第181篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第38题(顺位题号是167).给定已按升序排序的整数数组,找到两个数字,使它们相加到特定 ...

  4. 乘风破浪:LeetCode真题_040_Combination Sum II

    乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...

  5. 乘风破浪:LeetCode真题_039_Combination Sum

    乘风破浪:LeetCode真题_039_Combination Sum 一.前言     这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...

  6. LeetCode算法题-Two Sum IV - Input is a BST(Java实现)

    这是悦乐书的第280次更新,第296篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第148题(顺位题号是653).给定二进制搜索树和目标数,如果BST中存在两个元素,使得 ...

  7. LeetCode算法题-Path Sum III(Java实现)

    这是悦乐书的第227次更新 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第94题(顺位题号是437).您将获得一个二叉树,其中每个节点都包含一个整数值.找到与给定值相加的路径数 ...

  8. LeetCode算法题-Range Sum Query Immutable(Java实现)

    这是悦乐书的第204次更新,第214篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第70题(顺位题号是303).给定整数数组nums,找到索引i和j(i≤j)之间的元素之 ...

  9. LeetCode算法题-Path Sum(Java实现)

    这是悦乐书的第169次更新,第171篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第28题(顺位题号是112).给定二叉树和整数sum,确定树是否具有根到叶路径,使得沿路 ...

  10. leetcode第一题(easy)

    第一题:题目内容 Given an array of integers, return indices of the two numbers such that they add up to a sp ...

随机推荐

  1. h => h(App)解析

    在创建Vue实例时经常看见render: h => h(App)的语句,现做出如下解析: h即为createElement,将h作为createElement的别名是Vue生态系统的通用管理,也 ...

  2. 在IOS开发中,项目的目录结构如何搭建?

    网上有很多关于IOS开发的学习资料.然而却很少有关于设计一个项目时,如何设计其目录结构?这对于自学IOS的程序猿们,无疑有诸多不利.接下来,我就简单的谈下真正在公司中,项目的目录结构如何搭建: 以上为 ...

  3. 【转】nodejs mysql 链接数据库集群

    1.建立数据库连接:createConnection(Object)方法      该方法接受一个对象作为参数,该对象有四个常用的属性host,user,password,database.与php中 ...

  4. Centos Crontab查看状态和开启

    # service crond status crond is stopped # service crond start Starting crond # service crond status ...

  5. MySQL数据copy

    摘自http://database.51cto.com/art/201011/234776.htm 1. 下面这个语句会拷贝表结构到新表newadmin中. (不会拷贝表中的数据) CREATE TA ...

  6. jar包作用

    hibernate中jar包的作用 (1)hibernate3.jar:Hibernate的核心库,没有什么可说的,必须使用的jar包 (2)cglib-asm.jar:CGLIB库,Hibernat ...

  7. sklearn.preprocessing.StandardScaler 离线使用 不使用pickle如何做

    Having said that, you can query sklearn.preprocessing.StandardScaler for the fit parameters: scale_ ...

  8. mac上获取手机的uuid

    把手机连上mac 终端中输入: system_profiler SPUSBDataType | grep "Serial Number:.*" 修改用 | sed s#" ...

  9. Mybatis 接口方式对数据的增删改查 一对一关联查询

    数据库中有两个表 student 和studentInfo student表中的字段和数据 studentInfo表中的字段 ok数据库说完了,开始建立一个项目,对数据库中的数据进行操作吧 新建jav ...

  10. Django 基础 ORM系统

    Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据 ...