{"id":5649,"date":"2019-09-30T08:40:43","date_gmt":"2019-09-30T15:40:43","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5649"},"modified":"2019-09-30T08:41:21","modified_gmt":"2019-09-30T15:41:21","slug":"leetcode-58-length-of-last-word","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-58-length-of-last-word\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 58. Length of Last Word"},"content":{"rendered":"\n<p>Given a string&nbsp;<em>s<\/em>&nbsp;consists of upper\/lower-case alphabets and empty space characters&nbsp;<code>' '<\/code>, return the length of last word in the string.<\/p>\n\n\n\n<p>If the last word does not exist, return 0.<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;A word is defined as a character sequence consists of non-space characters only.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> \"Hello World\"\n<strong>Output:<\/strong> 5<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution:<\/strong><\/h2>\n\n\n\n<p>There can be tailing spaces, e.g. &#8220;abc   &#8220;, we need to trim the string first and then scan the string in reverse order until a space or reach the beginning of the string.<\/p>\n\n\n\n<p>Time complexity: O(n)<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 lengthOfLastWord(string s) {    \n    int i = s.length() - 1;\n    int l = 0;\n    while (i >= 0 && s[i] == ' ') --i;\n    while (i >= 0 && s[i] != ' ') {\n      --i;\n      ++l;\n    }\n    return l;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s&nbsp;consists of upper\/lower-case alphabets and empty space characters&nbsp;&#8216; &#8216;, return the length of last word in the string. If the last word does&#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,503,4,109],"class_list":["post-5649","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-length","tag-string","tag-word","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5649","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=5649"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5649\/revisions"}],"predecessor-version":[{"id":5652,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5649\/revisions\/5652"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}