LeetCode第一题:Two Sum
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的更多相关文章
- leetcode第一题--two sum
Problem:Given an array of integers, find two numbers such that they add up to a specific target numb ...
- LeetCode第一题—— Two Sum(寻找两数,要求和为target)
题目描述: Given an array of integers, return indices of the two numbers such that they add up to a speci ...
- LeetCode算法题-Two Sum II - Input array is sorted
这是悦乐书的第179次更新,第181篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第38题(顺位题号是167).给定已按升序排序的整数数组,找到两个数字,使它们相加到特定 ...
- 乘风破浪:LeetCode真题_040_Combination Sum II
乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...
- 乘风破浪:LeetCode真题_039_Combination Sum
乘风破浪:LeetCode真题_039_Combination Sum 一.前言 这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...
- LeetCode算法题-Two Sum IV - Input is a BST(Java实现)
这是悦乐书的第280次更新,第296篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第148题(顺位题号是653).给定二进制搜索树和目标数,如果BST中存在两个元素,使得 ...
- LeetCode算法题-Path Sum III(Java实现)
这是悦乐书的第227次更新 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第94题(顺位题号是437).您将获得一个二叉树,其中每个节点都包含一个整数值.找到与给定值相加的路径数 ...
- LeetCode算法题-Range Sum Query Immutable(Java实现)
这是悦乐书的第204次更新,第214篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第70题(顺位题号是303).给定整数数组nums,找到索引i和j(i≤j)之间的元素之 ...
- LeetCode算法题-Path Sum(Java实现)
这是悦乐书的第169次更新,第171篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第28题(顺位题号是112).给定二叉树和整数sum,确定树是否具有根到叶路径,使得沿路 ...
- leetcode第一题(easy)
第一题:题目内容 Given an array of integers, return indices of the two numbers such that they add up to a sp ...
随机推荐
- 【Head First Servlets and JSP】笔记7:如何创建一个全局的dog?
重定向与请求分派 “局部”参数——ServletConfig——servlet初始化参数 “全局”参数——ServletContext——上下文初始化参数 Web app的“构造器”——Servlet ...
- 0801 RESTAPI设计,DRF 序列化
1.内容回顾 1.restframework serializer(序列化)的简单使用 QuereySet([obj,obj,obj]) --> JSON ...
- CentOS 7卸载mariadb安装mysql
CentOS 7已经将默认集成mariadb而不是mysql,这对于多数还是依赖于mysql的应用来说,需要手动的进行更新. 可能会遇到这样错误,换成MySQL就好了. error 2002 (hy0 ...
- 20145240 《Java程序设计》第八周学习总结
20145240 <Java程序设计>第八周学习总结 教材学习内容总结 15.1日志 15.1.1日志API简介 java.util.logging包提供了日志功能相关类与接口,不必额外配 ...
- win7 与 Ubuntu 16.04 文件传送
win7 与 Ubuntu 16.04 文件传送 环境:主机系统为win7,虚拟机为vmware12, 虚拟系统为ubuntu 16.04 方案一: 通过虚拟机vmware的共享文件夹实现. 方案二: ...
- redis 数据导入导出,实例内db迁移
源实例db0迁移至目标实例db1 [root@172.20.0.1 ~]# cat redis_mv.sh #!/bin/bash redis-cli -h -a password -n keys & ...
- Android内核开发:理解和掌握repo工具【转】
本文转载自:http://ticktick.blog.51cto.com/823160/1653304 由于Android源码是用repo工具来管理的,因此,搞Android内核开发,首先要搞清楚re ...
- tomcat 正常启动但不能访问
Eclipse中的Tomcat可以正常启动,不过发布项目之后,无法访问,包括http://localhost:8080/的小猫页面也无法访问到,报404错误.这是因为Eclipse所指定的Server ...
- Myeclipse 快捷键使用
MyEclipse快捷键大全-------------------------------------MyEclipse 快捷键1(CTRL)----------------------------- ...
- java.net.UnknownHostException异常处理
1.问题描述 最近迁移环境,在Linux系统下部署Java产品的应用,后台报出如下异常,系统报找不到名为“xxx-houtai1”的主机: 1 java.net.UnknownHostExceptio ...