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. Please note that 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: index1=1, index2=2

思路:首先应该注意标题中的关键字,输入的数组已经排序了,也就是说前面是小的数字,后面是大的数字。

自己代码:(从头开始遍历,会超时)

 vector<int> twoSum(vector<int>& numbers, int target) {
int n = numbers.size();
vector<int>index;
for(int i = ; i < n - ; i++){
for(int j = i + ; j < n; j++){
if(numbers[i] + numbers[j] == target){
index.push_back(i+);
index.push_back(j+);
}
}
}
return index;
}

优秀代码:(3ms)

 vector<int> twoSum(vector<int>& numbers, int target) {
int m = , n = numbers.size() - ;
while(m < n){
if(numbers[m] + numbers[n] < target)
m++;
else if(numbers[m] + numbers[n] > target)
n--;
else
return vector<int>{m + , n + };//这里可以直接返回vector,{}表示赋值初始化
}
}

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

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

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

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

  6. [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 ...

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

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

  9. (双指针 二分) leetcode 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 ...

随机推荐

  1. SpringCloud学习笔记《---02 Eureka ---》篇

  2. springAop的使用

    AspectJ使用org.aspectj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强时,使用org.aspectj.lang.ProceedingJoinPoint表示连接点 ...

  3. 编写main方法

  4. keepalived的常见的健康检查方式

    TCP_CHECK tcp端口检测 HTTP_GET http接口检测 MISC_CHECK 自定义脚本检测 tcp端口检测 TCP_CHECK { connect_port 80 connect_t ...

  5. sqllocaldb

    创建实例  sqllocaldb create v12.0 启动实例 sqllocaldb start v12.0

  6. rsyslog 服务器重启后 发现不能接受到外部日志 只能接受本地日志 关闭防火墙即可

    rsyslog 服务器重启后 发现不能接受到外部日志 只能接受本地日志  关闭防火墙即可 1 关闭防火墙: # systemctl stop firewalld 2 将SELINUX设置为disabl ...

  7. 期望——邮票收集问题lightoj1342

    邮票手机问题: 有n种类型的邮票,问将所有的类型的邮票全部收集起来所要的收集次数期望是多少. 设dp[i]为已经收集了i种类型的票,还要收集n-i种的次数的期望. dp[n]=0; 递推式: dp[i ...

  8. php数据结构课程---6、常见排序有哪些

    php数据结构课程---6.常见排序有哪些 一.总结 一句话总结: 冒泡排序(Bubble sort):依次交换 选择排序 ( Selection Sort ):在未排序序列中找到最小(大)元素,依次 ...

  9. Java笔记 - 异常机制

    JAVA异常机制是Java提供的用于处理程序在运行期可能出现的异常事件(如数组下标越界.文件不存在等)的一种机制,使程序不会因为 异常的发生 而 阻断或产生不可预见的结果 .而且还可以将逻辑代码与错误 ...

  10. Solrj demo

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...