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. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  2. RabbitMQ 集群

    集群 消息队列要想在项目里用的溜,还是要做集群.稳定可靠 但是如果只有一个服务器的话 崩溃的话还是会懵逼的.所以集群化让崩溃的损失降到最小. 在这里我要用三台机器来做集群,不同系统在一起集合才叫酸爽 ...

  3. 翻书插件:wowbook.js

    wowbook是一个jQuery插件,可以让你在网站中发布一本页面翻转效果超赞的书. demo演示 基本页面 <!DOCTYPE html> <html lang="en& ...

  4. dos 命令帮助文档chm

    http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx?mfr=true

  5. Swift-代理

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px "Helvetica Neue"; color: #535b60; bac ...

  6. 命令查询网站是否开启CDN加速

    1.使用ping命令 不一定准确  运行-CMD 直接输入ping  域名 C:\Users\6503>ping www.netnic.com.cn 正在 Ping u999.v.qingcdn ...

  7. iPhone屏幕尺寸/launch尺寸/icon尺寸

    屏幕尺寸 6p/6sp     414 X 736 6/6s         375 X 667 5/5s         320 X 568  4/4s         320 X 480   la ...

  8. 利用beans.xml进行简单的Spring应用上下文创建与使用

    继上次配置Spring完成后,我们来创建一个简单的例程来理解Spring中利用beans.xml创建应用上下文的方法. 程序路径包为:com.spring.kinghts(kinght单词拼写错误,怕 ...

  9. 关于java的格式输出控制

    java中的System.out.println()功能十分强大,可以对任意类型的数据进行输出. 这里我们不讨论System.out.println(),而是讨论System.out.printf() ...

  10. netlink优势

    netlink相对其他应用进程和内核之间通信的方式(ioctrl或者系统文件等方式),全双工,可由内核发起,应用进程可用epoll监听,而其他方式只能由应用进程发起. 顺便记下隧道,隧道可以通过在ip ...