<?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>fraction Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/fraction/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/fraction/</link>
	<description></description>
	<lastBuildDate>Wed, 12 Sep 2018 07:02:52 +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>fraction Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/fraction/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 592. Fraction Addition and Subtraction</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-592-fraction-addition-and-subtraction/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-592-fraction-addition-and-subtraction/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 07 Aug 2018 05:11:43 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[fraction]]></category>
		<category><![CDATA[gcd]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3461</guid>

					<description><![CDATA[<p>Problem Given a string representing an expression of fraction addition and subtraction, you need to return the calculation result in string format. The final result&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-592-fraction-addition-and-subtraction/">花花酱 LeetCode 592. Fraction Addition and Subtraction</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__3U1T">
<div>
<h1><strong>Problem</strong></h1>
<p>Given a string representing an expression of fraction addition and subtraction, you need to return the calculation result in string format. The final result should be <a href="https://en.wikipedia.org/wiki/Irreducible_fraction">irreducible fraction</a>. If your final result is an integer, say <code>2</code>, you need to change it to the format of fraction that has denominator <code>1</code>. So in this case, <code>2</code> should be converted to <code>2/1</code>.</p>
<p><b>Example 1:</b></p>
<pre class="crayon:false"><b>Input:</b>"-1/2+1/2"
<b>Output:</b> "0/1"
</pre>
<p><b>Example 2:</b></p>
<pre class="crayon:false"><b>Input:</b>"-1/2+1/2+1/3"
<b>Output:</b> "1/3"
</pre>
<p><b>Example 3:</b></p>
<pre class="crayon:false"><b>Input:</b>"1/3-1/2"
<b>Output:</b> "-1/6"
</pre>
<p><b>Example 4:</b></p>
<pre class="crayon:false"><b>Input:</b>"5/3+1/3"
<b>Output:</b> "2/1"
</pre>
<p><b>Note:</b></p>
<ol>
<li>The input string only contains <code>'0'</code> to <code>'9'</code>, <code>'/'</code>, <code>'+'</code> and <code>'-'</code>. So does the output.</li>
<li>Each fraction (input and output) has format <code>±numerator/denominator</code>. If the first input fraction or the output is positive, then <code>'+'</code> will be omitted.</li>
<li>The input only contains valid <b>irreducible fractions</b>, where the <b>numerator</b> and <b>denominator</b> of each fraction will always be in the range [1,10]. If the denominator is 1, it means this fraction is actually an integer in a fraction format defined above.</li>
<li>The number of given fractions will be in the range [1,10].</li>
<li>The numerator and denominator of the <b>final result</b> are guaranteed to be valid and in the range of 32-bit int.</li>
</ol>
</div>
</div>
<h1><strong>Solution: Math</strong></h1>
<p>a/b+c/d = (a*d + b * c) / (b * d)</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</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:
  string fractionAddition(string expression) {
    int a = 0;
    int b = 1;    
    int c;
    int d;
    char slash;
    istringstream in(expression);
    while (in &gt;&gt; c &gt;&gt; slash &gt;&gt; d) {
      a = a * d + b * c;
      b *= d;
      int gcd = abs(__gcd(a, b));
      a /= gcd;
      b /= gcd;      
    }
    return to_string(a) + "/" + to_string(b);
  }
};</pre><p></div><h2 class="tabtitle">C++/class</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 0 ms
class Fraction {
public:
  Fraction(int a, int b): a_(a), b_(b) {}
  Fraction&amp; operator+=(const Fraction&amp; f) {
    a_ = a_ * f.b_ + b_ * f.a_;
    b_ = b_ * f.b_;
    int d = abs(__gcd(a_, b_));
    a_ /= d;
    b_ /= d;
    return *this;
  }
  
  string toString() {
    return to_string(a_) + "/" + to_string(b_);
  }
private:
  int a_; // hold the sign, e.g. can be +-.
  int b_; // always &gt; 0.
};

class Solution {
public:
    string fractionAddition(string expression) {
      Fraction f(0, 1);
      int a;
      int b;
      char op;
      istringstream in(expression);
      while (in &gt;&gt; a &gt;&gt; op &gt;&gt; b)
        f += Fraction(a, b);  
      return f.toString();
    }
};</pre><p></div></div></p>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-592-fraction-addition-and-subtraction/">花花酱 LeetCode 592. Fraction Addition and Subtraction</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/math/leetcode-592-fraction-addition-and-subtraction/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
