给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。
函数应该返回这两个下标值 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 - 输入有序数组的更多相关文章

  1. [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 ...

  2. 167. Two Sum II - Input array is sorted两数之和

    1. 原始题目 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. 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 ...

  7. Leetcode653.Two Sum IV - Input is a BST两数之和4-输入BST

    给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定的目标结果,则返回 true. struct TreeNode { int val; struct TreeNode * ...

  8. 167两数之和II-输入有序数组

    from typing import List# 这道题很容易能够想到,只需要遍历两边列表就可以了# 两层循环class Solution: def twoSum(self, numbers: Lis ...

  9. 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 ...

随机推荐

  1. 对soc-audio体系snd_soc_machine和snd_soc_dai_link简单理解

    ASOC (ALSA system on chip)              // 主要为嵌入式系统专门开发的sound管理体系结构[luther.gliethttp].Digital Audio ...

  2. iOS NSString拼接字符串

    NSString* str_C; // 结果字符串NSString* str_A, str_B; //已存在的字符串,需要将str_A和str_B连接起来 //方法1 str_C = [NSStrin ...

  3. opencv直方图该怎么画

    图像直方图是反映图像中像素分布特性的统计表,一般显示如下: 其中横坐标代表的是图像像素的种类,或者说是灰度级,纵坐标代表的是每一级灰度下像素数或者该灰度级下像素数在所有图像总像素数总所占的百分比. 直 ...

  4. OpenMediaVault Redmine 安装

    /******************************************************************** * OpenMediaVault Redmine 安装 * ...

  5. 区间DP中的环形DP

    vijos1312 链接:www.vijos.org/p/1312 题目分析:经典的环形DP(区间DP) 环形DP,首先解环过程,把数组复制一遍,n个数变成2n个数,从而实现解环 dp[i][j]表示 ...

  6. 【SDOI2009】SuperGCD

    [题目链接] 点击打开链接 [算法] 1.关于求最大公约数的算法 若使用辗转相除法,那么显然会超时 不妨这样思考 : 要求gcd(a,b), 若a为偶数,b为偶数,则gcd(a,b) = 2 * gc ...

  7. javascript之数组的6种去重方法

    去重 var arr=[11,11,333,4,4,5,66,66,7]; // 方法一:在新数组内判断不存在时加入 var newarr1=[]; function quchong1(){ for( ...

  8. javaScript的几个问题简答

    1.javascript的typeof返回哪些数据类型    Object.number. function. boolean. underfind 2.例举3种强制类型转换和2种隐式类型转换?    ...

  9. Backbone.js之model篇(一)

    Backbone.js之model篇(一) Backbone 是一个前端 JS 代码 MVC 框架,它不可取代 Jquery,不可取代现有的 template 库.而是和这些结合起来构建复杂的 web ...

  10. 6-9 Haar+adaboost人脸识别

    我们重点分析了Haar特征的概念以及如何计算Haar特征,并介绍了Haar+Adaboost分类器它们的组合以及Adaboost分类器如何使用和训练.这节课我们将通过代码来实现一下Haar+Adabo ...