当前位置:网站首页>Leetcode/ sum of two numbers in a sorted array
Leetcode/ sum of two numbers in a sorted array
2022-07-28 08:33:00 【xcrj】
Code
package com.xcrj;
import java.util.Arrays;
/** * The finger of the sword Offer II 006. Sort the sum of two numbers in the array * 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 . */
public class Solution6 {
/** * target-numbers[i] Do a binary search ( Binary search ) * Premise , Ordered sequence , It is known that numbers[] Array sequence order */
public int[] twoSum1(int[] numbers, int target) {
for (int i = 0; i < numbers.length; i++) {
int idx = binarySearch(numbers, target - numbers[i], i + 1, numbers.length - 1);
// Binary search found the result
if (-1 != idx) {
return new int[]{
i, idx};
}
}
return new int[0];
}
/** * Two points search ( Binary search ) * The target value is equal to the intermediate value and returns the index * The target value is greater than the middle value. Look for the right half * The target value is less than the middle value. Look for the left half */
public int binarySearch(int[] numbers, int x, int start, int end) {
int i = start;
int j = end;
while (i <= j) {
int mid = (i + j) / 2;
if (x == numbers[mid]) return mid;
if (x > numbers[mid]) i = mid + 1;
else j = mid - 1;
}
return -1;
}
/** * Known ascending sequence * Double pointer i j Move in the opposite direction , Each move 1 A pointer to the */
public int[] twoSum2(int[] numbers, int target) {
int i = 0;
int j = numbers.length - 1;
while (i < j) {
int sum = numbers[i] + numbers[j];
if (sum == target) {
return new int[]{
i, j};
}
// Less than i Move right
if (sum < target) i++;
// Greater than j Move left
else {
j--;
}
}
return new int[0];
}
public static void main(String[] args) {
Solution6 solution6 = new Solution6();
System.out.println(Arrays.toString(solution6.twoSum2(new int[]{
1, 2, 4, 6, 10}, 8)));
}
}
Reference resources
author :tangweiqun
link :https://leetcode.cn/problems/kLl5u1/solution/jian-dan-yi-dong-javac-pythonjs-liang-sh-et4y/
source : Power button (LeetCode)
边栏推荐
- How to close the blocked program process?
- CarSim simulation quick start (XI) - Driver Model (1)
- Chairman tree review
- PostgreSQL is the world's most advanced open source relational database
- The even number of an integer queue is placed in the front, the odd number is placed in the back, and the relative position of the even and odd numbers remains unchanged
- Can a flinksql script write insert statements for two tables?
- h5机甲射击类小游戏源码下载
- js卡片层叠样式的图片切换js特效
- Find out whether the number exists from the matrix
- Deluxe H5 Tetris game source code
猜你喜欢

2022牛客多校第二场解题报告

Prescan quick start to master the transportation elements in lesson 14, prescan

Is the salary of test / development programmers unbalanced? Busy life, all kinds of job hopping

CarSim simulation quick start (10) - Modeling of braking system

@The role of documented

Recommend a fully open source, feature rich, beautiful interface mall system

Meituan Er Mian: why does redis have sentinels?

UE4 engine customizes screenpass and MRT output

Matlab file path

Solve the inherent defects of CNN! Common CNN architecture ccnn is coming | icml2022
随机推荐
SQL function
[Qt5] small software with 5 people randomly selected from the bid evaluation expert base
js卡通英文字母打字小游戏源码
Redis of non relational database [detailed setup of redis cluster]
Day112.尚医通:手机验证码登录功能
[leetcode] 24. Exchange nodes in the linked list in pairs
XSS knowledge points and 20 character short domain name bypass
Get the clicked line number in qtablewidget
No super high-rise buildings | new regulations: what information does it reveal that no new buildings above 500 meters should be built?
Can‘t connect to server on ‘IP‘ (60)
js信息提示框定时关闭
UE4 engine customizes screenpass and MRT output
解决EMC、EMI传导干扰的八大方法
Es6: template string
PostgreSQL is the world's most advanced open source relational database
一篇文章搞懂数据仓库:元数据分类、元数据管理
How to use QT help documents
2022牛客多校第二场解题报告
CarSim simulation quick start (XI) - Driver Model (1)
CarSim simulation quick start (10) - Modeling of braking system