1、题目

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

Subscribe to see which companies asked this question.

2、代码实现

public class Solution {public int[] twoSum(int[] nums, int target) {if (null == nums)  return null;         int[] result = new int[2];  HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();  for (int i = 0; i < nums.length; ++i) {  Integer value = map.get(nums[i]);  if (null == value)   map.put(nums[i], i);  Integer index = map.get(target - nums[i]);  if (null != index && index < i)  {  result[0] = index + 1;  result[1] = i + 1;  }  }  return result;  }
}

LeetCode之Two Sum II - Input array is sorted相关推荐

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

  2. leetcode 167 Two Sum II - Input array is sorted

    给定一个有序的数组,和一个整数目标,求两个数的和等于目标的索引,索引从1开始.假设必定存在解. 有两种思路: 直接找: vector<int> twoSum(vector<int&g ...

  3. LeetCode 167. Two Sum II - Input array is sorted--Python解法

    题目地址:Two Sum II - Input array is sorted - LeetCode Given an array of integers that is already sorted ...

  4. C#LeetCode刷题之#167-两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3903 访问. 给定一个已按照升序排列 的有序数组,找到两个数使得 ...

  5. leetcode python3 简单题167. Two Sum II - Input array is sorted

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百六十七题 (1)题目 英文: Given an array of intege ...

  6. [LeetCode By Python]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 ...

  7. 【LeetCode 剑指offer刷题】数组题2:57 有序数组中和为s的两个数(167 Two Sum II - Input array is sorted)...

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 57 有序数组中和为s的两个数 题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是 ...

  8. 167. Two Sum II - Input array is sorted (C, C++, Python)

    本文讲述了Array类中第167个问题的几种解法,实现语言包括C,Python以及C++. 问题: Given an array of integers that is already sorted ...

  9. LeetCode167 | Two Sum II - Input array is sorted (Easy)

    题目地址(167. 两数之和 II - 输入有序数组) https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/ 题目描述 ...

最新文章

  1. 转载 iOS js oc相互调用(JavaScriptCore) --iOS调用js
  2. hive sqoop导出 postgresql精度丢失_Mysql 与 hadoop 数据同步(迁移),你需要知道 Sqoop...
  3. 美国远程MBA全面占领MBA市场
  4. Python笔记之读取yaml文件
  5. DE20 Derivative Formulas
  6. 【数学建模】基于matlab无线可充电传感器网络充电路线规划【含Matlab源码 750期】
  7. 数学之路-数据分析进阶-转化率
  8. 美团笔试题及解析(时间:2022年9月3号)
  9. 单位元转换万元怎么转_excel教程:将单位为元的excel数值转换成单位为万元的数值...
  10. 桌面计算机图标不删除,电脑小技巧:教你删除桌面无法删除的图标
  11. android日历订阅,Android日历.
  12. vlan的理解以及华为access、trunk、hybrid三种链路对比
  13. DevExpress XtraReport报表开发相关知识点
  14. 计算机时代杂志好吗,计算机时代是什么级别的刊物
  15. SpeechSynthesisUtterance实现语音播报
  16. python将txt转为字符串_Python玩转《生僻字》
  17. Verilog设计流程:综合(一)
  18. 添加商务通的几种方式
  19. 移动运营商ipcc文件_如何在苹果官网提取IPCC文件?
  20. networker对mysql备份_【经验分享】EMC NetWorker数据库备份及异机恢复

热门文章

  1. ABP Framework V4.4 RC 新增功能介绍
  2. .NET 6 Preview5+VS2022实战千万并发秒杀项目,帅爆了(附源码)
  3. 龙芯3A5000完成流片 同主频性能追平AMD Zen1
  4. IComparer与IEqualityComparer的简单使用
  5. 2021年了,`IEnumerator`、`IEnumerable`接口还傻傻分不清楚?
  6. 使用 ML.NET 识别乐高颜色块
  7. 进击吧! Blazor !第三期 信息交互
  8. 以正确的方式下载和配置 ASP.NET Core 官方源码
  9. 代码演示C#各版本新功能
  10. .NET Core 3.0预览版7中的ASP.NET Core和Blazor更新