当前位置:网站首页>[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];
}
}
边栏推荐
- One week dynamics of dragon lizard community | 2.07-2.13
- Research Report on market supply and demand and strategy of China's atomic spectrometer industry
- MySQL learning record (3)
- [shutter] shutter layout component (opacity component | clipprect component | padding component)
- D4:非成对图像去雾,基于密度与深度分解的自增强方法(CVPR 2022)
- Research Report on market supply and demand and strategy of China's Plastic Geogrid industry
- MySQL inserts Chinese data and reports an error. Set the default collation
- B.Odd Swap Sort(Codeforces Round #771 (Div. 2))
- 关于测试用例
- Construction and maintenance of business website [3]
猜你喜欢
Hot backup routing protocol (HSRP)
[zero foundation I] Navicat download link
MySQL learning record (7)
[shutter] statefulwidget component (bottom navigation bar component | bottomnavigationbar component | bottomnavigationbaritem component | tab switching)
[shutter] shutter layout component (wrap component | expanded component)
Today, I met a Alipay and took out 35K. It's really sandpaper to wipe my ass. it's a show for me
How does esrally perform simple custom performance tests?
[shutter] statefulwidget component (create statefulwidget component | materialapp component | scaffold component)
[CV] Wu Enda machine learning course notes | Chapter 12
Blue Bridge Cup Winter vacation homework (DFS backtracking + pruning)
随机推荐
[shutter] shutter layout component (fractionallysizedbox component | stack layout component | positioned component)
Cardinality sorting (detailed illustration)
Technical solution of vision and manipulator calibration system
Analysis of enterprise financial statements [1]
[shutter] shutter page Jump (route | navigator | page close)
Analysis of enterprise financial statements [2]
Analysis of enterprise financial statements [3]
VIM command-t plugin error: unable to load the C extension - VIM command-t plugin error: could not load the C extension
Cloud computing technology [1]
Market trend report, technical innovation and market forecast of China's Micro pliers
Plastic floating dock Industry Research Report - market status analysis and development prospect forecast
*C语言期末课程设计*——通讯录管理系统(完整项目+源代码+详细注释)
Analysis of enterprise financial statements [4]
China microporous membrane filtration market trend report, technological innovation and market forecast
How does esrally perform simple custom performance tests?
PIP version update timeout - download using domestic image
MySQL learning notes (Advanced)
[shutter] shutter layout component (opacity component | clipprect component | padding component)
Today, I met a Alipay and took out 35K. It's really sandpaper to wipe my ass. it's a show for me
VictoriaMetrics 简介