leetcode167. Two Sum II - Input array is sorted
来源:互联网 发布:java 数组接收 split 编辑:程序博客网 时间:2024/05/21 14:46
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
class Solution {public: vector<int> twoSum(vector<int>& numbers, int target) { vector<int> res; int len=numbers.size(); if(len<2) return res; for(int i=1;i<len;i++){ for(int j=i-1;j>=0;j--){ if((numbers[i]+numbers[j])==target){ res.push_back(j+1); res.push_back(i+1); return res; } else if((numbers[i]+numbers[j])<target) break; } } return res; }};
0 0
- Leetcode167 - Two Sum II - Input array is sorted(two pointers)
- [LeetCode167]Two Sum II - Input array is sorted
- leetcode167. Two Sum II - Input array is sorted
- LeetCode167—Two Sum II - Input array is sorted
- Leetcode167. Two Sum II - Input array is sorted
- Two Sum II - Input array is sorted
- Two Sum II - Input array is sorted
- Two Sum II - Input array is sorted
- Two Sum II - Input array is sorted
- Two Sum II - Input array is sorted
- Two Sum II - Input array is sorted
- Two Sum II - Input array is sorted
- Two Sum II - Input array is sorted
- LeetCode167---Input array is sorted
- leetcode 167: Two Sum II - Input array is sorted
- Leetcode NO.167 Two Sum II - Input array is sorted
- LeetCode-Two Sum II - Input array is sorted
- [Leetcode]Two Sum II - Input array is sorted
- NDK各个版本链接
- Xcode8 新特性2
- 荆棘
- Python 爬虫笔记(获取整个站点中的所有外部链接)
- python使用jdbc连接phoenix
- leetcode167. Two Sum II - Input array is sorted
- android系统架构及源码目录结构
- IRP 处理流程
- secureCRT 7.3软件下载及破解工具+教程
- 人脸识别(活体检测)-使用敏识提供的第三方SDK
- AndroidStudio导入第三方库的一些问题
- 升高、降低gcc版本的简易方法
- Netty实现服务端客户端长连接通讯及心跳检测
- 手把手教你扩展个人微信号(2)