{"id":3086,"date":"2018-07-12T08:09:18","date_gmt":"2018-07-12T15:09:18","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3086"},"modified":"2018-09-04T01:51:14","modified_gmt":"2018-09-04T08:51:14","slug":"leetcode-709-to-lower-case","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-709-to-lower-case\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 709. To Lower Case"},"content":{"rendered":"<h1>Problem<\/h1>\n<div class=\"question-description__3U1T\">\n<div>\n<p>Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.<\/p>\n<\/div>\n<\/div>\n<h1><strong>Solution<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 0 ms\r\nclass Solution {\r\npublic:\r\n  string toLowerCase(string str) {\r\n    for (char &amp;c : str)\r\n      if (c &gt;= 'A' &amp;&amp; c &lt;= 'Z') c = c - 'A' + 'a';\r\n    return str;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:java decode:true \">\/\/ Author: Huahua, 1 ms\r\nclass Solution {\r\n  public String toLowerCase(String str) {\r\n    char[] s = str.toCharArray();\r\n    for (int i = 0; i &lt; s.length; ++i)\r\n      if (s[i] &gt;= 'A' &amp;&amp; s[i] &lt;= 'Z') s[i] = (char)(s[i] - 'A' + 'a');\r\n    return new String(s);\r\n  }\r\n}<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true\"># Author: Huahua, 40 ms\r\nclass Solution:\r\n  def toLowerCase(self, str):\r\n    ans = ''\r\n    for c in str:\r\n      if c &gt;= 'A' and c &lt;= 'Z':\r\n        ans += chr(ord(c) + 32)\r\n      else:\r\n        ans += c\r\n    return ans<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3 1-linear<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true\"># Author: Huahua, 68 ms\r\nclass Solution:\r\n  def toLowerCase(self, str):\r\n    return ''.join(chr(ord(c) + 32) if c &gt;= 'A' and c &lt;= 'Z' else c for c in str)<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. Solution Time complexity: O(n) Space complexity: O(1) \/\/&#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":[222,322,4],"class_list":["post-3086","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-lower-case","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3086","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=3086"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3086\/revisions"}],"predecessor-version":[{"id":3848,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3086\/revisions\/3848"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}