1 Star 0 Fork 0

MetaverseMobile/leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
sumupof3.cpp 1.56 KB
一键复制 编辑 原始数据 按行查看 历史
MetaverseMobile 提交于 2020-10-24 19:38 . {* : add : new files into repo; }
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
class Solution {
public:
int threeSumClosest(std::vector<int>& nums, int target) {
int Sum;
unsigned int Err = (unsigned int)-1;
int TmpErr;
std::sort(nums.begin(), nums.end());
int Res = nums[0] + nums[1] + nums[2];
for (int f = 0; f < nums.size() - 2; f++) {
int b = f + 1, e = nums.size() - 1;
while (b != e) {
// 求和
Sum = nums[f] + nums[b] + nums[e];
TmpErr = target - Sum;
std::cout << "Get " << TmpErr << " : " << nums[f] << ", " << nums[b] << ", " << nums[e] << std::endl;
//
if (abs(TmpErr) < Err) {
Res = Sum;
Err = abs(TmpErr);
}
if (TmpErr > 0)
b++;
else
e--;
}
}
return Res;
}
};
int main(void)
{
// std::vector<int> nums = {-3, 0, 1, 2};
std::vector<int> nums = {0, 2, 1, -3};
int target = 1;
Solution s;
std::cout << s.threeSumClosest(nums, target) << std::endl;
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/MetaverseMobile/leetcode.git
[email protected]:MetaverseMobile/leetcode.git
MetaverseMobile
leetcode
leetcode
main

搜索帮助