<?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>generation Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/generation/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/generation/</link>
	<description></description>
	<lastBuildDate>Sun, 10 Apr 2022 06:04:15 +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>generation Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/generation/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2231. Largest Number After Digit Swaps by Parity</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2231-largest-number-after-digit-swaps-by-parity/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2231-largest-number-after-digit-swaps-by-parity/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 10 Apr 2022 06:03:37 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[generation]]></category>
		<category><![CDATA[largest]]></category>
		<category><![CDATA[sorting]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9633</guid>

					<description><![CDATA[<p>You are given a positive integer&#160;num. You may swap any two digits of&#160;num&#160;that have the same&#160;parity&#160;(i.e. both odd digits or both even digits). Return&#160;the&#160;largest&#160;possible value&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2231-largest-number-after-digit-swaps-by-parity/">花花酱 LeetCode 2231. Largest Number After Digit Swaps by Parity</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 positive integer&nbsp;<code>num</code>. You may swap any two digits of&nbsp;<code>num</code>&nbsp;that have the same&nbsp;<strong>parity</strong>&nbsp;(i.e. both odd digits or both even digits).</p>



<p>Return<em>&nbsp;the&nbsp;<strong>largest</strong>&nbsp;possible value of&nbsp;</em><code>num</code><em>&nbsp;after&nbsp;<strong>any</strong>&nbsp;number of swaps.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> num = 1234
<strong>Output:</strong> 3412
<strong>Explanation:</strong> Swap the digit 3 with the digit 1, this results in the number 3214.
Swap the digit 2 with the digit 4, this results in the number 3412.
Note that there may be other sequences of swaps but it can be shown that 3412 is the largest possible number.
Also note that we may not swap the digit 4 with the digit 1 since they are of different parities.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> num = 65875
<strong>Output:</strong> 87655
<strong>Explanation:</strong> Swap the digit 8 with the digit 6, this results in the number 85675.
Swap the first digit 5 with the digit 7, this results in the number 87655.
Note that there may be other sequences of swaps but it can be shown that 87655 is the largest possible number.
</pre>



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



<ul><li><code>1 &lt;= num &lt;= 10<sup>9</sup></code></li></ul>



<p>Solution:</p>



<p>Put all even digits into one array, all odd digits into another one, all digits into the third. Sort two arrays, and generate a new number from sorted arrays.</p>



<p>Time complexity: O(logn*loglogn)<br>Space complexity: O(logn)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int largestInteger(int num) {
    vector&lt;int&gt; odd, even, org;
    for (int cur = num; cur; cur /= 10) {
      ((cur &amp; 1) ? odd : even).push_back(cur % 10);
      org.push_back(cur % 10);
    }
    sort(begin(odd), end(odd));
    sort(begin(even), end(even));    
    int ans = 0;
    for (int i = 0; i &lt; org.size(); ++i) {
      if (i) ans *= 10;
      auto&amp; cur = (org[org.size() - i - 1] &amp; 1) ? odd : even;
      ans += cur.back();
      cur.pop_back();
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2231-largest-number-after-digit-swaps-by-parity/">花花酱 LeetCode 2231. Largest Number After Digit Swaps by Parity</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/algorithms/array/leetcode-2231-largest-number-after-digit-swaps-by-parity/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1304. Find N Unique Integers Sum up to Zero</title>
		<link>https://zxi.mytechroad.com/blog/generation/leetcode-1304-find-n-unique-integers-sum-up-to-zero/</link>
					<comments>https://zxi.mytechroad.com/blog/generation/leetcode-1304-find-n-unique-integers-sum-up-to-zero/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 29 Dec 2019 18:52:37 +0000</pubDate>
				<category><![CDATA[Generation]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[generation]]></category>
		<category><![CDATA[O(n)]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6018</guid>

					<description><![CDATA[<p>Given an integer&#160;n, return&#160;any&#160;array containing&#160;n&#160;unique&#160;integers such that they add up to 0. Example 1: Input: n = 5 Output: [-7,-1,1,3,4] Explanation: These arrays also are&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/generation/leetcode-1304-find-n-unique-integers-sum-up-to-zero/">花花酱 LeetCode 1304. Find N Unique Integers Sum up to Zero</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>Given an integer&nbsp;<code>n</code>, return&nbsp;<strong>any</strong>&nbsp;array containing&nbsp;<code>n</code>&nbsp;<strong>unique</strong>&nbsp;integers such that they add up to 0.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 5
<strong>Output:</strong> [-7,-1,1,3,4]
<strong>Explanation:</strong> These arrays also are accepted [-5,-1,1,2,3] , [-3,-1,2,-2,4].
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 3
<strong>Output:</strong> [-1,0,1]
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 1
<strong>Output:</strong> [0]
</pre>



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



<ul><li><code>1 &lt;= n &lt;= 1000</code></li></ul>



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



<p>Use numbers from {-n/2, &#8230; n/2} + {0 if n is odd}</p>



<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:
  vector&lt;int&gt; sumZero(int n) {
    vector&lt;int&gt; ans;
    for (int i = 1; i &lt;= n / 2; ++i) {
      ans.push_back(i);
      ans.push_back(-i);
    }
    if (ans.size() != n) ans.push_back(0);
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/generation/leetcode-1304-find-n-unique-integers-sum-up-to-zero/">花花酱 LeetCode 1304. Find N Unique Integers Sum up to Zero</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/generation/leetcode-1304-find-n-unique-integers-sum-up-to-zero/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
