当前位置:网站首页>【剑指 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];
}
}
边栏推荐
- Research Report on micro gripper industry - market status analysis and development prospect prediction
- Internet Explorer ignores cookies on some domains (cannot read or set cookies)
- Research Report on market supply and demand and strategy of microplate instrument industry in China
- Three chess games
- [hands on deep learning]02 softmax regression
- [shutter] shutter layout component (opacity component | clipprect component | padding component)
- Baidu sued a company called "Ciba screen"
- [shutter] statefulwidget component (pageview component)
- China's Micro SD market trend report, technology dynamic innovation and market forecast
- treevalue——Master Nested Data Like Tensor
猜你喜欢
[hands on deep learning]02 softmax regression
It is said that this year gold three silver four has become gold one silver two..
[CV] Wu Enda machine learning course notes | Chapter 12
[shutter] shutter layout component (physicalmodel component)
How to test the process of restoring backup files?
MySQL learning record (6)
The neo4j skill tree was officially released to help you easily master the neo4j map database
7. Build native development environment
[shutter] statefulwidget component (bottom navigation bar component | bottomnavigationbar component | bottomnavigationbaritem component | tab switching)
[shutter] statefulwidget component (floatingactionbutton component | refreshindicator component)
随机推荐
Who do you want to open a stock account? Is it safe to open a mobile account?
In depth research and investment feasibility report of global and Chinese isolator industry, 2022-2028
基本IO接口技术——微机第七章笔记
Share the easy-to-use fastadmin open source system - Installation
Accounting regulations and professional ethics [16]
[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)
Common routines of compressed packets in CTF
[shutter] shutter layout component (fractionallysizedbox component | stack layout component | positioned component)
Analysis of neural network
Read a doctor, the kind that studies cows! Dr. enrollment of livestock technology group of Leuven University, milk quality monitoring
Plastic granule Industry Research Report - market status analysis and development prospect forecast
Construction and maintenance of business website [2]
MySQL learning record (7)
Golang string segmentation
Analysis of enterprise financial statements [4]
The web version of xshell supports FTP connection and SFTP connection
读博士吧,研究奶牛的那种!鲁汶大学 Livestock Technology 组博士招生,牛奶质量监测...
Jar package startup failed -mysql modify the default port number / set password free enter
rwctf2022_ QLaaS
Construction and maintenance of business websites [10]