当前位置:网站首页>Sword finger offer notes: t57 - I. and two numbers of S
Sword finger offer notes: t57 - I. and two numbers of S
2022-07-27 11:48:00 【Ignorant little nine】
T57 - I. And for s Two numbers of
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
solution 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;
}
}
It's overtime
solution 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];
}
}
Execution time :2 ms, In all Java Defeated in submission **95.08%** Users of
Memory consumption :55.1 MB, In all Java Defeated in submission **86.87%** Users of
Time complexity :O(n), Spatial complexity :O(1)
边栏推荐
- Proteus8专业版破解后用数码管闪退的解决
- Vscode removes style / syntax highlighting / code highlighting / black background when copying code
- Shell脚本文本三剑客之awk
- torch‘ has no attribute ‘inference_mode‘
- pytorch和tensorflow一样展示summary
- LeetCode 03: T58. 最后一个单词的长度(简单); 剑指 Offer 05. 替换空格(简单); 剑指 Offer 58 - II. 左旋转字符串(简单)
- 局域网SDN硬核技术内幕 25 展望未来——RDMA(下)
- 局域网SDN硬核技术内幕 23 展望未来——RDMA(上)
- Could not load dynamic library ‘libcudnn.so.8‘;
- 哈希表 详细讲解
猜你喜欢

Detailed explanation of hash table

Wilcoxon rank-sum 和 signed-rank

Principle of PWM and generation of PWM wave

Why is ack=seq+1 when TCP shakes hands three times

Introduction to box diagram

求不同采样周期下的传递函数有限零点

第8章 多线程
新版数据仓库的同步使用参考(新手向)

IDEA: Can‘t use Subversion command line client:svn 解决方案

Japan Fukushima waste dump safety monitoring agreement will recognize the "safety" of the sea discharge plan
随机推荐
Maker Hongmeng application development training notes 03
Principle of control system based on feedback rate
Keil MDK编译出现..\USER\stm32f10x.h(428): error: #67: expected a “}“错误的解决办法
剑指 Offer 笔记: T58 - I. 翻转单词顺序
ZABBIX custom monitoring items
Adobe audit prompts that the sampling rate of audio input does not match the output device - problem solving
LAN SDN technology hard core insider 12 cloud CP's daily love - hardware vxlan forwarding plane
Newton-Raphson迭代法
CTF crypto RSA getting started
Analysis of the use of JUC framework from runnable to callable to futuretask
TapNet: Multivariate Time Series Classification with Attentional Prototypical Network
C programming language (2nd Edition) -- Reading Notes -- 1.5.4
【Unity入门计划】CreatorKitFPS:第一人称射击3D小游戏
[special topic] summary of RMQ question brushing with ST table
The C programming language -- (2nd) -- Notes -- 4.11.2
The C programming language (2nd) -- Notes -- 1.7
Database cli tool docker image
你真的会写二分查找吗——变种二分查找
compute_ class_ weight() takes 1 positional argument but 3 were given
Why is ack=seq+1 when TCP shakes hands three times