当前位置:网站首页>【剑指 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 website [5]
- Check the confession items of 6 yyds
- [shutter] the shutter plug-in is used in the shutter project (shutter plug-in management platform | search shutter plug-in | install shutter plug-in | use shutter plug-in)
- [shutter] shutter layout component (wrap component | expanded component)
- Report on investment development and strategic recommendations of China's vibration isolator market, 2022-2027
- kernel_ uaf
- mysql
- Structure array, pointer and function and application cases
- Who do you want to open a stock account? Is it safe to open a mobile account?
- Spend more time with your computer on this special holiday, HHH
猜你喜欢

Capacity expansion mechanism of ArrayList

Three chess games

Browser - clean up the cache of JS in the page

MySQL learning record (4)

How to prevent your jar from being decompiled?

How to test the process of restoring backup files?

MySQL learning record (6)

Cardinality sorting (detailed illustration)

Huawei Hongmeng watch achieves fireworks display effect on New Year's Eve
![[error record] the command line creates an error pub get failed (server unavailable) -- attempting retry 1 in 1 second](/img/6e/c82ff02a249b5d275a4589120a197a.jpg)
[error record] the command line creates an error pub get failed (server unavailable) -- attempting retry 1 in 1 second
随机推荐
~91 rotation
Browser - clean up the cache of JS in the page
Basic knowledge of tree and binary tree (detailed illustration)
Accounting regulations and professional ethics [18]
MySQL inserts Chinese data and reports an error. Set the default collation
Jar package startup failed -mysql modify the default port number / set password free enter
Unexpectedly, there are such sand sculpture code comments! I laughed
Plastic floating dock Industry Research Report - market status analysis and development prospect forecast
Cloud computing technology [2]
MySQL learning record (3)
One week dynamics of dragon lizard community | 2.07-2.13
Volvo's first MPV is exposed! Comfortable and safe, equipped with 2.0T plug-in mixing system, it is worth first-class
MySQL learning record (2)
AES encryption CBC mode pkcs7padding filling Base64 encoding key 32byte iv16byte
[shutter] statefulwidget component (image component | textfield component)
~90z axis translation
pyqt图片解码 编码后加载图片
The neo4j skill tree was officially released to help you easily master the neo4j map database
China's crude oil heater market trend report, technological innovation and market forecast
D4:非成对图像去雾,基于密度与深度分解的自增强方法(CVPR 2022)