Description

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.

Example:

Input: numbers = [,,,], target =
Output: [,]
Explanation: The sum of and is . Therefore index1 = , index2 = .

题目描述,给定一个已排序的数组,和一个目标值,计算数组中的两个值的和等于目标值时的位置,这里位置不从0开始。

思路: 数组是以排序的,则使用两个指针,分别指向数组的头尾,当两数相加小于目标值是则头指针递增一位,当相加大于目标值值,尾指针递减。

下面是代码,感觉还有优化的地方。先贴出来。

public int[] TwoSum(int[] numbers, int target)
{
int[] result = new int[];
int lo=, hi=numbers.Length-;
while(lo < hi)
{
if (numbers[lo] + numbers[hi] < target) lo++;
else if (numbers[lo] + numbers[hi] > target) hi--;
else
break;
}
result[] = lo + ;
result[] = hi + ;
return result;
}

LeetCode Array Easy 167. Two Sum II - Input array is sorted的更多相关文章

  1. [LeetCode&Python] Problem 167. 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. 167. Two Sum II - Input array is sorted【easy】

    167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...

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

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

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

  5. 167. Two Sum II - Input array is sorted@python

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

  6. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

  7. [LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组

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

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

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

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

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

随机推荐

  1. spring集成rabbitMq(非springboot)

    首先 , pom文件需要加入spring集成rabbitMq的依赖: <dependency> <groupId>org.springframework.amqp</gr ...

  2. WPFのStyle TargetType的不同写法

    一.隐式写法 <Style TargetType="TextBlock"> <Setter Property="/> </Style> ...

  3. oracle中Varchar2和nvarchar2的区别

    在oracle 11g下测试了下varchar2与nvarchar2这两种类型,网上有很多关于这两种类型的区别的帖子,我还是自己测试了下. varchar2(size type),size最大为400 ...

  4. elasticsearch 嵌套对象使用Multi Match Query、query_string全文检索设置

    参考: https://www.elastic.co/guide/en/elasticsearch/reference/1.7/mapping-nested-type.html https://sta ...

  5. 对于一键退出APP功能实现的技术探讨

    在Android的开发过程中,会经常存在“一键退出APP”的需求.经过一段时间的整理,其主要实现方式有以下几种. 本质:一键结束当前APP的所有activity&一键结束当前APP进程,两者合 ...

  6. Docker基础(上)

    Docker基础(上) 链接:https://pan.baidu.com/s/1KQjKml2OZAReYwOvpWD9XQ 提取码:6vo8 复制这段内容后打开百度网盘手机App,操作更方便哦 1. ...

  7. zTree节点排序、jsTree节点排序

    项目中遇到了这个问题,网上也没找到比较清晰的答案,索性提供一个方案吧. 原理:将整个树形插件的数据源进行排序,插件在构造UI时,自然也是按照顺序来排列的,目前这种思路适用于 zTree 和 jsTre ...

  8. Jetson Nano 系列教程2:串口调试接口登录Jetson Nano

    连接Jetson Nano可以有多种方法,这里我们一一介绍一下.开始本章节前,请先参考上一章,烧写好镜像 直接连接 所谓直接连接,就是将Jetson Nano当做主机,连接HDMI屏幕,连接键盘和鼠标 ...

  9. mapreduce求共同好友

    逻辑分析 以下是qq的好友列表数据,冒号前是一个用户,冒号后是该用户的所有好友(数据中的好友关系是单向的) A:B,C,D,F,E,O B:A,C,E,K C:F,A,D,I D:A,E,F,L E: ...

  10. BZOJ 3585: mex(分块+莫队)

    传送门 解题思路 首先直接莫队是能被卡的,时间复杂度不对.就考虑按照值域先进行分块再进行莫队,然后统计答案的时候就暴力扫所有的块,直到一个块内元素不满,再暴力扫这个块就行了,时间复杂度O(msqrt( ...