From 386069e2ec4e3f04e9df781d216046ae47b84e0a Mon Sep 17 00:00:00 2001 From: un-lock-able Date: Thu, 21 Aug 2025 23:23:51 +0800 Subject: [PATCH] add prob 5 --- 5-2239-find-closest-to-zero.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 5-2239-find-closest-to-zero.cpp diff --git a/5-2239-find-closest-to-zero.cpp b/5-2239-find-closest-to-zero.cpp new file mode 100644 index 0000000..161bb6b --- /dev/null +++ b/5-2239-find-closest-to-zero.cpp @@ -0,0 +1,29 @@ +#include +#include +#include + +using namespace std; + +class Solution { +public: + int findClosestNumber(vector& 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 l = {1, 2, 3}; + Solution s; + cout << s.findClosestNumber(l) << endl; + return 0; +} \ No newline at end of file