当前位置:网站首页>【剑指 Offer】57. 和为s的两个数字
【剑指 Offer】57. 和为s的两个数字
2022-07-02 21:32:00 【LuZhouShiLi】
剑指 Offer 57. 和为s的两个数字
题目
输入一个递增排序的数组和一个数字s,在数组中查找两个数,使得它们的和正好是s。如果有多对数字的和等于s,则输出任意一对即可。
思路
- 双指针,设置指针i,j分别指向nums数组的左右两端
- 计算s = target[i] + target[j]
- 如果s > target 那么指针j向左移动 j = j - 1
- 如果s < target 那么指针i向右移动 i = i + 1
- s = target 直接返回数组[nums[i],nums[j]]
代码
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 websites [6]
- Research Report on micro vacuum pump industry - market status analysis and development prospect prediction
- Import a large amount of data to redis in shell mode
- How to test the process of restoring backup files?
- Research Report on crude oil tanker industry - market status analysis and development prospect forecast
- Summary of the first week of summer vacation
- How does esrally perform simple custom performance tests?
- Research Report on market supply and demand and strategy of Chinese garden equipment industry
- Welfare | Pu Aries | liv heart co branded Plush surrounding new products are on the market!
- [shutter] statefulwidget component (image component | textfield component)
猜你喜欢

MySQL learning record (8)
![[shutter] shutter layout component (opacity component | clipprect component | padding component)](/img/6b/4304be6a4c5427dcfc927babacb4d7.jpg)
[shutter] shutter layout component (opacity component | clipprect component | padding component)

Hot backup routing protocol (HSRP)

qwb2018_ core kernel_ rop

Check the confession items of 6 yyds

Gbase8s database type

6 pyspark Library
![[shutter] statefulwidget component (floatingactionbutton component | refreshindicator component)](/img/17/b5889ec263687aeacf19214785ea8a.jpg)
[shutter] statefulwidget component (floatingactionbutton component | refreshindicator component)
![[shutter] shutter layout component (fractionallysizedbox component | stack layout component | positioned component)](/img/5f/e96baefd9481c496024fed345e31fe.jpg)
[shutter] shutter layout component (fractionallysizedbox component | stack layout component | positioned component)
![[shutter] shutter layout component (Introduction to layout component | row component | column component | sizedbox component | clipoval component)](/img/45/735431f59a84e9554225a72a551ab8.jpg)
[shutter] shutter layout component (Introduction to layout component | row component | column component | sizedbox component | clipoval component)
随机推荐
Browser - clean up the cache of JS in the page
Sword finger offer (I) -- handwriting singleton mode
Jar package startup failed -mysql modify the default port number / set password free enter
Research Report on market supply and demand and strategy of microplate instrument industry in China
26 FPS video super-resolution model DAP! Output 720p Video Online
Centos7 installation and configuration of redis database
treevalue——Master Nested Data Like Tensor
How to test the process of restoring backup files?
Sword finger offer (II) -- search in two-dimensional array
System (hierarchical) clustering method and SPSS implementation
Research Report on right-hand front door industry - market status analysis and development prospect forecast
Market trend report, technical innovation and market forecast of China's Micro pliers
[dynamic planning] p1220: interval DP: turn off the street lights
Codeworks global round 19 (CF 1637) a ~ e problem solution
Structured text language XML
Market trend report, technical dynamic innovation and market forecast of China's low gloss instrument
Welfare | Pu Aries | liv heart co branded Plush surrounding new products are on the market!
Three chess games
The web version of xshell supports FTP connection and SFTP connection
股票开户要找谁?手机开户是安全么?