{"id":9652,"date":"2022-04-16T21:49:56","date_gmt":"2022-04-17T04:49:56","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9652"},"modified":"2022-04-16T21:52:53","modified_gmt":"2022-04-17T04:52:53","slug":"leetcode-2239-find-closest-number-to-zero","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2239-find-closest-number-to-zero\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2239. Find Closest Number to Zero"},"content":{"rendered":"\n<p>Given an integer array&nbsp;<code>nums<\/code>&nbsp;of size&nbsp;<code>n<\/code>, return&nbsp;<em>the number with the value&nbsp;<strong>closest<\/strong>&nbsp;to&nbsp;<\/em><code>0<\/code><em>&nbsp;in&nbsp;<\/em><code>nums<\/code>. If there are multiple answers, return&nbsp;<em>the number with the&nbsp;<strong>largest<\/strong>&nbsp;value<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [-4,-2,1,4,8]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong>\nThe distance from -4 to 0 is |-4| = 4.\nThe distance from -2 to 0 is |-2| = 2.\nThe distance from 1 to 0 is |1| = 1.\nThe distance from 4 to 0 is |4| = 4.\nThe distance from 8 to 0 is |8| = 8.\nThus, the closest number to 0 in the array is 1.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [2,-1,1]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> 1 and -1 are both the closest numbers to 0, so 1 being larger is returned.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 1000<\/code><\/li><li><code>-10<sup>5<\/sup>&nbsp;&lt;= nums[i] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: ABS<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int findClosestNumber(vector<int>& nums) {\n    return *min_element(begin(nums), end(nums), [](int a, int b){\n      return abs(a) < abs(b) || (abs(a) == abs(b) &#038;&#038; a > b);\n    });\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;nums&nbsp;of size&nbsp;n, return&nbsp;the number with the value&nbsp;closest&nbsp;to&nbsp;0&nbsp;in&nbsp;nums. If there are multiple answers, return&nbsp;the number with the&nbsp;largest&nbsp;value. Example 1: Input: nums = [-4,-2,1,4,8]&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,222],"class_list":["post-9652","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9652"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9652\/revisions"}],"predecessor-version":[{"id":9655,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9652\/revisions\/9655"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}