国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Leetcode 167. Two Sum II - Input array is sorted

2019-11-11 01:31:35
字體:
供稿:網(wǎng)友

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

s思路: 1. 排好序的情況,說明不考慮map,但可以用two pointer:左邊一個(gè)、右邊一個(gè),往中間走! 2. two pointer對排好序的情況,很好用!

class Solution {public: vector<int> twoSum(vector<int>& numbers, int target) { // int l=0,r=numbers.size()-1; while(l<r){ if(numbers[l]+numbers[r]==target) break; if(numbers[l]+numbers[r]<target){ l++; }else r--; } //return {l++,r++};//bug:應(yīng)該先++ return {++l,++r}; }};
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 韶关市| 武平县| 黄石市| 高要市| 淅川县| 奉节县| 宽甸| 临颍县| 通许县| 临武县| 新宾| 玛曲县| 三亚市| 乌兰察布市| 平陆县| 甘洛县| 丰城市| 凉山| 新竹县| 华阴市| 全州县| 瑞金市| 固原市| 古蔺县| 新沂市| 北流市| 高雄县| 内丘县| 岑巩县| 九龙城区| 永新县| 扎囊县| 岱山县| 临猗县| 营山县| 绩溪县| 余姚市| 宝清县| 楚雄市| 泰宁县| 三亚市|