<?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>valid Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/valid/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/valid/</link>
	<description></description>
	<lastBuildDate>Sun, 15 Apr 2018 08:04:16 +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>valid Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/valid/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 816. Ambiguous Coordinates</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-816-ambiguous-coordinates/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-816-ambiguous-coordinates/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 15 Apr 2018 03:15:39 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[format]]></category>
		<category><![CDATA[number]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[valid]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2484</guid>

					<description><![CDATA[<p>Problem 题目大意：把一串数字分成两段，输出所有合法的分法（可以加小数点）。 https://leetcode.com/problems/ambiguous-coordinates/description/ We had some 2-dimensional coordinates, like "(1, 3)" or "(2, 0.5)".  Then, we removed all commas, decimal points, and spaces, and ended up with the string S. &#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-816-ambiguous-coordinates/">花花酱 LeetCode 816. Ambiguous Coordinates</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>题目大意：把一串数字分成两段，输出所有合法的分法（可以加小数点）。</p>
<p><a href="https://leetcode.com/problems/ambiguous-coordinates/description/">https://leetcode.com/problems/ambiguous-coordinates/description/</a></p>
<p>We had some 2-dimensional coordinates, like <code>"(1, 3)"</code> or <code>"(2, 0.5)"</code>.  Then, we removed all commas, decimal points, and spaces, and ended up with the string <code>S</code>.  Return a list of strings representing all possibilities for what our original coordinates could have been.</p>
<p>Our original representation never had extraneous zeroes, so we never started with numbers like &#8220;00&#8221;, &#8220;0.0&#8221;, &#8220;0.00&#8221;, &#8220;1.0&#8221;, &#8220;001&#8221;, &#8220;00.01&#8221;, or any other number that can be represented with less digits.  Also, a decimal point within a number never occurs without at least one digit occuring before it, so we never started with numbers like &#8220;.1&#8221;.</p>
<p>The final answer list can be returned in any order.  Also note that all coordinates in the final answer have exactly one space between them (occurring after the comma.)</p>
<pre class="crayon:false"><strong>Example 1:</strong>
<strong>Input:</strong> "(123)"
<strong>Output:</strong> ["(1, 23)", "(12, 3)", "(1.2, 3)", "(1, 2.3)"]
</pre>
<pre class="crayon:false"><strong>Example 2:</strong>
<strong>Input:</strong> "(00011)"
<strong>Output:</strong>  ["(0.001, 1)", "(0, 0.011)"]
<strong>Explanation:</strong> 
0.0, 00, 0001 or 00.01 are not allowed.
</pre>
<pre class="crayon:false"><strong>Example 3:</strong>
<strong>Input:</strong> "(0123)"
<strong>Output:</strong> ["(0, 123)", "(0, 12.3)", "(0, 1.23)", "(0.1, 23)", "(0.1, 2.3)", "(0.12, 3)"]
</pre>
<pre class="crayon:false"><strong>Example 4:</strong>
<strong>Input:</strong> "(100)"
<strong>Output:</strong> [(10, 0)]
<strong>Explanation:</strong> 
1.0 is not allowed.
</pre>
<h1><strong>Solution: Brute Force</strong></h1>
<p>Time complexity: O(n^3)</p>
<p>Space complexity: O(n)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 16 ms
class Solution {
public:
  vector&lt;string&gt; ambiguousCoordinates(string S) {
    vector&lt;string&gt; ans;
    int n = S.length();
    for (int l1 = 1; l1 &lt;= n - 3; ++l1) {      
      int l2 = n - l1 - 2;
      string s1 = S.substr(1, l1);
      string s2 = S.substr(l1 + 1, l2);      
      if (valid(s1) &amp;&amp; valid(s2))
        ans.push_back("(" + s1 + ", " + s2 + ")");
      
      for (int i = 0; i &lt; l1; ++i) {
        string ts1 = s1;
        if (i &gt; 0) ts1.insert(i, ".");        
        if (!valid(ts1)) continue;
        for (int j = 0; j &lt; l2; ++j) {
          if (i == 0 &amp;&amp; j == 0) continue;
          string ts2 = s2;
          if (j &gt; 0) ts2.insert(j, ".");          
          if (!valid(ts2)) continue;          
          ans.push_back("(" + ts1 + ", " + ts2 + ")");
        }
      }
    }
    
    return ans;
  }
private:    
  bool valid(const string&amp; s) {
    bool p = false; // has "."
    bool d = false; // has digits in front of "."
    int zeros = 0;  // leading zeros
    int i = 0;
    while (s[zeros] == '0' &amp;&amp; zeros &lt; s.length()) ++zeros;
    for (int i = zeros; i &lt; s.length(); ++i) {
      if (!p &amp;&amp; isdigit(s[i])) d = true;
      if (s[i] == '.') p = true;
    }
    if (zeros == 1 &amp;&amp; s != "0" &amp;&amp; !p || zeros &gt; 1) return false;
    if (p &amp;&amp; (s.back() == '0' || s.back() == '.' || zeros &gt; 0 &amp;&amp; d)) return false;    
    return true;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-816-ambiguous-coordinates/">花花酱 LeetCode 816. Ambiguous Coordinates</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-816-ambiguous-coordinates/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 593. Valid Square</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-593-valid-square/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-593-valid-square/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 30 Mar 2018 15:56:03 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[square]]></category>
		<category><![CDATA[valid]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2397</guid>

					<description><![CDATA[<p>Problem 题目大意：给你四个点判断能否构成正方形。 https://leetcode.com/problems/valid-square/description/ Given the coordinates of four points in 2D space, return whether the four points could construct a square. The coordinate (x,y) of&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-593-valid-square/">花花酱 LeetCode 593. Valid Square</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>题目大意：给你四个点判断能否构成正方形。</p>
<p><a href="https://leetcode.com/problems/valid-square/description/">https://leetcode.com/problems/valid-square/description/</a></p>
<p>Given the coordinates of four points in 2D space, return whether the four points could construct a square.</p>
<p>The coordinate (x,y) of a point is represented by an integer array with two integers.</p>
<p><b>Example:</b></p>
<pre class="crayon:false "><b>Input:</b> p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
<b>Output:</b> True
</pre>
<p>Note:</p>
<ol>
<li>All the input integers are in the range [-10000, 10000].</li>
<li>A valid square has four equal sides with positive length and four equal angles (90-degree angles).</li>
<li>Input points have no order.</li>
</ol>
<h1><strong>Solution</strong></h1>
<p>Time complexity: O(1)</p>
<p>Space complexity: O(1)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 8 ms
class Solution {
public:
  bool validSquare(vector&lt;int&gt;&amp; p1, vector&lt;int&gt;&amp; p2, vector&lt;int&gt;&amp; p3, vector&lt;int&gt;&amp; p4) {
    set&lt;int&gt; s{distSqr(p1, p2), distSqr(p1, p3), distSqr(p1, p4), distSqr(p2, p3), distSqr(p2, p4), distSqr(p3, p4)};
    return !s.count(0) &amp;&amp; s.size() == 2;
  }
private:
  static inline int distSqr(const vector&lt;int&gt;&amp; p1, const vector&lt;int&gt;&amp; p2) {
    return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]);
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-593-valid-square/">花花酱 LeetCode 593. Valid Square</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/geometry/leetcode-593-valid-square/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
