From 2ac21df2d097adeae6b60ed87674ab35aa57af08 Mon Sep 17 00:00:00 2001 From: Abhinandan <93651229+AE-Hertz@users.noreply.github.com> Date: Fri, 23 Aug 2024 20:08:51 +0530 Subject: [PATCH 1/4] feat: add solutions to lc problem: No.0592 --- .../Solution.js | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 solution/0500-0599/0592.Fraction Addition and Subtraction/Solution.js diff --git a/solution/0500-0599/0592.Fraction Addition and Subtraction/Solution.js b/solution/0500-0599/0592.Fraction Addition and Subtraction/Solution.js new file mode 100644 index 0000000000000..4f1a22bfe205a --- /dev/null +++ b/solution/0500-0599/0592.Fraction Addition and Subtraction/Solution.js @@ -0,0 +1,42 @@ +/** + * @param {string} expression + * @return {string} + */ +var fractionAddition = function(expression) { + let x = 0, y = 1; + + if (!expression.startsWith('-') && !expression.startsWith('+')) { + expression = '+' + expression; + } + + let i = 0; + const n = expression.length; + + while (i < n) { + const sign = expression[i] === '-' ? -1 : 1; + i++; + + let j = i; + while (j < n && expression[j] !== '+' && expression[j] !== '-') { + j++; + } + + const [a, b] = expression.slice(i, j).split('/').map(Number); + x = x * b + sign * a * y; + y *= b; + i = j; + } + + const gcd = (a, b) => { + while (b !== 0) { + [a, b] = [b, a % b]; + } + return Math.abs(a); + }; + + const z = gcd(x, y); + x = Math.floor(x / z); + y = Math.floor(y / z); + + return `${x}/${y}`; +}; From 2f84e7f109fea2761329ef2805c7885fe8ae18ea Mon Sep 17 00:00:00 2001 From: Abhinandan <93651229+AE-Hertz@users.noreply.github.com> Date: Fri, 23 Aug 2024 20:10:17 +0530 Subject: [PATCH 2/4] feat: add solutions to lc problem: No.0592 --- .../README_EN.md | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/solution/0500-0599/0592.Fraction Addition and Subtraction/README_EN.md b/solution/0500-0599/0592.Fraction Addition and Subtraction/README_EN.md index f2217c40bb8c9..ec53836110e1b 100644 --- a/solution/0500-0599/0592.Fraction Addition and Subtraction/README_EN.md +++ b/solution/0500-0599/0592.Fraction Addition and Subtraction/README_EN.md @@ -171,7 +171,52 @@ func gcd(a, b int) int { return gcd(b, a%b) } ``` +#### JavaScript +```js +/** + * @param {string} expression + * @return {string} + */ +var fractionAddition = function(expression) { + let x = 0, y = 1; + + if (!expression.startsWith('-') && !expression.startsWith('+')) { + expression = '+' + expression; + } + + let i = 0; + const n = expression.length; + + while (i < n) { + const sign = expression[i] === '-' ? -1 : 1; + i++; + + let j = i; + while (j < n && expression[j] !== '+' && expression[j] !== '-') { + j++; + } + + const [a, b] = expression.slice(i, j).split('/').map(Number); + x = x * b + sign * a * y; + y *= b; + i = j; + } + + const gcd = (a, b) => { + while (b !== 0) { + [a, b] = [b, a % b]; + } + return Math.abs(a); + }; + const z = gcd(x, y); + x = Math.floor(x / z); + y = Math.floor(y / z); + + return `${x}/${y}`; +}; + +``` From 0f61e4346b94de3273dedbc63fa84d2b95496fbd Mon Sep 17 00:00:00 2001 From: AE-Hertz Date: Fri, 23 Aug 2024 15:04:16 +0000 Subject: [PATCH 3/4] style: format code and docs with prettier --- .../0592.Fraction Addition and Subtraction/README_EN.md | 9 ++++++--- .../0592.Fraction Addition and Subtraction/Solution.js | 5 +++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/solution/0500-0599/0592.Fraction Addition and Subtraction/README_EN.md b/solution/0500-0599/0592.Fraction Addition and Subtraction/README_EN.md index ec53836110e1b..f7f073920fa37 100644 --- a/solution/0500-0599/0592.Fraction Addition and Subtraction/README_EN.md +++ b/solution/0500-0599/0592.Fraction Addition and Subtraction/README_EN.md @@ -171,14 +171,17 @@ func gcd(a, b int) int { return gcd(b, a%b) } ``` + #### JavaScript + ```js /** * @param {string} expression * @return {string} */ -var fractionAddition = function(expression) { - let x = 0, y = 1; +var fractionAddition = function (expression) { + let x = 0, + y = 1; if (!expression.startsWith('-') && !expression.startsWith('+')) { expression = '+' + expression; @@ -215,8 +218,8 @@ var fractionAddition = function(expression) { return `${x}/${y}`; }; - ``` + diff --git a/solution/0500-0599/0592.Fraction Addition and Subtraction/Solution.js b/solution/0500-0599/0592.Fraction Addition and Subtraction/Solution.js index 4f1a22bfe205a..67ec625258b22 100644 --- a/solution/0500-0599/0592.Fraction Addition and Subtraction/Solution.js +++ b/solution/0500-0599/0592.Fraction Addition and Subtraction/Solution.js @@ -2,8 +2,9 @@ * @param {string} expression * @return {string} */ -var fractionAddition = function(expression) { - let x = 0, y = 1; +var fractionAddition = function (expression) { + let x = 0, + y = 1; if (!expression.startsWith('-') && !expression.startsWith('+')) { expression = '+' + expression; From a4a3016d56b1985fa25e28a342081d9e833d61c2 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 24 Aug 2024 08:39:04 +0800 Subject: [PATCH 4/4] Update README.md --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/solution/0500-0599/0592.Fraction Addition and Subtraction/README.md b/solution/0500-0599/0592.Fraction Addition and Subtraction/README.md index 9f6eccb8bf0d6..f3014f3f82008 100644 --- a/solution/0500-0599/0592.Fraction Addition and Subtraction/README.md +++ b/solution/0500-0599/0592.Fraction Addition and Subtraction/README.md @@ -174,6 +174,54 @@ func gcd(a, b int) int { } ``` +#### JavaScript + +```js +/** + * @param {string} expression + * @return {string} + */ +var fractionAddition = function (expression) { + let x = 0, + y = 1; + + if (!expression.startsWith('-') && !expression.startsWith('+')) { + expression = '+' + expression; + } + + let i = 0; + const n = expression.length; + + while (i < n) { + const sign = expression[i] === '-' ? -1 : 1; + i++; + + let j = i; + while (j < n && expression[j] !== '+' && expression[j] !== '-') { + j++; + } + + const [a, b] = expression.slice(i, j).split('/').map(Number); + x = x * b + sign * a * y; + y *= b; + i = j; + } + + const gcd = (a, b) => { + while (b !== 0) { + [a, b] = [b, a % b]; + } + return Math.abs(a); + }; + + const z = gcd(x, y); + x = Math.floor(x / z); + y = Math.floor(y / z); + + return `${x}/${y}`; +}; +``` +