两数之和 Two Sum
给定一个整数数列,找出其中和为特定值的那两个数。
你可以假设每个输入都只会有一种答案,同样的元素不能被重用。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1]
方法一
使用i,j遍历数组nums中的每一个变量,验证nums[i]+nums[j]是否等于target
代码如下:
public static int[] twoSum(int[] nums, int target) {
int N = nums.length;
for (int i=0; i<N-1; i++)
for (int j=i+1; j<N; j++)
if (nums[i] + nums[j] == target)
return new int[] {i,j};
return null;
}
此方法的复杂度为O(N^2)
方法二
使用HashMap存储nums的值及对应的下标
定义变量c=target-nums[i],在map中寻找是否存在键值为c
代码如下:
public static int[] twoSumMap(int[] nums, int target) {
int N = nums.length;
Map<Integer, Integer> map = new HashMap<>();
// nums的值为key,下标为value
for (int i=0; i<N; i++)
map.put(nums[i], i);
for (int i=0; i<N-1; i++) {
int complement = target - nums[i];
// 找到的键值不能为i
if (map.containsKey(complement) && map.get(complement)!=i)
return new int[] {i, map.get(complement)};
}
return null;
}
复杂度分析:
将nums存入map中,空间复杂度O(N)
for循环寻找两个下标,时间复杂度为O(N)
该方法中,键值为数组值,value为数组下标,故数组中数值相等的元素在map中只存储了一次。
如nums[] = {2, 5, 5, 6}, map中为2-0, 5-2, 6-3,但这并不影响该算法找到正确的答案
另外,若数组nums为有序的,则可以使用两个指针分别指向数组的首尾两个元素。
比较两个元素的和与target,根据比较结果移动指针,直到两指针相遇或找到结果
实现代码如下:
public static int[] twoSumOrder(int[] nums, int target) {
int i = 0;
int j = nums.length - 1;
while (i != j) {
if (nums[i] + nums[j] == target)
return new int[] {i,j};
else if (nums[i] + nums[j] > target)
j--;
else
i++;
}
return null;
}
复杂度为O(N)
两数之和 Two Sum的更多相关文章
- 领扣-1/167 两数之和 Two Sum MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- [Swift]LeetCode1 .两数之和 | Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- c++谭浩强教材教学练习例题1.2 求两数之和 为什么sum=a+b;sum的值为65538
第一章 #include <iostream>using namespace std; int main(){ int a,b,sum; sum=a+b; cin>>a> ...
- LeetCode 1:两数之和 Two Sum
题目: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中 ...
- 1.两数之和(Two Sum) C++
暴力法可解决,速度很慢. 解决办法:哈希表 知识点: map的构造 遍历map使用迭代器,判断条件 插入 pair<int,int> 寻找key是否存在 class Solution { ...
- LeetCode 653. 两数之和 IV - 输入 BST(Two Sum IV - Input is a BST)
653. 两数之和 IV - 输入 BST 653. Two Sum IV - Input is a BST 题目描述 给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定 ...
- LeetCode_#1_两数之和 Two Sum_C++题解
1. 两数之和 Two Sum 题目描述 Given an array of integers, return indices of the two numbers such that they ad ...
- LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- 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 ...
随机推荐
- 变形CSS3
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> < ...
- SQL TUNING——从近半小时到几十毫秒的一次优化
昨天,一个用户的现场人员打电话紧急求助,说他们的一个系统卡了,半天不出结果,严重的影响了他们的使用,我简单的问了几句:什么时候的事儿?答:就今天下午的事儿.问:数据库软硬件最近动过没?答:没动过.问: ...
- eclipse编译zookeeper源码
使用版本zookeeper-3.4.6.jar 从官网下载zookeeper-3.4.6.tar.gz,解压缩到 D:\tools 文件夹,目录结构如下图. 1. Eclipse新建java工程: 2 ...
- $digest already in progress 解决办法——续
什么时候手动调用$apply()方法? 如果AngularJS总是将我们的代码wrap到一个function中并传入$apply(),以此来开始一轮$digest循环,那么什么时候才需要我们手动地调用 ...
- 进程通信方式-管道pipe
管道是两个进程间进行单向通信的机制.因为管道传递数据的单向性,管道又称之为半双工管道. 1.数据只能从一个进程流向另一个进程(其中一个写管道,另一个读管道):如果要进行全双工通信,需要建立两个管道. ...
- spring boot 学习(五)SpringBoot+MyBatis(XML)+Druid
SpringBoot+MyBatis(xml)+Druid 前言 springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成. 主要是 ...
- TClientDataSet的 AddIndex
unit Unit2; interface uses SysUtils, Classes, DB, DBClient; type TDataModule2 = class(TDataModule) C ...
- python中read()、readline()、readlnes()
在python中 1.file.read()直接按原样读取文件,它通常用于将文件内容放到一个字符串变量中,如果文件大于可用内存,则不可能实现这种处理,因为原来文件里面是str_class,所以 fil ...
- SharePoint的安装和配置-PowerShell
1. 引入SPModule组件 Import-Module SPModule.misc Import-Module SPModule.setup 需要将执行策略修改为不限制 2. 无人值守安装Shar ...
- UIViewController之间的相互跳转
一.最普通的视图控制器UIViewContoller 一个普通的视图控制器一般只有模态跳转的功能(ipad我不了解除外,这里只说iPhone),这个方法是所有视图控制器对象都可以用的,而实现这种功能, ...