{"id":2322,"date":"2018-03-23T23:23:54","date_gmt":"2018-03-24T06:23:54","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2322"},"modified":"2018-03-24T01:48:58","modified_gmt":"2018-03-24T08:48:58","slug":"leetcode-434-number-of-segments-in-a-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-434-number-of-segments-in-a-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 434. Number of Segments in a String"},"content":{"rendered":"<div class=\"question-description\">\n<h1><strong>Problem<\/strong><\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u7684\u975e\u7a7a\u767d\u5b57\u7b26\u8fde\u7eed\u7684\u5b57\u4e32\u6570\u91cf\u3002<\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/number-of-segments-in-a-string\/description\/\">https:\/\/leetcode.com\/problems\/number-of-segments-in-a-string\/description\/<\/a><\/p>\n<p>Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.<\/p>\n<p>Please note that the string does not contain any\u00a0<b>non-printable<\/b>\u00a0characters.<\/p>\n<p><b>Example:<\/b><\/p>\n<pre class=\"crayon:false \"><b>Input:<\/b> \"Hello, my name is John\"\r\n<b>Output:<\/b> 5\r\n<\/pre>\n<\/div>\n<h1>Solution<\/h1>\n<p>Be aware of special cases: like &#8221;\u00a0 \u00a0 \u00a0 &#8220;.<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">class Solution {\r\npublic:\r\n  int countSegments(string s) {\r\n    if (s.empty()) return 0;\r\n    int ans = 0;\r\n    int i = 0;\r\n    while (i &lt; s.length()) {\r\n      while (i &lt; s.length() &amp;&amp; s[i] == ' ') ++i;\r\n      if (i == s.length()) break;\r\n      while (i &lt; s.length() &amp;&amp; s[i] != ' ') ++i;\r\n      ++ans;\r\n      ++i;\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>Python3<\/p>\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 40 ms\r\n\"\"\"\r\nclass Solution:\r\n  def countSegments(self, s):\r\n    return len(s.split())\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u7684\u975e\u7a7a\u767d\u5b57\u7b26\u8fde\u7eed\u7684\u5b57\u4e32\u6570\u91cf\u3002 https:\/\/leetcode.com\/problems\/number-of-segments-in-a-string\/description\/ Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters. Please&#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,267,4],"class_list":["post-2322","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-segment","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2322","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=2322"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2322\/revisions"}],"predecessor-version":[{"id":2331,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2322\/revisions\/2331"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}