C#LeetCode刷题之#167-两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章
- LeetCode 167:两数之和 II - 输入有序数组 Two Sum II - Input array is sorted
公众号: 爱写bug(ID:icodebugs) 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index ...
- [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 ...
- leetcode刷题笔记-1. 两数之和(java实现)
题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素不能使 ...
- LeetCode 刷题笔记 1. 两数之和(Two Sum)
tag: 栈(stack) 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案. ...
- (1)leetcode刷题Python笔记——两数之和
题目如下: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数 ...
- 167两数之和II-输入有序数组
from typing import List# 这道题很容易能够想到,只需要遍历两边列表就可以了# 两层循环class Solution: def twoSum(self, numbers: Lis ...
- Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)
Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...
- #leetcode刷题之路15-三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- #leetcode刷题之路1-两数之和
给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符.返回被除数 dividend 除以除数 divisor 得到的商. 示例 1:输入: ...
- LeetCode167.两数之和II-输入有序数组
给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值 ...
随机推荐
- Ant-Design-Vue中关于Form组件的使用
1.创建form表单的两种方式,不同的方式在js中创建表单的方式也不同 方式1:一般使用在搜索表单中,只需要双向绑定数据即可,那就使用这种方法即可 <template> <a-for ...
- SpringBoot系列之Elasticsearch极速入门与实际教程
@ 目录 一.什么Elasticsearch? 二.Elasticsearch安装部署 2.1 Elasticsearch安装环境准备 2.2 Docker环境安装Elasticsearch 2.3 ...
- vue & 百度地图:在地图上绘制多边形
<template> <div class="hello"> <div style="margin-bottom:10px"> ...
- uTools - 你的生产力工具集
工具介绍 uTools是一个极简.插件化.跨平台的现代化桌面软件.通过自由选配丰富的插件,打造你得心应手的工具集合. 通过快捷键(默认alt+space)就可以快速呼出这个搜索框.它相当聪明,可以支持 ...
- 循序渐进nginx(一):介绍、安装、hello world、Location匹配
目录 前言: Nginx是什么 使用场景: 官方文档说明 安装 windows下: linux(CentOS7)下: docker下: 目录结构 Hello World 1.展示一下默认的核心配置: ...
- C# WebClient几种常用方法的用法
1.UploadData方法(Content-Type:application/x-www-form-urlencoded) //创建WebClient 对象 WebClient ...
- Learning in the Frequency Domain 解读
论文:Learning in the Frequency Domain, CVPR 2020 代码:https://github.com/calmevtime/DCTNet 实际的图像尺寸比较大,无法 ...
- 互联网找的e是无理数的初等证明
e的两种计算方式 \(e=lim_{n \to \infty}(1+\frac{1}{n})^n\) \(e=\sum_{n=0}^{+\infty}\frac{1}{n!}\) \(即,e=\fra ...
- 利用div显示隐藏实现的分页效果
实现步骤: 1.创建对应切换div <div class="bottom_daohang"> <div class="bottom_daohang_zo ...
- Python while 中简单的语句组
Python while 中简单的语句组: 只使用 while: # 简单的语句组 a = 4 b = 8 num = 0 while a < b: print("a 比 b 小&qu ...