当前位置:网站首页>[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];
}
}
边栏推荐
- [use of pointer and pointer and array]
- SQL必需掌握的100个重要知识点:使用游标
- Plastic floating dock Industry Research Report - market status analysis and development prospect forecast
- 关于测试用例
- Blue Bridge Cup Eliminate last one (bit operation, code completion)
- How to prevent your jar from being decompiled?
- treevalue——Master Nested Data Like Tensor
- [shutter] shutter layout component (physicalmodel component)
- MySQL learning record (7)
- China's crude oil heater market trend report, technological innovation and market forecast
猜你喜欢

Analysis of neural network

【零基础一】Navicat下载链接

Blue Bridge Cup Winter vacation homework (DFS backtracking + pruning)
![[shutter] shutter layout component (wrap component | expanded component)](/img/a7/824a990235fc5ce67841ebdcf001fb.jpg)
[shutter] shutter layout component (wrap component | expanded component)

Baidu sued a company called "Ciba screen"

Go language learning summary (5) -- Summary of go learning notes

In depth research and investment feasibility report of global and Chinese isolator industry, 2022-2028

Read a doctor, the kind that studies cows! Dr. enrollment of livestock technology group of Leuven University, milk quality monitoring

Huawei Hongmeng watch achieves fireworks display effect on New Year's Eve

How is LinkedList added?
随机推荐
One week dynamics of dragon lizard community | 2.07-2.13
Etcd Raft 协议
Construction and maintenance of business website [5]
How to test the process of restoring backup files?
Redis distributed lock failure, I can't help but want to burst
Research Report on market supply and demand and strategy of China's plastic trunking industry
VIM command-t plugin error: unable to load the C extension - VIM command-t plugin error: could not load the C extension
Market trend report, technical innovation and market forecast of China's Micro pliers
Research Report on minimally invasive medical robot industry - market status analysis and development prospect prediction
如何访问kubernetes API?
Gbase8s database type
基本IO接口技术——微机第七章笔记
MySQL inserts Chinese data and reports an error. Set the default collation
~90z axis translation
发现你看不到的物体!南开&武大&ETH提出用于伪装目标检测SINet,代码已开源!...
Pyqt picture decodes and encodes and loads pictures
pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform
Five message formats of OSPF
rwctf2022_ QLaaS
Three chess games