2239. Find Closest Number to Zero 發表於 2023-02-13 | 分類於 leetcode problemsolution1234567891011121314class Solution {public: int findClosestNumber(vector<int>& nums) { int ret = nums[0]; for(int n:nums){ if(abs(n) <= abs(ret) ) { if(abs(n)==abs(ret) && n>0) ret= n; // else if(abs(n) == abs(ret)) continue; else if(abs(n) < abs(ret)) ret = n; } } return ret; }}; analysis time complexity O(n) space complexity O(1)