当前位置:网站首页>Sword finger offer II 006 Sort the sum of two numbers in the array
Sword finger offer II 006 Sort the sum of two numbers in the array
2022-07-02 03:58:00 【Ruthless young Fisherman】
subject
Given a has been according to Ascending order Array of integers for numbers , Please find out two numbers from the array, and the sum of them is equal to the target number target .
Functions should be length based 2 Returns the subscript values of the two numbers in the form of an array of integers .numbers The subscript from 0 Start counting , So the answer array should satisfy 0 <= answer[0] < answer[1] < numbers.length .
Suppose that there is only one pair of qualified numbers in the array , At the same time, a number cannot be used twice .
Example
Input :numbers = [1,2,4,6,10], target = 8
Output :[1,3]
explain :2 And 6 The sum is equal to the number of targets 8 . therefore index1 = 1, index2 = 3 .
Input :numbers = [2,3,4], target = 6
Output :[0,2]
Input :numbers = [-1,0], target = -1
Output :[0,1]
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/kLl5u1
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Method 1: Hashtable
class Solution {
public int[] twoSum(int[] numbers, int target) {
int n = numbers.length;
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
int a = target - numbers[i];
int b = numbers[i];
if(map.containsKey(a)) return new int[]{
map.get(a), i};
map.put(b, i);
}
return new int[]{
};
}
}
Method 2: Double pointer
The title is ascending array
Java Realization
class Solution {
public int[] twoSum(int[] numbers, int target) {
int n = numbers.length;
int l = 0, r = n - 1;
while (l < r) {
if (numbers[l] + numbers[r] == target) return new int[]{
l, r};
else if (numbers[l] + numbers[r] > target) r--;
else l++;
}
return new int[]{
};
}
}
边栏推荐
- 蓝桥杯单片机省赛第十二届第二场
- 0基础如何学习自动化测试?按照这7步一步一步来学习就成功了
- 【leetcode】81. Search rotation sort array II
- Qt插件之Qt Designer插件实现
- How to solve the code error when storing array data into the database
- The 6th Blue Bridge Cup single chip microcomputer provincial competition
- pip 安装第三方库
- SQL: common SQL commands
- 接口调试工具模拟Post上传文件——ApiPost
- Imageai installation
猜你喜欢
Basic operations of MySQL database (based on tables)
蓝桥杯单片机第四届省赛
How should the team choose the feature branch development mode or trunk development mode?
Realizing deep learning framework from zero -- Introduction to neural network
A thorough understanding of the development of scorecards - the determination of Y (Vintage analysis, rolling rate analysis, etc.)
Déchirure à la main - tri
pip 安装第三方库
蓝桥杯单片机省赛第七届
The second game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
C language: examples of logical operation and judgment selection structure
随机推荐
The fourth provincial competition of Bluebridge cup single chip microcomputer
Jetpack's livedata extension mediatorlivedata
Monkey test
Homework in Chapter 3 of slam course of dark blue vision -- derivative application of T6 common functions
QT designer plug-in implementation of QT plug-in
蓝桥杯单片机省赛第九届
近段时间天气暴热,所以采集北上广深去年天气数据,制作可视化图看下
The second game of the 12th provincial single chip microcomputer competition of the Blue Bridge Cup
ImageAI安装
Pandora IOT development board learning (HAL Library) - Experiment 2 buzzer experiment (learning notes)
整理了一份ECS夏日省钱秘籍,这次@老用户快来领走
Flutter中深入了解MaterialApp,常用属性解析
uni-app - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
5g era is coming in an all-round way, talking about the past and present life of mobile communication
First acquaintance with string+ simple usage (II)
[designmode] Prototype Pattern
潘多拉 IOT 开发板学习(RT-Thread)—— 实验1 LED 闪烁实验(学习笔记)
Account management of MySQL
Hand tear - sort
蓝湖的安装及使用