当前位置:网站首页>剑指 Offer 笔记: T57 - I. 和为 s 的两个数字
剑指 Offer 笔记: T57 - I. 和为 s 的两个数字
2022-07-27 10:58:00 【无知小九】
T57 - I. 和为 s 的两个数字
输入一个递增排序的数组和一个数字s,在数组中查找两个数,使得它们的和正好是s。如果有多对数字的和等于s,则输出任意一对即可。
示例 1
输入:nums = [2,7,11,15], target = 9
输出:[2,7] 或者 [7,2]
示例 2:
输入:nums = [10,26,30,31,47,60], target = 40
输出:[10,30] 或者 [30,10]
限制:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^6
解法 1
class Solution {
public int[] twoSum(int[] nums, int target) {
int left =0, right = nums.length-1;
int[] two = new int[2];
while(left<=right){
if(target-nums[right]<=0){
right--;
}else{
if(target==nums[left]+nums[right]){
two[0]=nums[left];
two[1]=nums[right];
}else if(target>nums[left]+nums[right]){
left++;
}else{
return two;
}
}
}
return two;
}
}
超时了
解法 2
class Solution {
public int[] twoSum(int[] nums, int target) {
int left =0, right = nums.length-1;
while(left<=right){
int sum =nums[left]+nums[right];
if(sum>target){
right--;
}else if(sum<target){
left++;
}else{
return new int[]{nums[left],nums[right]};
}
}
return new int[0];
}
}
执行用时:2 ms, 在所有 Java 提交中击败了**95.08%**的用户
内存消耗:55.1 MB, 在所有 Java 提交中击败了**86.87%**的用户
时间复杂度:O(n),空间复杂度:O(1)
边栏推荐
- Backpack model acwing 1022. Collection of pet elves
- 数字三角形模型 AcWing 1018. 最低通行费
- C programming language (2nd Edition) -- Reading Notes -- 1.5
- 49 letter ectopic grouping and 242 effective letter ectopic words
- torch‘ has no attribute ‘inference_mode‘
- Digital triangle model acwing 1027. Grid retrieval
- 求组合数 AcWing 888. 求组合数 IV
- Longest ascending subsequence model acwing 1010. Interceptor missile
- Find the combination number acwing 888. find the combination number IV
- 博弈论 AcWing 893. 集合-Nim游戏
猜你喜欢

Knapsack problem acwing 9. grouping knapsack problem

PAT(乙级)2022年夏季考试

求组合数 AcWing 886. 求组合数 II

状态压缩DP AcWing 91. 最短Hamilton路径

C# 自定义集合

properties文件

The C programming language (2nd) -- Notes -- 1.6

Digital triangle model acwing 275. pass note

Solve importerror: cannot import name'abs'import tensorflow error

Backpack model acwing 1022. Collection of pet elves
随机推荐
ZABBIX custom monitoring items
Inclusion exclusion principle acwing 890. divisible numbers
C programming language (2nd Edition) -- Reading Notes -- 1.3
第7章 异常处理
The C programming language (2nd) -- Notes -- 1.6
Win10 vscode code code format setting and remote breakpoint debugging
2022 Niuke multi school (3) j.journey
Raw socket grabs packets, and packets on some ports cannot be caught
Find the combination number acwing 886. find the combination number II
Moveit2 - 5. Scenario Planning
栈 AcWing 3302. 表达式求值
LAN SDN technology hard core insider 11 the key of cloud convergence CP -- hierarchical port binding
When std:: bind meets this
Luogu p1896 non aggression
The longest ascending subsequence model acwing 1016. The sum of the largest ascending subsequence
Longest ascending subsequence model acwing 482. Chorus formation
Digital triangle model acwing 275. pass note
本地虚拟机初始化脚本
C programming language (2nd Edition) -- Reading Notes -- 1.5.3
Markdown editor syntax - setting of text color, size, font and background color (Reprint)