问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3903 访问。

给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。

函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2

说明:

  • 返回的下标值(index1 和 index2)不是从零开始的。
  • 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。

输入: numbers = [2, 7, 11, 15], target = 9

输出: [1,2]

解释: 2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。


Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.

Note:

  • Your returned answers (both index1 and index2) are not zero-based.
  • You may assume that each input would have exactly one solution and you may not use the same element twice.

Input: numbers = [2,7,11,15], target = 9

Output: [1,2]

Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3903 访问。

public class Program {

    public static void Main(string[] args) {
int[] prices = { 2, 7, 11, 15 }; var res = TwoSum(prices, 9);
Console.WriteLine($"[{res[0]},{res[1]}]"); Console.ReadKey();
} private static int[] TwoSum(int[] numbers, int target) {
int left = 0, right = numbers.Length - 1, sum = 0;
//双指针法,因为原数组已经有序
//所有用左右指针分别指向首和尾
//根据值的比较移动指针直到相撞时为止
while(left < right) {
sum = numbers[left] + numbers[right];
//当2个值的和大于目标值时,左移右指针
if(sum > target) right--;
//当2个值的和小于目标值时,右移左指针
else if(sum < target) left++;
//匹配目标值时,直接返回
else if(sum == target)
return new int[] { left + 1, right + 1 };
}
//找不到时返回空数组
return new int[] { };
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3903 访问。

[1,2]

分析:

显而易见,以上算法的时间复杂度为: 

另外,因为是有序的数组,很容易想到使用二分查找,二分查找本身的时间复杂度为:  ,考虑到还需要一个原问题规模n的外循环,所以其时间复杂度应为:  , 有兴趣的同学可以自己动手试一下。

C#LeetCode刷题之#167-两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)的更多相关文章

  1. LeetCode 167:两数之和 II - 输入有序数组 Two Sum II - Input array is sorted

    公众号: 爱写bug(ID:icodebugs) 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index ...

  2. [Swift]LeetCode167. 两数之和 II - 输入有序数组 | 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 ...

  3. leetcode刷题笔记-1. 两数之和(java实现)

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

  4. LeetCode 刷题笔记 1. 两数之和(Two Sum)

    tag: 栈(stack) 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案. ...

  5. (1)leetcode刷题Python笔记——两数之和

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

  6. 167两数之和II-输入有序数组

    from typing import List# 这道题很容易能够想到,只需要遍历两边列表就可以了# 两层循环class Solution: def twoSum(self, numbers: Lis ...

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

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

  8. #leetcode刷题之路15-三数之和

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...

  9. #leetcode刷题之路1-两数之和

    给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符.返回被除数 dividend 除以除数 divisor 得到的商. 示例 1:输入: ...

  10. LeetCode167.两数之和II-输入有序数组

    给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值 ...

随机推荐

  1. JavaScript定时器及回调用法

    JavaScript定时器及回调用法 循环定时任务 // 假设现在有这样一个需求:我需要请求一个接口,根据返回结果判断需不需要重复请求,直到达到某一条件为止,停止请求执行某操作 <script ...

  2. springmvc 重定向到外网地址

    return  new ModelAndView(new RedirectView("http://www.baidu.com"));

  3. Onedrive分享型网盘搭建 - OneManager

    注册账号 部署OneManager 注册完账号后打开网址:https://heroku.com/deploy?template=https://github.com/qkqpttgf/OneManag ...

  4. PagedList分页,如何添加action参数

    使用PagedList分页,如 @Html.PagedListPager((IPagedList)Model, page => Url.Action("Index", new ...

  5. css选择器大全

    1.元素选择器 这是最基本的CSS选择器,HTML文档中的元素本身就是一个选择器: p {line-height:1.5em; margin-bottom:1em;} //设置p元素行高1.5em,距 ...

  6. package.json中dependencies和devDependencies区别

    package.json中dependencies和devDependencies区别 dependencies: 应用能够正常运行依赖的包.用户发布环境,依赖的包不仅开发环境能够使用,生产环境也能使 ...

  7. 05 ES6模块化规范基础详解

    ES6模块规范 1.1 ES6规范说明 历史上,JavaScript 一直没有模块(module)体系,无法将一个大程序拆分成互相依赖的小文件,再用简单的方法拼装起来.其他语言都有这项功能,比如 Ru ...

  8. xilinx fpga中块ram的使用——简单双端口ram的使用

    在简单双端口ram中最简单有9个端口:分别是 clka  为输入端口的时钟 wea  读写控制端,高为写,低为读 addra 写地址 dina  待写入的数据 clkb 为输出端口的时钟的 addrb ...

  9. Java 中的链式编程

    前言 ​ 在写项目的时候,有一个实体类有好多个属性,new 出来之后需要不停的使用setXXX( )方法,效率低而且代码可读性差,查询了下发现可以实现实体类的链式编程. public class Us ...

  10. select、poll和epoll之间的区别

    在深入理解select.poll和epoll之间的区别之前,首先要了解什么是IO多路复用模型. IO多路复用 简单来说,IO多路复用是指内核一旦发现进程指定的一个或者多个IO条件准备就绪,它就通知该进 ...