<?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>atoi Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/atoi/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/atoi/</link>
	<description></description>
	<lastBuildDate>Wed, 12 Sep 2018 16:37:54 +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>atoi Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/atoi/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 8. String to Integer (atoi)</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-8-string-to-integer-atoi/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-8-string-to-integer-atoi/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 12 Sep 2018 16:37:00 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[atoi]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[expression]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[overflow]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3931</guid>

					<description><![CDATA[<p>Problem Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then,&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-8-string-to-integer-atoi/">花花酱 LeetCode 8. String to Integer (atoi)</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>Implement <code>atoi</code> which converts a string to an integer.</p>
<p>The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.</p>
<p>The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.</p>
<p>If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.</p>
<p>If no valid conversion could be performed, a zero value is returned.</p>
<p><strong>Note:</strong></p>
<ul>
<li>Only the space character <code>' '</code> is considered as whitespace character.</li>
<li>Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2<sup>31</sup>,  2<sup>31 </sup>− 1]. If the numerical value is out of the range of representable values, INT_MAX (2<sup>31 </sup>− 1) or INT_MIN (−2<sup>31</sup>) is returned.</li>
</ul>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input:</strong> "42"
<strong>Output:</strong> 42
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input:</strong> "   -42"
<strong>Output:</strong> -42
<strong>Explanation:</strong> The first non-whitespace character is '-', which is the minus sign.
             Then take as many numerical digits as possible, which gets 42.
</pre>
<p><strong>Example 3:</strong></p>
<pre class="crayon:false"><strong>Input:</strong> "4193 with words"
<strong>Output:</strong> 4193
<strong>Explanation:</strong> Conversion stops at digit '3' as the next character is not a numerical digit.
</pre>
<p><strong>Example 4:</strong></p>
<pre class="crayon:false"><strong>Input:</strong> "words and 987"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The first non-whitespace character is 'w', which is not a numerical 
             digit or a +/- sign. Therefore no valid conversion could be performed.</pre>
<p><strong>Example 5:</strong></p>
<pre class="crayon:false"><strong>Input:</strong> "-91283472332"
<strong>Output:</strong> -2147483648
<strong>Explanation:</strong> The number "-91283472332" is out of the range of a 32-bit signed integer.
             Thefore INT_MIN (−2<sup>31</sup>) is returned.</pre>
<h1><strong>Solution: Simulation</strong></h1>
<p>You need to handle all corner cases in order to pass&#8230;</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(n)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int myAtoi(const string&amp; s) {
    long long ans = 0;
    int neg = 0;
    int pos = 0;
    bool p = false; // has '.'
    bool a = false; // has letters
    bool n = false; // has number
    for (int i = 0; i &lt; s.length(); ++i) {      
      if (s[i] == ' ') { if (n || neg || pos) break; }
      else if (s[i] == '-') { if (n) break; else neg++; }
      else if (s[i] == '+') { if (n) break; else pos++; }
      else if (s[i] == '.') p = true;
      else if (s[i] &gt;= '0' &amp;&amp; s[i] &lt;= '9' &amp;&amp; !p &amp;&amp; !a) {
        ans = ans * 10 + (s[i] - '0');
        n = true;
      } else {
        a = true;
      }
      if (ans / 10 &gt; INT_MAX) break; // ans can be &gt; 64 bit
    }

    if (neg) ans = -ans;

    if (ans &gt; INT_MAX) ans = INT_MAX;
    if (ans &lt; INT_MIN) ans = INT_MIN;

    if (pos + neg &gt; 1) ans = 0;

    return ans;
  }
};</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-8-string-to-integer-atoi/">花花酱 LeetCode 8. String to Integer (atoi)</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/simulation/leetcode-8-string-to-integer-atoi/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
