当前位置:网站首页>[Jianzhi offer] 57 And are two numbers of S
[Jianzhi offer] 57 And are two numbers of S
2022-07-02 21:44:00 【LuZhouShiLi】
The finger of the sword Offer 57. And for s Two numbers of
subject
Enter an ascending array and a number s, Find two numbers in an array , So that their sum is exactly s. If the sum of many pairs of numbers is equal to s, Then output any pair .
Ideas
- Double pointer , Set the pointer i,j Point to respectively nums The left and right ends of the array
- Calculation s = target[i] + target[j]
- If s > target So the pointer j Move to the left j = j - 1
- If s < target So the pointer i To the right i = i + 1
- s = target Return the array directly [nums[i],nums[j]]
Code
class Solution {
public int[] twoSum(int[] nums, int target) {
int i = 0,j = nums.length - 1;
while(i < j)
{
int s = nums[i] + nums[j];
if(s < target)
{
i++;
}
else if(s > target)
{
j--;
}
else{
return new int[] {
nums[i],nums[j]};
}
}
return new int[0];
}
}
边栏推荐
- Construction and maintenance of business website [5]
- Market trend report, technical innovation and market forecast of China's Micro pliers
- Five message formats of OSPF
- System (hierarchical) clustering method and SPSS implementation
- *C语言期末课程设计*——通讯录管理系统(完整项目+源代码+详细注释)
- Research Report on right-hand front door industry - market status analysis and development prospect forecast
- Construction and maintenance of business websites [4]
- 3DES (deSede) encryption CBC mode pkcs7padding filling Base64 encoding key 24byte iv8byte
- PIP audit: a powerful security vulnerability scanning tool
- MySQL learning record (4)
猜你喜欢
如何防止你的 jar 被反编译?
Redis distributed lock failure, I can't help but want to burst
Investment strategy analysis of China's electronic information manufacturing industry and forecast report on the demand outlook of the 14th five year plan 2022-2028 Edition
Basic IO interface technology - microcomputer Chapter 7 Notes
Unexpectedly, there are such sand sculpture code comments! I laughed
D4:非成对图像去雾,基于密度与深度分解的自增强方法(CVPR 2022)
Redis分布式锁故障,我忍不住想爆粗...
[shutter] shutter layout component (wrap component | expanded component)
Spend more time with your computer on this special holiday, HHH
[shutter] statefulwidget component (create statefulwidget component | materialapp component | scaffold component)
随机推荐
Research Report on right-hand front door industry - market status analysis and development prospect forecast
China microporous membrane filtration market trend report, technological innovation and market forecast
Go language learning summary (5) -- Summary of go learning notes
Read a doctor, the kind that studies cows! Dr. enrollment of livestock technology group of Leuven University, milk quality monitoring
Cloud computing technology [2]
Structure array, pointer and function and application cases
Today, I met a Alipay and took out 35K. It's really sandpaper to wipe my ass. it's a show for me
SQL必需掌握的100个重要知识点:管理事务处理
Free open source web version of xshell [congratulations on a happy new year]
MySQL learning record (5)
[shutter] shutter layout component (fractionallysizedbox component | stack layout component | positioned component)
Welfare, let me introduce you to someone
Find objects you can't see! Nankai & Wuhan University & eth proposed sinet for camouflage target detection, and the code has been open source
基本IO接口技术——微机第七章笔记
[shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)
加了定位的文字如何水平垂直居中
【剑指 Offer】56 - I. 数组中数字出现的次数
[shutter] statefulwidget component (pageview component)
MySQL learning record (4)
Report on investment development and strategic recommendations of China's vibration isolator market, 2022-2027