Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update solutions to lc problem: No.2705 #2851

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions solution/2700-2799/2705.Compact Object/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,44 +78,43 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/2700-2799/2705.Co
type Obj = Record<any, any>;

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;
}, {});
};
```

Expand Down
57 changes: 32 additions & 25 deletions solution/2700-2799/2705.Compact Object/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/2700-2799/2705.Co

<!-- solution:start -->

### 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)$.

<!-- tabs:start -->

Expand All @@ -68,44 +76,43 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/2700-2799/2705.Co
type Obj = Record<any, any>;

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;
}, {});
};
```

Expand Down
23 changes: 12 additions & 11 deletions solution/2700-2799/2705.Compact Object/Solution.js
Original file line number Diff line number Diff line change
@@ -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;
}, {});
};
24 changes: 11 additions & 13 deletions solution/2700-2799/2705.Compact Object/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
type Obj = Record<any, any>;

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);
}
Loading