{"id":9043,"date":"2021-12-05T19:18:33","date_gmt":"2021-12-06T03:18:33","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9043"},"modified":"2021-12-05T19:19:05","modified_gmt":"2021-12-06T03:19:05","slug":"leetcode-202-happy-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-202-happy-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 202. Happy Number"},"content":{"rendered":"\n<p>Write an algorithm to determine if a number&nbsp;<code>n<\/code>&nbsp;is happy.<\/p>\n\n\n\n<p>A&nbsp;<strong>happy number<\/strong>&nbsp;is a number defined by the following process:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Starting with any positive integer, replace the number by the sum of the squares of its digits.<\/li><li>Repeat the process until the number equals 1 (where it will stay), or it&nbsp;<strong>loops endlessly in a cycle<\/strong>&nbsp;which does not include 1.<\/li><li>Those numbers for which this process&nbsp;<strong>ends in 1<\/strong>&nbsp;are happy.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<code>true<\/code>&nbsp;<em>if<\/em>&nbsp;<code>n<\/code>&nbsp;<em>is a happy number, and<\/em>&nbsp;<code>false<\/code>&nbsp;<em>if not<\/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> n = 19\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong>\n1<sup>2<\/sup> + 9<sup>2<\/sup> = 82\n8<sup>2<\/sup> + 2<sup>2<\/sup> = 68\n6<sup>2<\/sup> + 8<sup>2<\/sup> = 100\n1<sup>2<\/sup> + 0<sup>2<\/sup> + 0<sup>2<\/sup> = 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> n = 2\n<strong>Output:<\/strong> false\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;= 2<sup>31<\/sup>&nbsp;- 1<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>We can use a hasthable to store all the number we generated so far.<\/p>\n\n\n\n<p>Time complexity: O(L)<br>Space complexity: O(L)<\/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  bool isHappy(int n) {\n    auto getNext = [](int x) -> int {\n      int ans = 0;\n      while (x) {\n        ans += (x % 10) * (x % 10);\n        x \/= 10;\n      }\n      return ans;\n    };\n    \n    unordered_set<int> s;\n    while (n != 1) {\n      if (!s.insert(n).second) return false;\n      n = getNext(n);\n    }\n    return true;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Optimization: Space reduction<\/strong><\/h2>\n\n\n\n<p>Since the number sequence always has a cycle, we can use slow \/ fast pointers to detect the cycle without using a hastable.<\/p>\n\n\n\n<p>Time complexity: O(L)<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++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool isHappy(int n) {\n    auto getNext = [](int x) -&gt; int {\n      int ans = 0;\n      while (x) {\n        ans += (x % 10) * (x % 10);\n        x \/= 10;\n      }\n      return ans;\n    };\n    \n    int slow = n;\n    int fast = getNext(n);\n    do {      \n      slow = getNext(slow);\n      fast = getNext(getNext(fast));\n    } while (slow != fast);\n    return slow == 1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write an algorithm to determine if a number&nbsp;n&nbsp;is happy. A&nbsp;happy number&nbsp;is a number defined by the following process: Starting with any positive integer, replace the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[123,352,31,179],"class_list":["post-9043","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-cycle","tag-fast-slow","tag-math","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9043","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=9043"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9043\/revisions"}],"predecessor-version":[{"id":9045,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9043\/revisions\/9045"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}