当前位置:网站首页>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[]{
};
}
}

边栏推荐
- 蓝桥杯单片机省赛第十二届第一场
- Delete the code you wrote? Sentenced to 10 months!
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验2 蜂鸣器实验(学习笔记)
- How should the team choose the feature branch development mode or trunk development mode?
- 蓝桥杯单片机省赛第十一届第一场
- Analysis of the overall design principle of Nacos configuration center (persistence, clustering, information synchronization)
- Didi open source Delta: AI developers can easily train natural language models
- First acquaintance with string+ simple usage (II)
- 树莓派GPIO引脚控制红绿灯与轰鸣器
- 蓝桥杯单片机省赛第八届
猜你喜欢

First acquaintance with P4 language

u本位合约爆仓清算解决方案建议

PR zero foundation introductory guide note 2

BiShe cinema ticket purchasing system based on SSM

2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee

Jetpack's livedata extension mediatorlivedata

It took me only 3 months to jump out of the comfort zone and become an automated test engineer for 5 years

Recently, the weather has been extremely hot, so collect the weather data of Beijing, Shanghai, Guangzhou and Shenzhen last year, and make a visual map

Sorted out an ECS summer money saving secret, this time @ old users come and take it away

MD5 of Oracle
随机推荐
First acquaintance with P4 language
Go function
Monkey测试
【无线图传】基于FPGA的简易无线图像传输系统verilog开发,matlab辅助验证
Go language introduction
跳出舒适区,5年点工转型自动化测试工程师,我只用了3个月时间
The 7th Blue Bridge Cup single chip microcomputer provincial competition
The first game of the 12th Blue Bridge Cup single chip microcomputer provincial competition
SQL Yiwen get window function
[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
软件测试人的第一个实战项目:web端(视频教程+文档+用例库)
Which product of anti-cancer insurance is better?
Qt插件之Qt Designer插件实现
Get started with Aurora 8b/10b IP core in one day (5) -- learn from the official routine of framing interface
树莓派GPIO引脚控制红绿灯与轰鸣器
[untitled]
In wechat applet, the externally introduced JS is used in xwml for judgment and calculation
Welcome the winter vacation multi school league game 2 partial solution (B, C, D, F, G, H)
How should the team choose the feature branch development mode or trunk development mode?
《西线无战事》我们才刚开始热爱生活,却不得不对一切开炮