{"id":44,"date":"2015-01-01T14:45:10","date_gmt":"2015-01-01T14:45:10","guid":{"rendered":"http:\/\/emacslisp.com\/?p=44"},"modified":"2015-06-07T16:52:07","modified_gmt":"2015-06-07T16:52:07","slug":"leetcode-oj-length-of-last-word","status":"publish","type":"post","link":"http:\/\/emacslisp.com\/?p=44","title":{"rendered":"LeetCode OJ: Length of Last Word"},"content":{"rendered":"<div class=\"question-title\">\n<h3 style=\"display: inline;\">Length of Last Word<\/h3>\n<p><span class=\"total-ac text-info\">Total Accepted: <strong>29154<\/strong><\/span> <span class=\"total-submit text-info\">Total Submissions: <strong>100944<\/strong><\/span><\/p>\n<div class=\"pull-right btn-group right-pad\"><\/div>\n<\/div>\n<p>Given a string <i>s<\/i> consists of upper\/lower-case alphabets and empty space characters <code>' '<\/code>, return the length of last word in the string.<\/p>\n<p>If the last word does not exist, return 0.<\/p>\n<p><b>Note:<\/b> A word is defined as a character sequence consists of non-space characters only.<\/p>\n<p>For example,<br \/>\nGiven <i>s<\/i> = <code>\"Hello World\"<\/code>,<br \/>\nreturn <code>5<\/code>.<\/p>\n<p>https:\/\/oj.leetcode.com\/problems\/length-of-last-word\/<\/p>\n<p><strong>Solution: see the comment in the code , writed in Java<\/strong><\/p>\n<pre lang=\"java\" line=\"1\"> \r\n\r\npublic static int lengthOfLastWord(String s) {\r\n    if(s.length() == 0) return 0; \/\/ input may be \"\"\r\n\r\n    int result = -1;\r\n\r\n    int skipEmpty = 0;\r\n    \/\/ skip empty string at the end of input string s\r\n    for(int i= s.length() - 1;i>=0;i--)\r\n        {\r\n            if(s.charAt(i) == ' ') continue;\r\n            else {\r\n                skipEmpty = i;\r\n                break;\r\n            }\r\n        }\r\n    \/\/Start to Calculate\r\n    for(int i= skipEmpty;i>=0;i--)\r\n        {\r\n            if(s.charAt(i) != ' ') continue;\r\n            else {\r\n                result = i;\r\n                break;\r\n            }\r\n\r\n        }\r\n\r\n    return skipEmpty - result;\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Length of Last Word Total Accepted: 29154 Total Submissions: 100944 Given a string s consists of upper\/lower-case alphabets and empty space characters &#8216; &#8216;, return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space characters [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-44","post","type-post","status-publish","format-standard","hentry","category-algorithm"],"_links":{"self":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/44","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=44"}],"version-history":[{"count":4,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/44\/revisions"}],"predecessor-version":[{"id":108,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/44\/revisions\/108"}],"wp:attachment":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}