给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。
函数应该返回这两个下标值 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. 文件宝iOS/iPhone/iPad客户端简介

    App Store地址:https://itunes.apple.com/cn/app/id1023365565?mt=8 文件宝-装机必备的文件管家,专业的rar-zip 解压工具,局域网看片神器, ...

  2. Hibernate commit() 和flush() 的区别

    <<精通Hibernate java对象持久化技术详解>> ,flush()方法进行清理缓存的操作,执行一系列的SQL语句,但不会提交事务;commit()方法会先调用flus ...

  3. 记录001:AS11 BAPI

    未知元素(174657434)  15:05:41AS11有没有BAPI呀?有做过的吗 BAPI_FIXEDASSET_OVRTAKE_CREATE

  4. spring cloud-服务注册

    正常的服务模块,注册到注册中心,让别的服务发现,调用服务 创建“服务提供方” 下面我们创建提供服务的客户端,并向服务注册中心注册自己. 假设我们有一个提供计算功能的微服务模块,我们实现一个RESTfu ...

  5. Codeforces Beta Round #25 (Div. 2 Only)D. Roads not only in Berland

    D. Roads not only in Berland time limit per test 2 seconds memory limit per test 256 megabytes input ...

  6. HDU2612 Find a way —— BFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Time Limit: 3000/1000 MS (Java/Others ...

  7. 计算机学院大学生程序设计竞赛(2015’12)The Magic Tower

    The Magic Tower Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. 一步一步学Silverlight 2系列(25):综合实例之Live Search

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  9. WAS:Thread "server.startup : 1" (00000020) and may be hung异常

    有现场server启动时,启动不了,后台报错如下: [// ::: CST] ThreadMonitor W WSVR0605W: Thread ) has been active milliseco ...

  10. 查看JVM运行时堆内存

    利用jmap和MAT等工具查看JVM运行时堆内存 https://www.cnblogs.com/cjsblog/p/9561375.html jmap JDK自带了一些工具可以帮助我们查看JVM运行 ...