167 Two Sum II - Input array is sorted 两数之和 II - 输入有序数组
给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。
函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2。请注意,返回的下标值(index1 和 index2)不是从零开始的。
你可以假设每个输入都只有一个解决方案,而且你不会重复使用相同的元素。
输入:数组 = {2, 7, 11, 15}, 目标数 = 9
输出:index1 = 1, index2 = 2
详见:https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/
Java实现:
方法一:
class Solution {
public int[] twoSum(int[] nums, int target) {
int n=nums.length;
int[] res=new int[2];
if(n<2||nums==null){
return null;
}
int l=0;
int r=n-1;
while(l<r){
int sum=nums[r]+nums[l];
if(sum==target){
res[0]=l+1;
res[1]=r+1;
return res;
}else if(sum<target){
++l;
}else{
--r;
}
}
return res;
}
}
方法二:
class Solution {
public int[] twoSum(int[] nums, int target) {
int n=nums.length;
int[] res=new int[2];
if(n<2||nums==null){
return null;
}
Map<Integer,Integer> m=new HashMap<Integer,Integer>();
for(int i=0;i<n;++i){
if(m.containsKey(target-nums[i])){
res[0]=m.get(target-nums[i])+1;
res[1]=i+1;
return res;
}else{
m.put(nums[i],i);
}
}
return res;
}
}
167 Two Sum II - Input array is sorted 两数之和 II - 输入有序数组的更多相关文章
- [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 ...
- 167. Two Sum II - Input array is sorted两数之和
1. 原始题目 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明 ...
- [LeetCode] 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] 653. Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
- [LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
- 167. Two Sum II - Input array is sorted【Easy】【双指针-有序数组求两数之和为目标值的下标】
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- Leetcode653.Two Sum IV - Input is a BST两数之和4-输入BST
给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定的目标结果,则返回 true. struct TreeNode { int val; struct TreeNode * ...
- 167两数之和II-输入有序数组
from typing import List# 这道题很容易能够想到,只需要遍历两边列表就可以了# 两层循环class Solution: def twoSum(self, numbers: Lis ...
- 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 ...
随机推荐
- ivy
ivy https://ant.apache.org/ivy/ Apache Ivy™ is a popular dependency manager focusing on flexibility ...
- redis06----消息订阅
使用办法: 订阅端: Subscribe 频道名称 发布端: publish 频道名称 发布内容 r1:>publish news "aaaaaa" "" ...
- POJ3660 Cow Contest —— Floyd 传递闭包
题目链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- MYSQL进阶学习笔记五:MySQL函数的创建!(视频序号:进阶_13)
知识点六:MySQL函数的创建(13) 内置函数: 自定义函数: 首先查看是否已经开启了创建函数的功能: SHOW VARIABLES LIKE ‘%fun%’; 如果变量的值是OFF,那么需要开启 ...
- codeforces 435 B. Pasha Maximizes 解题报告
题目链接:http://codeforces.com/problemset/problem/435/B 题目意思:给出一个最多为18位的数,可以通过对相邻两个数字进行交换,最多交换 k 次,问交换 k ...
- VC++读写文件
目录 第1章读写文件 1 1.1 API 1 1.2 低级IO 1 1.2.1 文件序号 1 1.2.2 文本文件与二进制文件 1 1.3 流IO 2 1.4 Un ...
- 「LuoguP1145」 约瑟夫(打表
Description n 个人站成一圈,从某个人开始数数,每次数到 m 的人就被杀掉,然后下一个人重新开始数,直到最后只剩一个人.现在有一圈人, k 个好人站在一起, k 个坏人站在一起.从第一个好 ...
- 使用putty连接虚拟机上的centos提示Network:connection refused
转自:https://yeyuan.iteye.com/blog/1266484 今天早上开机之后,像往常一样使用putty连接linux的时候,突然提示Network:connection refu ...
- VS2008 MFC截取整个屏幕并保存为jpg格式
void CMainFrame::OnSavejpg() { // TODO: 在此添加命令处理程序代码 HWND hwnd = this->GetSafeHwnd(); //得到窗口句柄 HD ...
- ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 14. ASP.NET Core Identity 入门
默认的身份认证好授权系统 UserManager用来操作用户的类, Singi用来身份认证的 添加AccountController 先声明SignInManager和UserManager这两个服务 ...