麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

LeetCode 16. 3Sum Closest

2019-11-11 04:56:11
字體:
來源:轉載
供稿:網友

描述 Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

For example, given array S = {-1 2 1 -4}, and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

分析 先排序,然后左右夾逼,復雜度 O(n2)。

代碼

class Solution {public: int threeSumClosest(vector<int>& nums, int target) { int min_gap = INT_MAX; int result = 0; sort(nums.begin(), nums.end()); for (auto a = nums.begin(); a != PRev(nums.end(), 2); ++a) { auto b = next(a); auto c = prev(nums.end()); while (b < c) { int sum = *a + *b + *c; int gap = abs(sum - target); if (gap < min_gap) { min_gap = gap; result = sum; } if (sum < target) ++b; else --c; } } return result; }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产精品成年片在线观看, 激情小说另类 | 国毛片 | 亚洲福利视 | 欧美视频国产 | 毛片免费看的 | 黄色片网站在线看 | 成年人在线视频观看 | 99热久草 | 毛片在线免费观看完整版 | 国产精品伊人久久 | 精品国产96亚洲一区二区三区 | 久久久线视频 | 亚洲免费观看视频 | 亚洲一区二区不卡视频 | 欧洲精品久久久久69精品 | 有兽焉免费动画 | 男女隐私免费视频 | 嗯哈~不行好大h双性 | 91羞羞| 看个毛片 | a网站在线 | 黄色毛片视频在线观看 | 黄色片视频在线观看 | 久久艹综合 | 狠狠久久伊人中文字幕 | 宅男视频在线观看免费 | 一级在线视频 | 国产超碰人人做人人爱ⅴa 国产精品久久久久久久hd | 国产福利不卡一区二区三区 | 久久99精品久久久久久青青日本 | 成人免费福利 | 亚洲第一页中文字幕 | 免费的性生活视频 | 成年人视频在线免费观看 | 成人毛片网 | 国产精品视频一区二区噜噜 | 国产成年人网站 | 亚洲欧美aⅴ | 久久精品国产精品亚洲 | 成年人激情在线 | 免费一区二区三区 |