{"id":8303,"date":"2021-04-03T12:05:45","date_gmt":"2021-04-03T19:05:45","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8303"},"modified":"2021-04-03T12:06:08","modified_gmt":"2021-04-03T19:06:08","slug":"leetcode-1814-count-nice-pairs-in-an-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1814-count-nice-pairs-in-an-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1814. Count Nice Pairs in an Array"},"content":{"rendered":"\n<p>You are given an array&nbsp;<code>nums<\/code>&nbsp;that consists of non-negative integers. Let us define&nbsp;<code>rev(x)<\/code>&nbsp;as the reverse of the non-negative integer&nbsp;<code>x<\/code>. For example,&nbsp;<code>rev(123) = 321<\/code>, and&nbsp;<code>rev(120) = 21<\/code>. A pair of indices&nbsp;<code>(i, j)<\/code>&nbsp;is&nbsp;<strong>nice<\/strong>&nbsp;if it satisfies all of the following conditions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= i &lt; j &lt; nums.length<\/code><\/li><li><code>nums[i] + rev(nums[j]) == nums[j] + rev(nums[i])<\/code><\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the number of nice pairs of indices<\/em>. Since that number can be too large, return it&nbsp;<strong>modulo<\/strong>&nbsp;<code>10<sup>9<\/sup>&nbsp;+ 7<\/code>.<\/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 = [42,11,1,97]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The two pairs are:\n - (0,3) : 42 + rev(97) = 42 + 79 = 121, 97 + rev(42) = 97 + 24 = 121.\n - (1,2) : 11 + rev(1) = 11 + 1 = 12, 1 + rev(11) = 1 + 11 = 12.\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 = [13,10,35,24,76]\n<strong>Output:<\/strong> 4\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= nums[i] &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Two Sum<\/strong><\/h2>\n\n\n\n<p>Key = x &#8211; rev(x)<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/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 countNicePairs(vector<int>& nums) {\n    constexpr int kMod = 1e9 + 7;\n    unordered_map<int, int> m;\n    long ans = 0;\n    for (int x : nums) {      \n      string s = to_string(x);\n      reverse(begin(s), end(s));\n      ans += m[x - stoi(s)]++;\n    }\n    return ans % kMod;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array&nbsp;nums&nbsp;that consists of non-negative integers. Let us define&nbsp;rev(x)&nbsp;as the reverse of the non-negative integer&nbsp;x. For example,&nbsp;rev(123) = 321, and&nbsp;rev(120) = 21.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[82,177,208],"class_list":["post-8303","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-medium","tag-two-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8303","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=8303"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8303\/revisions"}],"predecessor-version":[{"id":8305,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8303\/revisions\/8305"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}