{"id":6426,"date":"2020-03-09T00:34:16","date_gmt":"2020-03-09T07:34:16","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6426"},"modified":"2020-03-09T00:41:46","modified_gmt":"2020-03-09T07:41:46","slug":"leetcode-258-add-digits","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-258-add-digits\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 258. Add Digits"},"content":{"rendered":"\n<p>Given a non-negative integer&nbsp;<code>num<\/code>, repeatedly add all its digits until the result has only one digit.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> <code>38<\/code>\n<strong>Output:<\/strong> 2 \n<strong>Explanation: <\/strong>The process is like: <code>3 + 8 = 11<\/code>, <code>1 + 1 = 2<\/code>. \n&nbsp;            Since <code>2<\/code> has only one digit, return it.\n<\/pre>\n\n\n\n<p><strong>Follow up:<\/strong><br>Could you do it without any loop\/recursion in O(1) runtime?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(logn)<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  int addDigits(int num) {\n    int n = num;\n    while (n >= 10) {      \n      int t = n;\n      n = 0;\n      while (t) {\n        n += t % 10;\n        t \/= 10;\n      }\n    }\n    return n;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Math<\/strong><\/h2>\n\n\n\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Digital_root#Congruence_formula\">https:\/\/en.wikipedia.org\/wiki\/Digital_root#Congruence_formula<\/a><\/p>\n\n\n\n<p>Digit root = num % 9 if num % 9 != 0 else min(num, 9) e.g. 0 or 9<\/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 addDigits(int num) {    \n    return num % 9 ? num % 9 : min(num, 9);\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a non-negative integer&nbsp;num, repeatedly add all its digits until the result has only one digit. Example: Input: 38 Output: 2 Explanation: The process is&#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":[521,31,92,179],"class_list":["post-6426","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-digits","tag-math","tag-o1","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6426","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=6426"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6426\/revisions"}],"predecessor-version":[{"id":6431,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6426\/revisions\/6431"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}