{"id":9519,"date":"2022-02-28T05:51:26","date_gmt":"2022-02-28T13:51:26","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9519"},"modified":"2022-02-28T06:58:34","modified_gmt":"2022-02-28T14:58:34","slug":"leetcode-2180-count-integers-with-even-digit-sum","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2180-count-integers-with-even-digit-sum\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2180. Count Integers With Even Digit Sum"},"content":{"rendered":"\n<p>Given a positive integer&nbsp;<code>num<\/code>, return&nbsp;<em>the number of positive integers&nbsp;<strong>less than or equal to<\/strong><\/em>&nbsp;<code>num<\/code>&nbsp;<em>whose digit sums are&nbsp;<strong>even<\/strong><\/em>.<\/p>\n\n\n\n<p>The&nbsp;<strong>digit sum<\/strong>&nbsp;of a positive integer is the sum of all its digits.<\/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 = 4\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong>\nThe only integers less than or equal to 4 whose digit sums are even are 2 and 4.    \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 = 30\n<strong>Output:<\/strong> 14\n<strong>Explanation:<\/strong>\nThe 14 integers less than or equal to 30 whose digit sums are even are\n2, 4, 6, 8, 11, 13, 15, 17, 19, 20, 22, 24, 26, and 28.\n<\/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;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Use std::to_string to convert an integer to string.<\/p>\n\n\n\n<p>Time complexity: O(nlgn)<br>Space complexity: O(lgn)<\/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 countEven(int num) {\n    int ans = 0;\n    for (int i = 1; i <= num; ++i) {\n      int sum = 0;\n      for (char c : to_string(i))\n        sum += (c - '0');\n      if (sum % 2 == 0) ++ans;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a positive integer&nbsp;num, return&nbsp;the number of positive integers&nbsp;less than or equal to&nbsp;num&nbsp;whose digit sums are&nbsp;even. The&nbsp;digit sum&nbsp;of a positive integer is the sum of&#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":[695,222,766,4],"class_list":["post-9519","post","type-post","status-publish","format-standard","hentry","category-string","tag-base10","tag-easy","tag-num","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9519","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=9519"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9519\/revisions"}],"predecessor-version":[{"id":9521,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9519\/revisions\/9521"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}