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. LVS 介绍

    LVS 介绍 说明: LVS是Linux Virtual Server的简称 LVS是一个实现负载均衡的开源软件项目 LVS效率要高于Nginx LVS工作在ISO的第4层(传输层) LVS架构有三层 ...

  2. pearson相关分析在R中的实现

    三个相关性函数: cor():R自带的,输入数据可以是vector,matrix,data.frame,输出两两的相关系数R值 cor.test():R自带的,输入数据只能是两个vector,输出两个 ...

  3. 20145240《Java程序设计》第七周学习总结

    20145240<Java程序设计>第七周学习总结 教材学习内容总结 12.1认识Lambda语法 12.1.1Lambda语法概览 在java中引入了Lambda的同时,与现有API维持 ...

  4. 基于Visual c++ 2012的php扩展开发 - 环境搭建

    软件准备 Apache2.4 php-5.6.20-Win32-VC11-x86 php-5.6.20-src mysql-5.5.45-win32 vcredist_x86.exe vs2012旗舰 ...

  5. Unity Json 之三

    今天在网上看到一个simplejson,直接调用这两个API就可以了,简单易用 string jsonstr = SimpleJson.SimpleJson.SerializeObject(json) ...

  6. uiwebview 加载本地js、css、img,html从网站加载

    资源文件都是放在根目录下 1.index.html <html> <head> <title>My test Page</title> <link ...

  7. keepalived检测脚本及注意事项

    keepalived检测脚本的作用及注意事项: 默认每隔3秒钟执行一次检测脚本,检查nginx服务是否启动,如果没启动就把nginx服务启动起来,如果启动不成功,就把keepalived服务down掉 ...

  8. 设置本地wamp环境挂载多站点同时运行

    之前写过一篇关于在Linux环境下配置虚拟主机的文章:现在又有一种场景:在本地同时写多个项目:可本地的wamp环境下默认只有一个www目录:这样经常修改目录很痛苦:那么幸福就这么猝不及防的来了:下面就 ...

  9. div css 练习1

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. Spring初学之bean的生命周期

    实体Bean: Car.java: package spring.beans.cycle; public class Car { private String name; private int pr ...