add prob 5
This commit is contained in:
29
5-2239-find-closest-to-zero.cpp
Normal file
29
5-2239-find-closest-to-zero.cpp
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int findClosestNumber(vector<int>& nums) {
|
||||||
|
int ans = nums[0];
|
||||||
|
for (auto it = nums.begin(); it != nums.end(); ++it) {
|
||||||
|
if (abs(*it) < abs(ans)) {
|
||||||
|
ans = *it;
|
||||||
|
}
|
||||||
|
else if (abs(*it) == abs(ans) && *it > ans) {
|
||||||
|
ans = *it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
cout << "Hello world" << endl;
|
||||||
|
vector<int> l = {1, 2, 3};
|
||||||
|
Solution s;
|
||||||
|
cout << s.findClosestNumber(l) << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user