这是悦乐书的第179次更新,第181篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第38题(顺位题号是167)。给定已按升序排序的整数数组,找到两个数字,使它们相加到特定的目标数。函数twoSum应该返回两个数字的索引,使它们加起来到目标,其中index1必须小于index2。

注意

  • 返回的答案(index1和index2)不是从零开始的。

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

例如:

输入:数字= [2,7,11,15],目标= 9

输出:[1,2]

说明:2和7之和为9.因此index1 = 1,index2 = 2。



本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。

02 第一种解法

此题和LeetCode算法题的第一题Two Sum类似,不过此题定义和条件更加清晰,规定了只存在唯一解,并且将数组进行了排序。

和以前一样,第一种解法是暴力解法,使用双层循环,依次遍历相加判断和是否等于目标值,直到找到两元素为止。但是元素的索引需要加1,因为题目要求索引不从0开始。

特殊情况:当数组为空或者其长度小于2时,直接返回空。

public int[] twoSum (int[] numbers, int target) {
if (numbers == null || numbers.length < 2) {
return null;
}
int[] index = new int[2];
for(int i = 0; i < numbers.length; i++) {
for(int j = i+1; j < numbers.length; j++) {
if (numbers[i] + numbers[j] == target) {
index[0] = i+1;
index[1] = j+1;
}
}
}
return index[0] == 0 ? null : index;
}

此解法时间复杂度是O(n^2),空间复杂度是O(1)。

03 第二种解法

使用HashMap,遍历数组时,判断当前元素和目标值之间的差值是否存在map中,如果存在,就返回map中此元素的value,即其索引,和当前元素的索引;否则,将当前元素作为key,索引作为value存入map中,然后进行下一次循环。

特殊情况:当数组为空或者其长度小于2时,直接返回空。

public int[] twoSum2 (int[] numbers, int target) {
if (numbers == null || numbers.length < 2) {
return null;
}
int[] index = new int[2];
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
for (int k=0; k<numbers.length; k++) {
if (map.containsKey(target-numbers[k])) {
index[0] = map.get(target-numbers[k]);
index[1] = k+1;
}
map.put(numbers[k], k+1);
}
return index[0] == 0 ? null : index;
}

时间复杂度是O(n),空间复杂度是O(n)。

04 第三种解法

既然数组已经是排过序的,使用双指针,每次从前往后和从后往前各取一个元素,判断两数之和是否等于目标值,如果小于目标值,则从前往后的指针向前移动一位;如果大于目标值,则从后往前的指针向前移动一位,直到两数之和等于目标值即可。

特殊情况:当数组为空或者其长度小于2时,直接返回空。

public int[] twoSum3(int[] numbers, int target) {
if (numbers == null || numbers.length < 2) {
return null;
}
int start = 0;
int end = numbers.length - 1;
while (start < end) {
int sum = numbers[start] + numbers[end];
if (sum == target) {
return new int[] {start+1, end+1};
} else if (sum < target) {
start++;
} else {
end--;
}
}
return null;
}

此解法的时间复杂度是O(n),空间复杂度是O(1)。

05 小结

算法专题目前已连续日更超过一个月,算法题文章38+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

LeetCode算法题-Two Sum II - Input array is sorted的更多相关文章

  1. leetcode算法:Two Sum II - Input array is sorted

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  2. Leetcode No.167 Two Sum II - Input array is sorted(c++实现)

    1. 题目 1.1 英文题目 Given an array of integers numbers that is already sorted in non-decreasing order, fi ...

  3. 【LeetCode】167. Two Sum II - Input array is sorted

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...

  4. 【LeetCode】167. Two Sum II - Input array is sorted 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  5. 【Leetcode 167】Two Sum II - Input array is sorted

    问题描述:给出一个升序排列好的整数数组,找出2个数,它们的和等于目标数.返回这两个数的下标(从1开始),其中第1个下标比第2个下标小. Input: numbers={2, 7, 11, 15}, t ...

  6. 【LEETCODE】38、167题,Two Sum II - Input array is sorted

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  7. Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

    Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...

  8. 167. Two Sum II - Input array is sorted - LeetCode

    Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...

  9. 29. leetcode 167. Two Sum II - Input array is sorted

    167. Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascendi ...

随机推荐

  1. 实战!基于lamp安装Discuz论坛-技术流ken

    简介 我前面的博客已经详细介绍了lamp采用yum安装以及编译安装的方式,这篇博客将基于yum安装的lamp架构来实战安装Discuz论坛,你可以任选其一来完成. 系统环境 centos7.5 服务器 ...

  2. shell编程基础(三): 位置参数与shell脚本的输入输出

    一.位置参数和特殊变量 有很多特殊变量是被Shell自动赋值的,我们已经遇到了$?和$1,现在总结一下: 常用的位置参数和特殊变量: $0 相当于C语言main函数的argv[0] $1.$2... ...

  3. ife2018 零基础学院 day 4

    第四天,背景边框列表链接和更复杂的选择器 背景,边框,列表,链接相关属性 背景 MDN 背景 W3School 背景 元素的背景是指,在元素内容.内边距和边界下层的区域. 属性 描述 backgrou ...

  4. 从零开始学安全(二十)●PHP辅助函数

  5. 月薪15k的测试员需要学习什么技术?

    想了很久,决定还是要写一篇这样的文章出来,月薪15k的测试员需要学习什么技术?我觉得测试想要月薪15k并不难,只要做到我说的这几点肯定是可以的! 克服懒惰 我觉得,越是聪明的人越是觉得自己“懒惰”.大 ...

  6. Idea 15 激活

    https://www.cnblogs.com/moko/p/5012006.html 1.把补丁下载到自己的电脑上 2.打开idea,help->edit custom VM options ...

  7. POJ2155(二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17226   Accepted: 6461 Descripti ...

  8. ajaxJson(常用)

    function ajaxJson(method, url, data, callback) { var options = { type: method, url: url, dataType: ' ...

  9. 洛谷P4103 [HEOI2014]大工程(虚树 树形dp)

    题意 链接 Sol 虚树. 首先建出虚树,然后直接树形dp就行了. 最大最小值直接维护子树内到该节点的最大值,然后合并两棵子树的时候更新一下答案. 任意两点的路径和可以考虑每条边两边的贡献,\(d[x ...

  10. Spring学习之旅(六)Spring AOP工作原理初探

    AOP(Aspect-Oriented  Programming,面向切面编程)是Spring提供的关键技术之一. AOP基于IoC,是对OOP(Object-Oriented Programming ...