当前位置:网站首页>16. Somme des trois plus proches
16. Somme des trois plus proches
2022-06-12 05:24:00 【Anieoo】
Lien vers la question originale:16. La somme des trois plus proches
solution:
Ce sujet est très similaire à la somme des trois,Dans le calcul le plus prochetargetQuand,Plus detargetEt moins detargetDeux émotions.
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()); //Trier
for(int i = 0;i < n;i++) {
if(i > 0 && nums[i] == nums[i - 1]) continue; //Poids mort
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--;
// Calculer plus d'une fois target Le nombre et une fois de est inférieur à targetNombre de
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;
}
};边栏推荐
- Applet pull-down load refresh onreachbottom
- How Bi makes SaaS products have a "sense of security" and "sensitivity" (Part I)
- 43. Number of occurrences of 1 in 1 ~ n integers
- Yolo opencv scale identification scale reading identification water gauge identification water level identification source code
- 4.3 模拟浏览器操作和页面等待(显示等待和隐式等待、句柄)
- It costs less than 30 yuan, but we still don't build it quickly - check the small knowledge of software application
- Performance test - performance test tool analysis
- ESP8266 Arduino OLED
- Three. JS import model demo analysis (with notes)
- [backtracking] backtracking to solve subset problems
猜你喜欢

分公司负责人需要承担的法律责任

什么是工程预付款

National land use data of 30m precision secondary classification

Automated test - dark horse headline test project

Some problems of silly girl solved

Project requirements specification

Detailed usage of vim editor

国企为什么要上市

Vivado HLS introductory notes

Codec of ASoC framework driven by alsa
随机推荐
Variables and data types
Lvgl8.1 hi3536c platform use
Servlet core technology
Performance test - Analysis of performance test results
Simulation of array implementation stack
JS disable mobile sharing
19. regular expression matching
公司注册认缴资金多久
The way to promote software test engineer
[GIS tutorial] ArcGIS for sunshine analysis (with exercise data download)
The master programmer "plays" a C program that is not like C
Test work summary - performance test indicators
Reason: Canonical names should be kebab-case (‘-‘ separated), lowercase alpha-numeric characters and
12.24 day exercise -- Programming summation, 99 multiplication table, while loop and for loop exercises
Main business objects of pupanvr record (5)
[GIS tutorial] land use transfer matrix
Deploying a single node kubernetes cluster using rancher-2.5.5
[C language] realize string interception function
20000 word detailed reptile knowledge reserve, basic exercises of data collection and cleaning (I) reference answers to the first song
分公司负责人需要承担的法律责任