当前位置:网站首页>[leetcode refers to the two numbers of offer 57. And S (simple)]
[leetcode refers to the two numbers of offer 57. And S (simple)]
2022-04-23 21:21:00 【Minaldo7】
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 .
Example 1:
Input :nums = [2,7,11,15], target = 9
Output :[2,7] perhaps [7,2]
Example 2:
Input :nums = [10,26,30,31,47,60], target = 40
Output :[10,30] perhaps [30,10]
Limit :
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^6
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/he-wei-sde-liang-ge-shu-zi-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The problem solving process :
Double pointer
class Solution {
public int[] twoSum(int[] nums, int target) {
int left = 0, right = nums.length-1;
while(left<right){
if(nums[left]+nums[right]>target){
right--;
}else if(nums[left]+nums[right]<target){
left++;
}else
return new int[]{
nums[left],nums[right]};
}
return new int[0];
}
}
Execution results :
版权声明
本文为[Minaldo7]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/111/202204210544479057.html
边栏推荐
猜你喜欢
Addition, deletion, modification and query of MySQL advanced table
MySQL basic collection
Common problems in deploying projects with laravel and composer for PHP
Deep analysis of C language function
Fastdfs mind map
Prim、Kruskal
Problem brushing plan -- dynamic programming (III)
Centos7 builds MySQL master-slave replication from scratch (avoid stepping on the pit)
airbase 初步分析
GSI-ECM工程建设管理数字化平台
随机推荐
Awk example skills
Send email to laravel
Selenium displays webdriverwait
Explore ASP Net core read request The correct way of body
软件测试要怎么学?自学还是培训看完这篇文章你就懂了
Rust更适合经验较少的程序员?
1.整理华子面经--1
Sharpness difference (SD) calculation method of image reconstruction and generation domain index
Two Stage Detection
flomo软件推荐
启牛学堂有用吗,推荐的证券账户是否安全
Google tries to use rust in Chrome
FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ‘:app:stripDe
Express ③ (use express to write interface and cross domain related issues)
MySQL进阶之数据的增删改查(DML)
阿里云回应用户注册信息泄露事件
2. Finishing huazi Mianjing -- 2
Singleton mode
Deep analysis of C language pointer (Part I)
The more you use the computer, the slower it will be? Recovery method of file accidental deletion