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. C# 封装首页、上一页、下一月、尾页处理器

    public void BtnPageClickEvent(object sender,string focusForeground,string lostFocusForeground) { But ...

  2. jQuery.event.move

    http://stephband.info/jquery.event.move/ Move events movestart Fired following touchmove or mousemov ...

  3. CF961F k-substring

    题意:给你一个字符串(sl<=1e6),问每一个起点到1和终点到sl距离相等的子串的最长不等于串长的border. 标程: #include<cstdio> #include< ...

  4. $.ajax()方法和$.get()方法使用小结

    一. 使用JQuery的$.get()方法实现异步请求 1. 编写JSP <!DOCTYPE html> <html lang="en"> <head ...

  5. leetcode-第五场双周赛-1133-最大唯一数

    第一次提交: class Solution: def largestUniqueNumber(self, A: List[int]) -> int: dict = {} for i in A: ...

  6. 廖雪峰Java14Java操作XML和JSON-1XML-3SAX

    SAX:Simple API for XML 基于事件的API import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXPars ...

  7. LUOGU P3387 【模板】缩点 (缩点+DAG dp)

    解题思路 缩点后按拓扑排序跑一个dp. #include<iostream> #include<cstdio> #include<cstring> #include ...

  8. MYSQL获取同时关注了某两个(或者N个)用户的用户

    使用redis的set类型数据的话会比较容易,但是业务场景就是在mysql里面,因此也需要思考解决方法 表结构: CREATE TABLE `table_name` ( `id` ) unsigned ...

  9. Excel常用函数总结

    Excel常用函数总结 2016-10-28 Kevin 叼着奶瓶撩妹 1. VLOOKUP函数 常见形式 问题描述:将下图中G列的数据根据学生的姓名填充到D列. 公式解析: =VLOOKUP(A2, ...

  10. iOS之CAScrollLayer属性简介和使用

    1.CAScrollLayer的简介 CAScrollLayer用于显示一个滑动图层的一部分,可以确定滑动方向和可视区域面积,限制不滑出区域外!相关属性如下:其中 /* Scroll the cont ...