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

方法一:

  1. class Solution {
  2. public int[] twoSum(int[] nums, int target) {
  3. int n=nums.length;
  4. int[] res=new int[2];
  5. if(n<2||nums==null){
  6. return null;
  7. }
  8. int l=0;
  9. int r=n-1;
  10. while(l<r){
  11. int sum=nums[r]+nums[l];
  12. if(sum==target){
  13. res[0]=l+1;
  14. res[1]=r+1;
  15. return res;
  16. }else if(sum<target){
  17. ++l;
  18. }else{
  19. --r;
  20. }
  21. }
  22. return res;
  23. }
  24. }

方法二:

  1. class Solution {
  2. public int[] twoSum(int[] nums, int target) {
  3. int n=nums.length;
  4. int[] res=new int[2];
  5. if(n<2||nums==null){
  6. return null;
  7. }
  8. Map<Integer,Integer> m=new HashMap<Integer,Integer>();
  9. for(int i=0;i<n;++i){
  10. if(m.containsKey(target-nums[i])){
  11. res[0]=m.get(target-nums[i])+1;
  12. res[1]=i+1;
  13. return res;
  14. }else{
  15. m.put(nums[i],i);
  16. }
  17. }
  18. return res;
  19. }
  20. }

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. 设计模式学习笔记——Template Method模板方法模式

    可能是最简单的设计模式. 而且你我都用过而不自知. 因为,模板方法模式也者,就是面向对象中的继承.公用部分放在父类,子类继承父类,然后扩展.呵呵.

  2. Sequelize入门一

    最近刚开始接触Sequelize,当中遇到不少坑,所以想写篇Sequelize入门和大家分享,避免有道友和我一样爬坑. 学习sequelize的初衷是想解决SQL注入,它支持MySQL, SQLite ...

  3. DDD领域建模基本流程

    整理一个精简的DDD领域建模基本流程,供大家在DDD领域建模实践中进行参考. 搜集用户故事(用户的原始需求) 整理用户故事,抽出用例(用例表达了用户对系统的需求,定义了系统的边界以及系统外部角色和系统 ...

  4. (C)字节对齐#pragma pack()

    1. 为什么要进行对齐 对于结构体,编译器会自动进行成员变量对齐处理,是为了提高运算效率. 缺省情况下是自然对齐方式. 2. 自然对齐 即默认对齐,按照结构体的成员中size最大的成员进行对齐. 例: ...

  5. 秒懂单链表及其反转(reverse)

    什么是链表,这种数据结构是由一组Node组成的,这群Node一起表示了一个序列.链表是最普通,最简单的数据结构(物理地址不连续),它是实现其他数据结构如stack, queue等的基础. 链表比起数组 ...

  6. bootstrap的学习注意点

    1.bootstrop里面所有的内容都需要用一个container 容器包裹起来: 2.一屏二屏什么的,是通过id 与href实现的: 3.下拉与菜单之类的都有固定的代码: 4.需要修改相关属性的话, ...

  7. mongodb mongod 参数解释

    基本配置----------------------------------------------------------------------------------quiet # 安静输出-- ...

  8. Microsoft.XMLHTTP基本用法

    客户端调用XMLHTTP的过程很简单,只有5个步骤:1.创建XMLHTTP对象2.打开与服务端的连接,同时定义指令发送方式,服务网页(URL)和请求权限等.客户端通过Open命令打开与服务端的服务网页 ...

  9. 使用TextView实现跑马灯的效果

    1.定义textView标签的4个属性: android:singleLine="true"//使其只能单行 android:ellipsize="marquee&quo ...

  10. windows下运行swoole搭建环境

    swoole windows环境搭建 swoole框架是一个很神奇很厉害的框架,它弥补了PHP的本身的一些不足之处.其实swoole确切的说是一个使用C语言编写的PHP扩展,并且这个扩展不能够在win ...