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 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
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
vector<int> res;
map<int, int> exist,index;
for(int i=0; i<numbers.size(); i++){
exist[numbers[i]]++;
index[numbers[i]]=i;
}
for(int i=0; i<numbers.size(); i++)
if(target-numbers[i]==numbers[i]&&exist[numbers[i]]>=2){
res.push_back(i+1);
res.push_back(i+2);
break;
}
else if(exist[target-numbers[i]]==1&&numbers[i]*2!=target){
res.push_back(i+1);
res.push_back(index[target-numbers[i]]+1);
break;
}
return res;
}
};
LeetCode 167. Two Sum II – Input array is sorted的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- (双指针 二分) 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 ...
- LeetCode 167 Two Sum II - Input array is sorted
Problem: Given an array of integers that is already sorted in ascending order, find two numbers such ...
- ✡ 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 ...
- 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 ...
- 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 ...
- 167. Two Sum II - Input array is sorted - LeetCode
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
随机推荐
- java String类为什么是final的
1.为了安全 java 必须借助操作系统本身的力量才能做事,jdk提供的很多核心类比如String,这类内的很多方法 都不是java编程语言本身编写的,很多方法都是调用操作系统本地的api,如果被继承 ...
- 员工管理系统(集合与IO流的结合使用 beta2.0 ObjectInputStream/ ObjectOutputStream)
package cn.employee; import java.io.Serializable; public class Employee implements Serializable{ pri ...
- 转-关于UIView的autoresizingMask属性的研究
在 UIView 中有一个autoresizingMask的属性,它对应的是一个枚举的值(如下),属性的意思就是自动调整子控件与父控件中间的位置,宽高. 1 2 3 4 5 6 7 8 9 enum ...
- ORA-28002错误原因及解决办法
在oracle database 11g中,默认在default概要文件中设置了“PASSWORD_LIFE_TIME=180天”所导致.密码过期后,业务进程连接数据库异常,影响业务使用.数据库密码过 ...
- easy ui combotree的操作
1.获取combotree的选中值 $("#id").combotree("getValue"); 2.设置combotree的选中值 $('#id').com ...
- 关于java的arrays数组排序示例AJPFX的分享
Java API对Arrays类的说明是:此类包含用来操作数组(比如排序和搜索)的各种方法. 1.对基本数据类型的数组的排序 说明: (1)Arrays类中的sort()使用的是“经过调优的快速排序法 ...
- koa2实现简易的webpack-dev-server
koa2实现简易的webpack-dev-server热更新 原文https://github.com/zhuangZhou/Blog/issues/3 闲来无事,用koa2撸了一个简易的webpac ...
- sql 关键字的用法
coalesce( T.GoodsCode,'0') 若 T.GoodsCode 为NULL 这 用0替换 round(S.SaleEarning,2) 保留两位小数 SUBSTRING(zb.acc ...
- 【Python】第一个爬虫
import urllib.request import re class DownPic: def __init__(self,url,re_str): self.url = url self.re ...
- vb,wps,excel 提取括号的数字
Sub 抽离数字() Dim hang Range("h1").Select Columns("E:F").Select Selection.Clear Ran ...