{"id":194,"date":"2018-08-18T20:26:28","date_gmt":"2018-08-18T12:26:28","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=194"},"modified":"2018-11-27T17:28:29","modified_gmt":"2018-11-27T09:28:29","slug":"258-add-digits","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=194","title":{"rendered":"258. Add Digits"},"content":{"rendered":"<p>Given a non-negative integer\u00a0<code>num<\/code>, repeatedly add all its digits until the result has only one digit.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre><strong>Input:<\/strong> <code>38<\/code> <strong>Output:<\/strong> 2 <strong>Explanation: <\/strong>The process is like: <code>3 + 8 = 11<\/code>, <code>1 + 1 = 2<\/code>. \u00a0 Since <code>2<\/code> has only one digit, return it.<\/pre>\n<p><b>Follow up:<\/b><br \/>\nCould you do it without any loop\/recursion in O(1) runtime?<\/p>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\nclass Solution(object):\r\n    def addDigits(self, num):\r\n        &quot;&quot;&quot;\r\n        :type num: int\r\n        :rtype: int\r\n        &quot;&quot;&quot;\r\n        while num &gt; 9:\r\n            num = self.add(num)\r\n        return num\r\n            \r\n            \r\n    def add(self, a):\r\n        result = 0\r\n        for i in str(a):\r\n            result += int(i)\r\n        return result\r\n        \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Given a non-negative integer\u00a0num, repeatedly add all it<a href=\"https:\/\/www.ccagml.com\/?p=194\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">258. Add Digits<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/194"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=194"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/194\/revisions"}],"predecessor-version":[{"id":195,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/194\/revisions\/195"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}