给定一个整数数列,找出其中和为特定值的那两个数。

你可以假设每个输入都只会有一种答案,同样的元素不能被重用。

示例:

给定 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. 领扣-1/167 两数之和 Two Sum MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  2. [Swift]LeetCode1 .两数之和 | Two Sum

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

  3. c++谭浩强教材教学练习例题1.2 求两数之和 为什么sum=a+b;sum的值为65538

    第一章 #include <iostream>using namespace std; int main(){ int a,b,sum; sum=a+b; cin>>a> ...

  4. LeetCode 1:两数之和 Two Sum

    题目: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中 ...

  5. 1.两数之和(Two Sum) C++

    暴力法可解决,速度很慢. 解决办法:哈希表 知识点: map的构造 遍历map使用迭代器,判断条件 插入 pair<int,int> 寻找key是否存在 class Solution { ...

  6. LeetCode 653. 两数之和 IV - 输入 BST(Two Sum IV - Input is a BST)

    653. 两数之和 IV - 输入 BST 653. Two Sum IV - Input is a BST 题目描述 给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定 ...

  7. LeetCode_#1_两数之和 Two Sum_C++题解

    1. 两数之和 Two Sum 题目描述 Given an array of integers, return indices of the two numbers such that they ad ...

  8. LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  9. 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 ...

随机推荐

  1. Oracle EBS R12 客户表结构

    参考链接: Oracle EBS R12 客户表结构 Oracle EBS中的“客户”."客户地点".‘订单’之间的关系 Oracle EBS中的“客户”."客户地点&q ...

  2. seo-摘自网友

    网页前端制作中的SEO 在SEO盛行的今天到处都在谈优化,对于网站前端制作人员来说,有几点是跟SEO相关的,也就是SEO站内优化中的一部分,下面总结几点: 1.title,<title>页 ...

  3. echatrs可视化图在隐藏后显示不出来或是宽度出现问题

    最近在做一个可视化的项目,用了百度的ECharts.js作为可视化的视图框架,echarts的实例很多,基本能满足项目的需求,而且文档也相对完整.清晰,是个很不错的前端可视化框架. 我们的项目是使用b ...

  4. ajax ajax基本介绍

    jquery中ajax方法参数详解 url 要求是string类型参数(默认为当前页面地址) 发送请求的地址 type 要求是string类型的参数,请求方式(post或get)默认为get.注意其他 ...

  5. struts2.1.6 action 01

    目录(?)[-] 安装与设置 HelloWorld 常见问题 Action   struts 官网下载 http://www.apache.org/ http://struts.apache.org/ ...

  6. Linux内核分析-分析system_call中断处理过程

    姓名:江军 ID:fuchen1994 分析system_call中断处理过程 使用gdb跟踪分析一个系统调用内核函数(您上周选择那一个系统调用),系统调用列表参见http://codelab.shi ...

  7. MyEclipse WebSphere开发教程:安装和更新WebSphere 6.1, JAX-WS, EJB 3.0(三)

    MyEclipse超值折扣 限量 100 套! 立即开抢>> [MyEclipse最新版下载] MyEclipse支持Java EE技术(如JAX-WS和EJB 3.0),它们以功能包的形 ...

  8. html <meta>设置自动刷新或者几秒内跳转到指定页面

    指定时间自动刷新: <meta http-equiv="refresh" content="2"/> 指定时间跳转到指定页面: <meta h ...

  9. mdadm 创建md 删除md步骤

    最近在使用mdadm创建和删除RAID设备.但是在创建和删除过程中会出现创建md0重启后变成md127,删除md127重启后又重新出现的状况.在网上搜索了一下,总结如下:   创建: 1.  mdad ...

  10. L2-009. 抢红包

    没有人没抢过红包吧…… 这里给出N个人之间互相发红包.抢红包的记录,请你统计一下他们抢红包的收获. 输入格式: 输入第一行给出一个正整数N(<= 104),即参与发红包和抢红包的总人数,则这些人 ...