{"id":1857,"date":"2018-02-24T20:10:25","date_gmt":"2018-02-25T04:10:25","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1857"},"modified":"2018-02-24T23:14:03","modified_gmt":"2018-02-25T07:14:03","slug":"leetcode-788-rotated-digits","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-788-rotated-digits\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 788 Rotated Digits"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a<\/p>\n<p>\u7ed9\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u72ec\u7acb\u65cb\u8f6c\u6bcf\u4e2a\u6570\u5b57180\u00b0\uff0c\u5224\u65ad\u5f97\u5230\u7684\u65b0\u5b57\u7b26\u4e32\u662f\u5426\u5408\u6cd5\u3002<\/p>\n<ol>\n<li>\u51fa\u73b0\u6570\u5b573,4,7\u4e00\u5b9a\u4e0d\u5408\u6cd5<\/li>\n<li>\u65b0\u5b57\u7b26\u4e32 == \u539f\u6765\u5b57\u7b26\u4e32\u4e0d\u5408\u6cd5<\/li>\n<\/ol>\n<p>X is a good number if after rotating each digit individually by 180 degrees, we get a valid number that is different from X. A number is valid if each digit remains a digit after rotation. 0, 1, and 8 rotate to themselves; 2 and 5 rotate to each other; 6 and 9 rotate to each other, and the rest of the numbers do not rotate to any other number.<\/p>\n<p>Now\u00a0given a positive number\u00a0<code>N<\/code>, how many numbers X from\u00a0<code>1<\/code>\u00a0to\u00a0<code>N<\/code>\u00a0are good?<\/p>\n<pre class=\"\">Example:\r\nInput: 10\r\nOutput: 4\r\nExplanation: \r\nThere are four good numbers in the range [1, 10] : 2, 5, 6, 9.\r\nNote that 1 and 10 are not good numbers, since they remain unchanged after rotating.\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li><span style=\"font-family: monospace;\">N<\/span>\u00a0 will be in range\u00a0<code>[1, 10000]<\/code>.<\/li>\n<\/ul>\n<p><strong>Solution 1: Brute Force<\/strong><\/p>\n<p>Time complexity: O(nlogn)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 21 ms\r\nclass Solution {\r\npublic:\r\n  int isValid(int n) {\r\n    string s = to_string(n);\r\n    string t(s);\r\n    for (int i = 0; i &lt; s.length(); ++i) {\r\n      if (s[i] == '3' || s[i] == '4' || s[i] == '7')\r\n        return 0;\r\n      else if (s[i] == '2') t[i] = '5';\r\n      else if (s[i] == '5') t[i] = '2';\r\n      else if (s[i] == '6') t[i] = '9';\r\n      else if (s[i] == '9') t[i] = '6';\r\n    }\r\n      \r\n    return t != s;\r\n  }\r\n  \r\n  int rotatedDigits(int N) {\r\n    int ans = 0;\r\n    for (int i = 1; i &lt;= N; ++i)\r\n      ans += isValid(i);    \r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>Bit\u00a0 Operation<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  int isValid(int n) {    \r\n    constexpr int kInValidMask = (1 &lt;&lt; 3) | (1 &lt;&lt; 4) | (1 &lt;&lt; 7);\r\n    constexpr int kValidMask = (1 &lt;&lt; 2) | (1 &lt;&lt; 5) | (1 &lt;&lt; 6) | (1 &lt;&lt; 9);\r\n    \r\n    int valid = 0;\r\n    \r\n    while (n &gt; 0) {\r\n      int r = 1 &lt;&lt; (n % 10);\r\n      if (r &amp; kInValidMask)\r\n        return 0;\r\n      else if (r &amp; kValidMask)\r\n        valid = 1;\r\n      n \/= 10;\r\n    }\r\n      \r\n    return valid;\r\n  }\r\n  \r\n  int rotatedDigits(int N) {\r\n    int ans = 0;\r\n    for (int i = 1; i &lt;= N; ++i)\r\n      ans += isValid(i);\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a \u7ed9\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u72ec\u7acb\u65cb\u8f6c\u6bcf\u4e2a\u6570\u5b57180\u00b0\uff0c\u5224\u65ad\u5f97\u5230\u7684\u65b0\u5b57\u7b26\u4e32\u662f\u5426\u5408\u6cd5\u3002 \u51fa\u73b0\u6570\u5b573,4,7\u4e00\u5b9a\u4e0d\u5408\u6cd5 \u65b0\u5b57\u7b26\u4e32 == \u539f\u6765\u5b57\u7b26\u4e32\u4e0d\u5408\u6cd5 X is a good number if after rotating each digit individually by 180 degrees, we get a valid number&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[222,4],"class_list":["post-1857","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1857","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=1857"}],"version-history":[{"count":8,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1857\/revisions"}],"predecessor-version":[{"id":1880,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1857\/revisions\/1880"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}