<?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>format Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/format/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/format/</link>
	<description></description>
	<lastBuildDate>Sun, 20 Dec 2020 09:06:59 +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>format Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/format/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1694. Reformat Phone Number</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-1694-reformat-phone-number/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-1694-reformat-phone-number/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 20 Dec 2020 09:03:59 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[format]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7820</guid>

					<description><![CDATA[<p>You are given a phone number as a string&#160;number.&#160;number&#160;consists of digits, spaces&#160;' ', and/or dashes&#160;'-'. You would like to reformat the phone number in a&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1694-reformat-phone-number/">花花酱 LeetCode 1694. Reformat Phone Number</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>You are given a phone number as a string&nbsp;<code>number</code>.&nbsp;<code>number</code>&nbsp;consists of digits, spaces&nbsp;<code>' '</code>, and/or dashes&nbsp;<code>'-'</code>.</p>



<p>You would like to reformat the phone number in a certain manner. Firstly,&nbsp;<strong>remove</strong>&nbsp;all spaces and dashes. Then,&nbsp;<strong>group</strong>&nbsp;the digits from left to right into blocks of length 3&nbsp;<strong>until</strong>&nbsp;there are 4 or fewer digits. The final digits are then grouped as follows:</p>



<ul><li>2 digits: A single block of length 2.</li><li>3 digits: A single block of length 3.</li><li>4 digits: Two blocks of length 2 each.</li></ul>



<p>The blocks are then joined by dashes. Notice that the reformatting process should&nbsp;<strong>never</strong>&nbsp;produce any blocks of length 1 and produce&nbsp;<strong>at most</strong>&nbsp;two blocks of length 2.</p>



<p>Return&nbsp;<em>the phone number after formatting.</em></p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> number = "1-23-45 6"
<strong>Output:</strong> "123-456"
<strong>Explanation:</strong> The digits are "123456".
Step 1: There are more than 4 digits, so group the next 3 digits. The 1st block is "123".
Step 2: There are 3 digits remaining, so put them in a single block of length 3. The 2nd block is "456".
Joining the blocks gives "123-456".
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> number = "123 4-567"
<strong>Output:</strong> "123-45-67"
<strong>Explanation: </strong>The digits are "1234567".
Step 1: There are more than 4 digits, so group the next 3 digits. The 1st block is "123".
Step 2: There are 4 digits left, so split them into two blocks of length 2. The blocks are "45" and "67".
Joining the blocks gives "123-45-67".
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> number = "123 4-5678"
<strong>Output:</strong> "123-456-78"
<strong>Explanation:</strong> The digits are "12345678".
Step 1: The 1st block is "123".
Step 2: The 2nd block is "456".
Step 3: There are 2 digits left, so put them in a single block of length 2. The 3rd block is "78".
Joining the blocks gives "123-456-78".
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> number = "12"
<strong>Output:</strong> "12"
</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> number = "--17-5 229 35-39475 "
<strong>Output:</strong> "175-229-353-94-75"
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>2 &lt;= number.length &lt;= 100</code></li><li><code>number</code>&nbsp;consists of digits&nbsp;and the characters&nbsp;<code>'-'</code>&nbsp;and&nbsp;<code>' '</code>.</li><li>There are at least&nbsp;<strong>two</strong>&nbsp;digits in&nbsp;<code>number</code>.</li></ul>



<h2><strong>Solution:</strong></h2>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  string reformatNumber(string number) {
    string ans;
    const int total = count_if(begin(number), end(number), 
                               [](char c) { return isdigit(c); });
    int l = 0;
    for (char c : number) {
      if (!isdigit(c)) continue;      
      ans += c;
      ++l;
      if ((l % 3 == 0 &amp;&amp; total - l &gt;= 4) ||
          (total % 3 != 0 &amp;&amp; total - l == 2)
          || (total % 3 == 0 &amp;&amp; total - l == 3))
        ans += &quot;-&quot;;
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1694-reformat-phone-number/">花花酱 LeetCode 1694. Reformat Phone Number</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-1694-reformat-phone-number/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 655. Print Binary Tree</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-655-print-binary-tree/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-655-print-binary-tree/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 17 Aug 2018 07:03:13 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[format]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3555</guid>

					<description><![CDATA[<p>Problem Print a binary tree in an m*n 2D string array following these rules: The row number m should be equal to the height of the given&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-655-print-binary-tree/">花花酱 LeetCode 655. Print Binary Tree</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/ipIL1qVAazk?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1><strong>Problem</strong></h1>
<p>Print a binary tree in an m*n 2D string array following these rules:</p>
<ol>
<li>The row number <code>m</code> should be equal to the height of the given binary tree.</li>
<li>The column number <code>n</code> should always be an odd number.</li>
<li>The root node&#8217;s value (in string format) should be put in the exactly middle of the first row it can be put. The column and the row where the root node belongs will separate the rest space into two parts (<b>left-bottom part and right-bottom part</b>). You should print the left subtree in the left-bottom part and print the right subtree in the right-bottom part. The left-bottom part and the right-bottom part should have the same size. Even if one subtree is none while the other is not, you don&#8217;t need to print anything for the none subtree but still need to leave the space as large as that for the other subtree. However, if two subtrees are none, then you don&#8217;t need to leave space for both of them.</li>
<li>Each unused space should contain an empty string <code>""</code>.</li>
<li>Print the subtrees following the same rules.</li>
</ol>
<p><b>Example 1:</b></p>
<pre class="crayon:false"><b>Input:</b>
     1
    /
   2
<b>Output:</b>
[["", "1", ""],
 ["2", "", ""]]
</pre>
<p><b>Example 2:</b></p>
<pre class="crayon:false"><b>Input:</b>
     1
    / \
   2   3
    \
     4
<b>Output:</b>
[["", "", "", "1", "", "", ""],
 ["", "2", "", "", "", "3", ""],
 ["", "", "4", "", "", "", ""]]
</pre>
<p><b>Example 3:</b></p>
<pre class="crayon:false"><b>Input:</b>
      1
     / \
    2   5
   / 
  3 
 / 
4 
<b>Output:</b>

[["",  "",  "", "",  "", "", "", "1", "",  "",  "",  "",  "", "", ""]
 ["",  "",  "", "2", "", "", "", "",  "",  "",  "",  "5", "", "", ""]
 ["",  "3", "", "",  "", "", "", "",  "",  "",  "",  "",  "", "", ""]
 ["4", "",  "", "",  "", "", "", "",  "",  "",  "",  "",  "", "", ""]]
</pre>
<p><b>Note:</b> The height of binary tree is in the range of [1, 10].</p>
<h1><strong>Solution: Recursion</strong></h1>
<p>Compute the layers h of the tree.</p>
<p>shape of the output matrix: h * w (w = 2^h &#8211; 1)</p>
<p>pre-order to fill the output matrix</p>
<p>first layer&#8217;s root: y1 = 0, x1 = (l1 + r1) / 2 = (0 + w &#8211; 1) / 2 (center)</p>
<p>first layer&#8217;s left child (2nd layer): y2 = 1, x2 = (l2 + r2) = (l1 + (x1 &#8211; 1)) / 2 (center of the left half)</p>
<p>first layer&#8217;s right child(2nd layer): y1 = 2, x2 = (l2&#8242; + r2&#8242;) = ((x1+1) + r1) / 2 (center of the right half)</p>
<p>&#8230;</p>
<p>Time complexity: O(m*n)</p>
<p>Space complexity: O(m*n)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 4 ms
class Solution {
public:
  vector&lt;vector&lt;string&gt;&gt; printTree(TreeNode* root) {
    int h = getHeight(root);
    int w = (1 &lt;&lt; h) - 1; // 2 ^ h - 1
    vector&lt;vector&lt;string&gt;&gt; ans(h, vector&lt;string&gt;(w, ""));
    fill(root, ans, 0, 0, w - 1);
    return ans;
  }
private:
  int getHeight(TreeNode* root) {
    if (!root) return 0;
    return max(getHeight(root-&gt;left), getHeight(root-&gt;right)) + 1;
  }
    
  void fill(TreeNode* root, vector&lt;vector&lt;string&gt;&gt;&amp; ans, int h, int l, int r){
    if (!root) return;
    int mid = (l + r) / 2;
    ans[h][mid] = std::to_string(root-&gt;val);
    fill(root-&gt;left, ans, h + 1, l, mid - 1);
    fill(root-&gt;right, ans, h + 1, mid + 1, r);
  }
};</pre><p></div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 44 ms
"""
class Solution:
  def printTree(self, root):
    def height(root):
      if not root: return 0
      return max(height(root.left), height(root.right)) + 1
    
    def fill(root, h, l, r):
      if not root: return
      mid = (l + r) // 2
      m[h][mid] = str(root.val)
      if root.left: fill(root.left, h + 1, l, mid - 1)
      if root.right: fill(root.right, h + 1, mid + 1, r)
    
    h = height(root)
    w = 2**h - 1
    m = [[""] * w for _ in range(h)]
    fill(root, 0, 0, w - 1)
    return m</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-655-print-binary-tree/">花花酱 LeetCode 655. Print Binary Tree</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/tree/leetcode-655-print-binary-tree/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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>
	</channel>
</rss>
