<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>segment Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/segment/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/segment/</link>
	<description></description>
	<lastBuildDate>Sat, 24 Mar 2018 08:48:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>segment Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/segment/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 434. Number of Segments in a String</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-434-number-of-segments-in-a-string/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-434-number-of-segments-in-a-string/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 24 Mar 2018 06:23:54 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[segment]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2322</guid>

					<description><![CDATA[<p>Problem 题目大意：返回字符串中的非空白字符连续的字串数量。 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>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-434-number-of-segments-in-a-string/">花花酱 LeetCode 434. Number of Segments in a String</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="question-description">
<h1><strong>Problem</strong></h1>
<p>题目大意：返回字符串中的非空白字符连续的字串数量。</p>
<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>
<p>Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.</p>
<p>Please note that the string does not contain any <b>non-printable</b> characters.</p>
<p><b>Example:</b></p>
<pre class="crayon:false "><b>Input:</b> "Hello, my name is John"
<b>Output:</b> 5
</pre>
</div>
<h1>Solution</h1>
<p>Be aware of special cases: like &#8221;      &#8220;.</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<p>C++</p><pre class="crayon-plain-tag">class Solution {
public:
  int countSegments(string s) {
    if (s.empty()) return 0;
    int ans = 0;
    int i = 0;
    while (i &lt; s.length()) {
      while (i &lt; s.length() &amp;&amp; s[i] == ' ') ++i;
      if (i == s.length()) break;
      while (i &lt; s.length() &amp;&amp; s[i] != ' ') ++i;
      ++ans;
      ++i;
    }
    return ans;
  }
};</pre><p>Python3</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 40 ms
"""
class Solution:
  def countSegments(self, s):
    return len(s.split())</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-434-number-of-segments-in-a-string/">花花酱 LeetCode 434. Number of Segments in a String</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/string/leetcode-434-number-of-segments-in-a-string/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
