当前位置:网站首页>[leetcode] 16. The sum of the nearest three numbers
[leetcode] 16. The sum of the nearest three numbers
2022-07-01 14:55:00 【Xiaoqu classmate】
16、 The closest sum of three
subject :
Give you a length of n Array of integers for nums and A target value target. Please start from nums Choose three integers , Make their sum with target Nearest .
Returns the sum of the three numbers .
It is assumed that there is only exactly one solution for each set of inputs .
Example 1:
Input :nums = [-1,2,1,-4], target = 1
Output :2
explain : And target The closest and 2 (-1 + 2 + 1 = 2) .
Example 2:
Input :nums = [0,0,0], target = 1
Output :0
Tips :
3 <= nums.length <= 1000
-1000 <= nums[i] <= 1000
-10 ^4 <= target <= 10 ^4
Their thinking :
This topic and 15、 Sum of three numbers , Very similar , You can use the idea of double pointers in the same way .
First , The title is the closest target Value , Closest, that is, the absolute value of the sum of three numbers is closest to the target value .
We can consider using... Directly Triple loop enumerating triples , Find the one closest to the target value as the answer , The time complexity is O(N^3). However, this question is N The maximum is 1000, Will exceed the time limit . So it's not advisable to .
therefore , The right thinking is : Sort + Double pointer
- label : Sort and double pointer
- Because there are three numbers to calculate , If we count by violence, the time complexity will be O(n^3), Need to reduce time complexity
- First, sort the array , Time complexity O(nlogn)
- In the array nums in , Traversal , Each traversal of a value uses its subscript i, Form a fixed value nums[i]
- Before using again, the pointer points to start = i + 1 It's about , The back pointer points to end = nums.length - 1 It's about , It's the end
- according to sum = nums[i] + nums[start] + nums[end] Result , Judge sum And target target Distance of , Update results if closer ans
- At the same time judge sum And target The size of the relationship , Because the array is ordered , If sum > target be end–, If sum <
target be start++, If sum == target The distance is 0 Direct return - The whole traversal process , The fixed value is n Time , The double pointer is n Time , The time complexity is O(n^2)
- Total time complexity :O(nlogn) + O(n^ 2) = O( n^2)
Reference code :
class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int ans = nums[0] + nums[1] + nums[2];
for(int i=0;i<nums.length;i++) {
int start = i+1, end = nums.length - 1;
while(start < end) {
int sum = nums[start] + nums[end] + nums[i];
if(Math.abs(target - sum) < Math.abs(target - ans))
ans = sum;
if(sum > target)
end--;
else if(sum < target)
start++;
else
return ans;
}
}
return ans;
}
}

边栏推荐
- Written on the first day after Doris graduated
- MIT团队使用图神经网络,加速无定形聚合物电解质筛选,促进下一代锂电池技术开发
- About the use of HTTP cache validation last modified and Etag
- [zero basic IOT pwn] reproduce Netgear wnap320 rce
- Semiconductor foundation of binary realization principle
- Salesforce, Johns Hopkins, Columbia | progen2: exploring the boundaries of protein language models
- C#学习笔记(5)类和继承
- Day-02 database
- 购物商城6.27待完成
- These three online PS tools should be tried
猜你喜欢

The State Administration of Chia Tai market supervision, the national development and Reform Commission and the China Securities Regulatory Commission jointly reminded and warned some iron ores

241. 为运算表达式设计优先级

The first word of JVM -- detailed introduction to JVM and analysis of runtime data area

Chapter 4 of getting started with MySQL: creation, modification and deletion of data tables

JVM第一话 -- JVM入门详解以及运行时数据区分析

It's settled! 2022 Hainan secondary cost engineer examination time is determined! The registration channel has been opened!

关于重载运算符的再整理

定了!2022海南二级造价工程师考试时间确定!报名通道已开启!

2022-2-15 learning xiangniuke project - Section 1 filtering sensitive words
![[Verilog quick start of Niuke series] ~ multi function data processor, calculate the difference between two numbers, use generate... For statement to simplify the code, and use sub modules to realize](/img/30/aea4ae24f418eb971bca77a1d46bef.png)
[Verilog quick start of Niuke series] ~ multi function data processor, calculate the difference between two numbers, use generate... For statement to simplify the code, and use sub modules to realize
随机推荐
QT capture interface is displayed as picture or label
Music player development example (can be set up)
Configuration of ZABBIX API and PHP
Mongodb second talk - - mongodb High available Cluster Implementation
Salesforce, Johns Hopkins, Columbia | progen2: exploring the boundaries of protein language models
适合没口才的人做,加入中视频伙伴计划收益是真香,一个视频拿3份收益
[零基础学IoT Pwn] 复现Netgear WNAP320 RCE
购物商城6.27待完成
Build your own website (14)
NPDP产品经理国际认证报名有什么要求?
JVM第一话 -- JVM入门详解以及运行时数据区分析
Hidden rules of the workplace that must be understood before 30
Use the npoi package of net core 6 C to read excel Pictures in xlsx cells and stored to the specified server
[dynamic programming] p1004 grid access (four-dimensional DP template question)
opencv学习笔记六--图像拼接
Tensorflow 2. X realizes iris classification
The first word of JVM -- detailed introduction to JVM and analysis of runtime data area
关于软件测试的一些思考
tensorflow2-savedmodel convert to pb(frozen_graph)
Vnctf2022 open web gocalc0