当前位置:网站首页>16. 最接近的三數之和
16. 最接近的三數之和
2022-06-12 05:24:00 【anieoo】
原題鏈接:16. 最接近的三數之和
solution:
本題和三數之和很相似,在計算最接近target的時候,需要考慮大於target和小於target兩種情
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int n = nums.size();
pair<int, int> res(INT_MAX, INT_MAX);
sort(nums.begin(),nums.end()); //排序
for(int i = 0;i < n;i++) {
if(i > 0 && nums[i] == nums[i - 1]) continue; //去重
for(int j = i + 1,k = n - 1;j < k;j++) {
if(j > i + 1 && nums[j] == nums[j - 1]) continue;
while(k - 1 > j && nums[i] + nums[j] + nums[k - 1] > target) k--;
//計算一次大於target的數和一次小於target的數
int sum = nums[i] + nums[j] + nums[k];
res = min(res, make_pair(abs(target - sum), sum));
if(k - 1 > j) {
sum = nums[i] + nums[j] + nums[k - 1];
res = min(res, make_pair(abs(target - sum), sum));
}
}
}
return res.second;
}
};边栏推荐
- Field xxxxDAO in com. nero. hua. service. impl. LoginServiceImpl required a bean of type
- [backtracking] backtracking to solve subset problems
- 12.26 exercise summary
- Force/release learning arrangement in IC Verification (5) research on the influence of reg type signals
- Variables and data types
- Layer sublayer assigns values to the page elements of the parent layer to achieve the effect of transferring values to the page of the parent layer
- Pupanvr hardware and software board side development environment configuration (4)
- Computer network connected but unable to access the Internet
- New knowledge today
- 【cjson】根节点注意事项
猜你喜欢

Performance test - GTI application service performance monitoring platform

Simulation of array implementation stack

Development of video preview for main interface of pupanvr-ui

How Bi makes SaaS products have a "sense of security" and "sensitivity" (Part I)

Some problems of silly girl solved

Performance test - performance test tool analysis

Detailed analysis of mathematical modeling problem a (vaccine production scheduling problem) of May Day cup in 2021

Why is Julia so popular?

20000 word detailed reptile knowledge reserve, basic exercises of data collection and cleaning (I) reference answers to the first song

Save the object in redis, save the bean in redis hash, and attach the bean map interoperation tool class
随机推荐
Nbiot module me3616 at command mqtt connecting thingsboard
Static keyword and inheritance, polymorphic and special classes
Surface net radiation flux data, solar radiation data, rainfall data, air temperature data, sunshine duration, water vapor pressure distribution, wind speed and direction data, surface temperature
Pytorch was reported by a large number of netizens that torchrec, a new library, was "born" and has a large scale
Fundamentals of intensive learning openai gym environment construction demo
MySQL Linux Installation mysql-5.7.24
Pupanvr- some tool class objects (6)
19. regular expression matching
MySQL5.7.21 Build For ARM
Servlet core
JS controls the display and hiding of tags through class
Design of a simple embedded web service application
February 19, 2022 [Nolan] Nolan resurrected? Change · Nolan [soul orchid] can be connected to XDD / silly girl
Automated testing - Po mode / log /allure/ continuous integration
Harris corner detection principle-
Yolov5 realizes road crack detection
Sv806 QT UI development
Three. JS import model demo analysis (with notes)
How to count the total length of roads in the region and draw data histogram
Yolo opencv scale identification scale reading identification water gauge identification water level identification source code