{"id":6120,"date":"2020-01-20T18:57:05","date_gmt":"2020-01-21T02:57:05","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6120"},"modified":"2020-01-20T19:02:53","modified_gmt":"2020-01-21T03:02:53","slug":"leetcode-1323-maximum-69-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1323-maximum-69-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1323. Maximum 69 Number"},"content":{"rendered":"\n<p>Given a positive integer&nbsp;<code>num<\/code>&nbsp;consisting only of digits 6 and 9.<\/p>\n\n\n\n<p>Return the maximum number you can get by changing&nbsp;<strong>at most<\/strong>&nbsp;one digit (6 becomes 9, and 9 becomes 6).<\/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> num = 9669\n<strong>Output:<\/strong> 9969\n<strong>Explanation:<\/strong> \nChanging the first digit results in 6669.\nChanging the second digit results in 9969.\nChanging the third digit results in 9699.\nChanging the fourth digit results in 9666.&nbsp;\nThe maximum number is 9969.\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> num = 9996\n<strong>Output:<\/strong> 9999\n<strong>Explanation:<\/strong> Changing the last digit 6 to 9 results in the maximum number.<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> num = 9999\n<strong>Output:<\/strong> 9999\n<strong>Explanation:<\/strong> It is better not to apply any change.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= num &lt;= 10^4<\/code><\/li><li><code>num<\/code>&#8216;s digits are 6 or 9.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>Replace the highest 6 to 9, if no 6, return the original number.<\/p>\n\n\n\n<p>Time complexity: O(1)<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 maximum69Number (int num) {\n    string s = to_string(num);\n    for (int i = 0; i < s.length(); ++i) {\n      if (s[i] == '6') {\n        s[i] = '9';\n        return stoi(s);\n      }\n    }\n    return num;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a positive integer&nbsp;num&nbsp;consisting only of digits 6 and 9. Return the maximum number you can get by changing&nbsp;at most&nbsp;one digit (6 becomes 9, and&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[222,88,141,92,4],"class_list":["post-6120","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-easy","tag-greedy","tag-max","tag-o1","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6120","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=6120"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6120\/revisions"}],"predecessor-version":[{"id":6122,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6120\/revisions\/6122"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}