当前位置:网站首页>Leetcode advanced road - 167 Sum of two numbers II - input ordered array
Leetcode advanced road - 167 Sum of two numbers II - input ordered array
2022-06-10 21:22:00 【Li_ XiaoJin】
Given that one is in ascending order Ordered array of , Find two numbers so that the sum of them is equal to the target number .
The function should return these two subscript values index1 and index2, among index1 Must be less than index2.
explain :
Subscript value returned (index1 and index2) Not from scratch .
You can assume that each input only corresponds to a unique answer , And you can't reuse the same elements .
Example :
Input : numbers = [2, 7, 11, 15], target = 9
Output : [1,2]
explain : 2 And 7 The sum is equal to the number of targets 9 . therefore index1 = 1, index2 = 2 .
Both solutions to this problem are common , Learn to draw inferences from one example .
- 1、 Double finger needling Define the first and last two pointers , Add the values of two , If the number obtained is greater than the target number , Then the tail pointer minus one ; If the number obtained is less than the target number , Then the header pointer plus 1. If the obtained number is equal to the target number, it directly returns two subscripts plus 1.
- 2、 Dichotomy search Binary search is to set a number first , And then binary search the following numbers , Check whether it meets the requirements .
Official explanation : Find two numbers in the array , Make their sum equal to the target value , You can fix the first number first , Then look for the second number , The second number is equal to the difference between the target value minus the first number . Using the ordered nature of arrays , You can find the second number by binary search . In order to avoid repeated search , When looking for the second number , Look only to the right of the first number .
/**
* 167. Sum of two numbers II - Input an ordered array
* @Author: lixj
* @Date: 2020/10/9 14:25
*/
public class TwoSumIIInputarrayissorted {
/**
* Double finger needling
* @param numbers
* @param target
* @return
*/
public int[] twoSum(int[] numbers, int target) {
int i = 0;
int j = numbers.length-1;
while (i < j) {
int sum = numbers[i] + numbers[j];
if (sum > target) {
j--;
} else if (sum < target) {
i++;
} else {
return new int[]{i+1, j+1};
}
}
return new int[]{-1, -1};
}
/**
* Dichotomy search
* @param numbers
* @param target
* @return
*/
public int[] twoSum1(int[] numbers, int target) {
for (int i = 0; i < numbers.length; ++i) {
int low = i + 1;
int high = numbers.length -1;
while (low <= high) {
int mid = (high - low) /2 + low;
if (numbers[i] + numbers[mid] < target) {
low = mid + 1;
} else if (numbers[i] + numbers[mid] > target) {
high = mid - 1;
} else {
return new int[]{i+1, mid+1};
}
}
}
return new int[]{-1, -1};
}
public static void main(String[] args) {
int[] nums = new int[]{2, 7, 11, 13, 14};
TwoSumIIInputarrayissorted test = new TwoSumIIInputarrayissorted();
int[] a = test.twoSum1(nums, 10);
for (int i = 0; i < 2; i++) {
System.out.print(a[i] + " ");
}
}
}
Copyright: use Creative Commons signature 4.0 International license agreement to license Links:https://lixj.fun/archives/167 Sum of two numbers ii- Input an ordered array
边栏推荐
- Leetcode advanced road - plus one
- Brute force method /k integers out of 1~n integers
- Redis缓存穿透
- LeetCode:497. 非重叠矩形中的随机点————中等
- LeetCode 进阶之路 - 删除排序数组中的重复项
- leetcode 划分数组使最大差为 K
- 六级考试-商务英语-考前最后一背
- LeetCode:497. Random points in non overlapping rectangles -- medium
- Redis cache avalanche
- 自注意力(self-attention)和多头注意力(multi-head attention)
猜你喜欢

【生成对抗网络学习 其一】经典GAN与其存在的问题和相关改进

Calculus review 1

Error code 1129, state HY000, host 'xxx' is blocked because of many connection errors

What should be paid attention to when designing Multilayer PCB?

魔塔类游戏实现源码及关卡生成

KCon 2022 议题大众评选火热进行中!不要错过“心仪”的议题哦~

分布式服务理论基础

标配双安全气囊,价格屠夫长安Lumin 4.89万起售

面试必备——synchronized底层原理的基础知识

Pytorch deep learning -- neural network convolution layer conv2d
随机推荐
Monitoring is easy to create a "quasi ecological" pattern and empower Xinchuang to "replace"
Connexion MySQL errorcode 1129, State hy000, Host 'xxx' is Blocked because of many Connection Errors
Self attention and multi head attention
Lengsuanling, a 30-year tortuous history of IPO of a domestic brand
Practical | how to use burp suite for password blasting!
^29 event cycle model
【无标题】破目
Node (express) implements interfaces such as adding, deleting, modifying, and paging
你的公司会选择开发数据中台吗?
App test case
LeetCode 进阶之路 - 反转字符串
^30h5 web worker multithreading
Construction of RT thread smart win10 64 bit compilation environment
Read the source code of micropyton - add the C extension class module (2)
Power set V4 recursion of brute force method /1~n
Read the source code of micropyton - add the C extension class module (3)
Redis cache breakdown
Will your company choose to develop data center?
ros虚拟时间
Obtained network time + time zone (+8)