Problem:

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.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

Summary:

有序数组,找到两数之和等于target的数字下标。

Solution:

两个指针i和j分别指向数组头和尾,若nums[i] + nums[j] > target,则j--,反之则i++,直至找到目标数。

 class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int len = numbers.size();
int i = , j = len - ;
vector<int> res;
while (i < j) {
if (numbers[i] + numbers[j] > target) {
j--;
}
else if (numbers[i] + numbers[j] < target) {
i++;
}
else {
return {i + , j + };
}
} return {};
}
};

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

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

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

  5. ✡ leetcode 167. Two Sum II - Input array is sorted 求两数相加等于一个数的位置 --------- java

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

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

  7. LeetCode - 167. Two Sum II - Input array is sorted - O(n) - ( C++ ) - 解题报告

    1.题目大意 Given an array of integers that is already sorted in ascending order, find two numbers such t ...

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

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

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

随机推荐

  1. hTML5实现表单内的上传文件框,上传前预览图片,针刷新预览images

    hTML5实现表单内的上传文件框,上传前预览图片,针刷新预览images, 本例子主要是使用HTML5 的File API,建立一個可存取到该file的url, 一个空的img标签,ID为img0,把 ...

  2. window.hostory(浏览器的历史记录)

    浏览器会对同一个窗口(选项卡)中访问的网页进行记录,不管我们是通过以下哪种方式改变网页,浏览器都会把改变后的网页记录下来,以便通过浏览器的前进和后退按钮,能够快速的切换到已经访问过的网页:  1)直接 ...

  3. WA题集

    #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> ...

  4. dede织梦批量导入关键词

    在后台替换对应的文件件即可. 注意:如果你的关键字长度超过16个字符的话,需要更改 dede 中 keywords 表中的keyword 字段字符长度 article_keywords_main.ph ...

  5. 打开gvim发现菜单栏是乱码

    默认安装将会把中文区域设置为zh_CN.utf8,而GVim能识别的中文区域设置为zh_CN.UTF-8.因此GVim会在启动时报错,且 无法正常加载中文菜单.解决方案:Linux下编辑 ~/.vim ...

  6. 用java解析字符串,如字符串"(1+2/5)*3"当成是数值表达式,进行计算出结果来

    import java.io.*;import javax.script.ScriptEngine;import javax.script.ScriptEngineManager;public cla ...

  7. Java学习笔记-Math类

    并非所有的类都需要main方法.Math类和JOptionPane类都没有main方法.这些类中所包含的方法主要是为了供其他类使用. package welcome; public class Tes ...

  8. 影响postgresql性能的几个重要参数

    转载 一篇蛮老的文章了,但是还是很有用,可参考修补. PG的配置文件是数据库目录下的postgresql.conf文件,8.0以后的版本可支持K,M,G这样的参数,只要修改相应参数后重新启动PG服务就 ...

  9. JavaScript - 原型

    一切皆为对象 殊不知,JavaScript的世界中的对象,追根溯源来自于一个 null 「一切皆为对象」,这句着实是一手好营销,易记,易上口,印象深刻. 万物初生时,一个null对象,凭空而生,接着O ...

  10. Bootstrap table使用心得

    序号显示带分页信息的连续编号,在序号列添加以下格式化代码即可. { field: 'number', title: '序号', align:'center', width:45, formatter: ...