{"id":8385,"date":"2021-04-25T09:04:05","date_gmt":"2021-04-25T16:04:05","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8385"},"modified":"2021-04-25T09:23:19","modified_gmt":"2021-04-25T16:23:19","slug":"leetcode-1837-sum-of-digits-in-base-k","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1837-sum-of-digits-in-base-k\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1837. Sum of Digits in Base K"},"content":{"rendered":"\n<p>Given an integer&nbsp;<code>n<\/code>&nbsp;(in base&nbsp;<code>10<\/code>) and a base&nbsp;<code>k<\/code>, return&nbsp;<em>the&nbsp;<strong>sum<\/strong>&nbsp;of the digits of&nbsp;<\/em><code>n<\/code><em>&nbsp;<strong>after<\/strong>&nbsp;converting&nbsp;<\/em><code>n<\/code><em>&nbsp;from base&nbsp;<\/em><code>10<\/code><em>&nbsp;to base&nbsp;<\/em><code>k<\/code>.<\/p>\n\n\n\n<p>After converting, each digit should be interpreted as a base&nbsp;<code>10<\/code>&nbsp;number, and the sum should be returned in base&nbsp;<code>10<\/code>.<\/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 = 34, k = 6\n<strong>Output:<\/strong> 9\n<strong>Explanation: <\/strong>34 (base 10) expressed in base 6 is 54. 5 + 4 = 9.\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 = 10, k = 10\n<strong>Output:<\/strong> 1\n<strong>Explanation: <\/strong>n is already in base 10. 1 + 0 = 1.\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;= 100<\/code><\/li><li><code>2 &lt;= k &lt;= 10<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Base Conversion<\/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 sumBase(int n, int k) {\n    int ans = 0;\n    while (n) {\n      ans += n % k;\n      n \/= k;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer&nbsp;n&nbsp;(in base&nbsp;10) and a base&nbsp;k, return&nbsp;the&nbsp;sum&nbsp;of the digits of&nbsp;n&nbsp;after&nbsp;converting&nbsp;n&nbsp;from base&nbsp;10&nbsp;to base&nbsp;k. After converting, each digit should be interpreted as a base&nbsp;10&nbsp;number, and 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":[49],"tags":[379,222,31],"class_list":["post-8385","post","type-post","status-publish","format-standard","hentry","category-math","tag-base","tag-easy","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8385","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=8385"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8385\/revisions"}],"predecessor-version":[{"id":8387,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8385\/revisions\/8387"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}