29 lines
622 B
C++
29 lines
622 B
C++
#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;
|
|
} |