{"id":6782,"date":"2020-05-17T18:45:57","date_gmt":"2020-05-18T01:45:57","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6782"},"modified":"2020-05-17T18:47:44","modified_gmt":"2020-05-18T01:47:44","slug":"leetcode-1447-simplified-fractions","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1447-simplified-fractions\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1447. Simplified Fractions"},"content":{"rendered":"\n<p>Given an integer&nbsp;<code>n<\/code>, return a list of all&nbsp;<strong>simplified<\/strong>&nbsp;fractions between 0 and 1 (exclusive) such that the denominator is less-than-or-equal-to&nbsp;<code>n<\/code>. The fractions can be in&nbsp;<strong>any<\/strong>&nbsp;order.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 2\n<strong>Output:<\/strong> [\"1\/2\"]\n<strong>Explanation: <\/strong>\"1\/2\" is the only unique fraction with a denominator less-than-or-equal-to 2.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 3\n<strong>Output:<\/strong> [\"1\/2\",\"1\/3\",\"2\/3\"]\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 4\n<strong>Output:<\/strong> [\"1\/2\",\"1\/3\",\"1\/4\",\"2\/3\",\"3\/4\"]\n<strong>Explanation: <\/strong>\"2\/4\" is not a simplified fraction because it can be simplified to \"1\/2\".<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 1\n<strong>Output:<\/strong> []\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: GCD<\/strong><\/h2>\n\n\n\n<p>if gcd(a, b) == 1 then a\/b is a simplified frication.<\/p>\n\n\n\n<p>std::gcd is available since c++17.<\/p>\n\n\n\n<p>Time complexity: O(n^2logn)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<string> simplifiedFractions(int n) {\n    vector<string> ans;\n    for (int i = 1; i < n; ++i)\n      for (int j = i + 1; j <= n; ++j)\n        if (gcd(i, j) == 1)\n          ans.push_back(to_string(i) + \"\/\" + to_string(j));\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer&nbsp;n, return a list of all&nbsp;simplified&nbsp;fractions between 0 and 1 (exclusive) such that the denominator is less-than-or-equal-to&nbsp;n. The fractions can be in&nbsp;any&nbsp;order. Example&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[359,31,177],"class_list":["post-6782","post","type-post","status-publish","format-standard","hentry","category-math","tag-gcd","tag-math","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6782","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6782"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6782\/revisions"}],"predecessor-version":[{"id":6784,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6782\/revisions\/6784"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}