From dc87d0d639a9a141de91201fa76939f18d6b6f48 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 20 May 2024 09:29:28 +0800 Subject: [PATCH] feat: update solutions to lc problem: No.2705 No.2705.Compact Object --- .../2700-2799/2705.Compact Object/README.md | 47 ++++++++------- .../2705.Compact Object/README_EN.md | 57 +++++++++++-------- .../2700-2799/2705.Compact Object/Solution.js | 23 ++++---- .../2700-2799/2705.Compact Object/Solution.ts | 24 ++++---- 4 files changed, 78 insertions(+), 73 deletions(-) diff --git a/solution/2700-2799/2705.Compact Object/README.md b/solution/2700-2799/2705.Compact Object/README.md index 85eedb03cd868..a28d199df66a7 100644 --- a/solution/2700-2799/2705.Compact Object/README.md +++ b/solution/2700-2799/2705.Compact Object/README.md @@ -78,44 +78,43 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/2700-2799/2705.Co type Obj = Record; function compactObject(obj: Obj): Obj { - if (Array.isArray(obj)) { - const temp = []; - for (const item of obj) { - if (item) { - if (typeof item === 'object') temp.push(compactObject(item)); - else temp.push(item); - } - } - return temp; + if (!obj || typeof obj !== 'object') { + return obj; } - for (const [key, value] of Object.entries(obj)) { - if (!value) delete obj[key]; - else if (typeof value === 'object') obj[key] = compactObject(value); + if (Array.isArray(obj)) { + return obj.map(compactObject).filter(Boolean); } - return obj; + return Object.entries(obj).reduce((acc, [key, value]) => { + const compactedValue = compactObject(value); + if (compactedValue) { + acc[key] = compactedValue; + } + return acc; + }, {} as Obj); } ``` #### JavaScript ```js +/** + * @param {Object|Array} obj + * @return {Object|Array} + */ var compactObject = function (obj) { - if (obj === null || typeof obj !== 'object') { + if (!obj || typeof obj !== 'object') { return obj; } - if (Array.isArray(obj)) { - return obj.filter(Boolean).map(compactObject); + return obj.map(compactObject).filter(Boolean); } - - const result = {}; - for (const key in obj) { - const value = compactObject(obj[key]); - if (Boolean(value)) { - result[key] = value; + return Object.entries(obj).reduce((acc, [key, value]) => { + const compactedValue = compactObject(value); + if (compactedValue) { + acc[key] = compactedValue; } - } - return result; + return acc; + }, {}); }; ``` diff --git a/solution/2700-2799/2705.Compact Object/README_EN.md b/solution/2700-2799/2705.Compact Object/README_EN.md index 8f0388abce004..440aaf1d0006a 100644 --- a/solution/2700-2799/2705.Compact Object/README_EN.md +++ b/solution/2700-2799/2705.Compact Object/README_EN.md @@ -58,7 +58,15 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/2700-2799/2705.Co -### Solution 1 +### Solution 1: Recursion + +If `obj` is not an object or is null, the function will return it as is, because there's no need to check for keys in non-object values. + +If `obj` is an array, it will use `obj.filter(Boolean)` to filter out falsy values (like `null`, `undefined`, `false`, 0, ""), then use `map(compactObject)` to recursively call `compactObject` on each element. This ensures that nested arrays are also compacted. + +If `obj` is an object, it will create a new empty object `compactedObj`. It will iterate over all keys of `obj`, and for each key, it will recursively call `compactObject` on the corresponding value, then store the result in the value variable. If the value is truthy (i.e., not falsy), it will assign it to the compacted object with the corresponding key. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. @@ -68,44 +76,43 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/2700-2799/2705.Co type Obj = Record; function compactObject(obj: Obj): Obj { - if (Array.isArray(obj)) { - const temp = []; - for (const item of obj) { - if (item) { - if (typeof item === 'object') temp.push(compactObject(item)); - else temp.push(item); - } - } - return temp; + if (!obj || typeof obj !== 'object') { + return obj; } - for (const [key, value] of Object.entries(obj)) { - if (!value) delete obj[key]; - else if (typeof value === 'object') obj[key] = compactObject(value); + if (Array.isArray(obj)) { + return obj.map(compactObject).filter(Boolean); } - return obj; + return Object.entries(obj).reduce((acc, [key, value]) => { + const compactedValue = compactObject(value); + if (compactedValue) { + acc[key] = compactedValue; + } + return acc; + }, {} as Obj); } ``` #### JavaScript ```js +/** + * @param {Object|Array} obj + * @return {Object|Array} + */ var compactObject = function (obj) { - if (obj === null || typeof obj !== 'object') { + if (!obj || typeof obj !== 'object') { return obj; } - if (Array.isArray(obj)) { - return obj.filter(Boolean).map(compactObject); + return obj.map(compactObject).filter(Boolean); } - - const result = {}; - for (const key in obj) { - const value = compactObject(obj[key]); - if (Boolean(value)) { - result[key] = value; + return Object.entries(obj).reduce((acc, [key, value]) => { + const compactedValue = compactObject(value); + if (compactedValue) { + acc[key] = compactedValue; } - } - return result; + return acc; + }, {}); }; ``` diff --git a/solution/2700-2799/2705.Compact Object/Solution.js b/solution/2700-2799/2705.Compact Object/Solution.js index 0edd2bc69fa8a..dfec608b4d8d8 100644 --- a/solution/2700-2799/2705.Compact Object/Solution.js +++ b/solution/2700-2799/2705.Compact Object/Solution.js @@ -1,18 +1,19 @@ +/** + * @param {Object|Array} obj + * @return {Object|Array} + */ var compactObject = function (obj) { - if (obj === null || typeof obj !== 'object') { + if (!obj || typeof obj !== 'object') { return obj; } - if (Array.isArray(obj)) { - return obj.filter(Boolean).map(compactObject); + return obj.map(compactObject).filter(Boolean); } - - const result = {}; - for (const key in obj) { - const value = compactObject(obj[key]); - if (Boolean(value)) { - result[key] = value; + return Object.entries(obj).reduce((acc, [key, value]) => { + const compactedValue = compactObject(value); + if (compactedValue) { + acc[key] = compactedValue; } - } - return result; + return acc; + }, {}); }; diff --git a/solution/2700-2799/2705.Compact Object/Solution.ts b/solution/2700-2799/2705.Compact Object/Solution.ts index 654391a62055d..847db1c486bc7 100644 --- a/solution/2700-2799/2705.Compact Object/Solution.ts +++ b/solution/2700-2799/2705.Compact Object/Solution.ts @@ -1,19 +1,17 @@ type Obj = Record; function compactObject(obj: Obj): Obj { - if (Array.isArray(obj)) { - const temp = []; - for (const item of obj) { - if (item) { - if (typeof item === 'object') temp.push(compactObject(item)); - else temp.push(item); - } - } - return temp; + if (!obj || typeof obj !== 'object') { + return obj; } - for (const [key, value] of Object.entries(obj)) { - if (!value) delete obj[key]; - else if (typeof value === 'object') obj[key] = compactObject(value); + if (Array.isArray(obj)) { + return obj.map(compactObject).filter(Boolean); } - return obj; + return Object.entries(obj).reduce((acc, [key, value]) => { + const compactedValue = compactObject(value); + if (compactedValue) { + acc[key] = compactedValue; + } + return acc; + }, {} as Obj); }