当前位置:网站首页>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[]{
};
}
}
边栏推荐
- 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
- It took me only 3 months to jump out of the comfort zone and become an automated test engineer for 5 years
- Jetpack之LiveData扩展MediatorLiveData
- [designmode] builder model
- 蓝桥杯单片机第四届省赛
- 蓝桥杯单片机省赛第八届
- 微信小程序 - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
- Blue Bridge Cup SCM digital tube skills
- Get started with Aurora 8b/10b IP core in one day (5) -- learn from the official routine of framing interface
- Failed to upgrade schema, error: “file does not exist
猜你喜欢
Pandora IOT development board learning (RT thread) - Experiment 1 LED flashing experiment (learning notes)
PR zero foundation introductory guide note 2
2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板,老板也有建设积分和捣乱积分, 排好队后,所有
【leetcode】74. Search 2D matrix
Jetpack之LiveData扩展MediatorLiveData
蓝桥杯单片机省赛第五届
【leetcode】34. Find the first and last positions of elements in a sorted array
跳出舒适区,5年点工转型自动化测试工程师,我只用了3个月时间
Realizing deep learning framework from zero -- Introduction to neural network
树莓派GPIO引脚控制红绿灯与轰鸣器
随机推荐
Blue Bridge Cup single chip microcomputer sixth temperature recorder
Analysis of the overall design principle of Nacos configuration center (persistence, clustering, information synchronization)
Oracle 常用SQL
Sorted out an ECS summer money saving secret, this time @ old users come and take it away
First acquaintance with string+ simple usage (II)
滴滴开源DELTA:AI开发者可轻松训练自然语言模型
SQL: common SQL commands
The first game of the 12th Blue Bridge Cup single chip microcomputer provincial competition
L'avènement de l'ère 5G, une brève discussion sur la vie passée et présente des communications mobiles
Wpviewpdf Delphi and Net PDF viewing component
SQL Yiwen get window function
初识string+简单用法(二)
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
[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!
0 foundation how to learn automated testing? Follow these seven steps step by step and you will succeed
C语言:逻辑运算和判断选择结构例题
[wireless image transmission] FPGA based simple wireless image transmission system Verilog development, matlab assisted verification
NLog use
High performance and low power cortex-a53 core board | i.mx8m Mini
XSS prevention