Skip to content

Commit ab09165

Browse files
authored
feat: add solutions to lc problem: No.2705 (#1304)
No.2705.Compact Object
1 parent e426ede commit ab09165

File tree

6 files changed

+135
-4
lines changed

6 files changed

+135
-4
lines changed

solution/0200-0299/0226.Invert Binary Tree/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public:
233233
// pub left: Option<Rc<RefCell<TreeNode>>>,
234234
// pub right: Option<Rc<RefCell<TreeNode>>>,
235235
// }
236-
//
236+
//
237237
// impl TreeNode {
238238
// #[inline]
239239
// pub fn new(val: i32) -> Self {

solution/0200-0299/0226.Invert Binary Tree/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public:
215215
// pub left: Option<Rc<RefCell<TreeNode>>>,
216216
// pub right: Option<Rc<RefCell<TreeNode>>>,
217217
// }
218-
//
218+
//
219219
// impl TreeNode {
220220
// #[inline]
221221
// pub fn new(val: i32) -> Self {

solution/2700-2799/2705.Compact Object/README.md

+54-1
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,67 @@
4848

4949
<!-- 这里可写通用的实现逻辑 -->
5050

51+
**方法一:递归**
52+
53+
如果 `obj` 不是对象或为空,函数会原封不动地返回,因为无需检查非对象值中的键。
54+
55+
如果 `obj` 是一个数组,它会使用 `obj.filter(Boolean)` 过滤掉虚假值(如 `null``undefined``false`、0、""),然后使用 `map(compactObject)` 对每个元素递归调用 `compactObject`。这样可以确保嵌套数组也被压缩。
56+
57+
如果 `obj` 是一个对象,则会创建一个新的空对象 `compactedObj`。它会遍历 `obj` 的所有键,并对每个键在相应的值上递归调用 `compactObject`,然后将结果存储在值变量中。如果值是真实的(即不是虚假的),它就会将其赋值给具有相应键的压缩对象。
58+
59+
时间复杂度 $O(n)$, 空间复杂度 $O(n)$。
60+
5161
<!-- tabs:start -->
5262

63+
### **JavaScript**
64+
65+
<!-- 这里可写当前语言的特殊实现逻辑 -->
66+
67+
```js
68+
var compactObject = function (obj) {
69+
if (obj === null || typeof obj !== 'object') {
70+
return obj;
71+
}
72+
73+
if (Array.isArray(obj)) {
74+
return obj.filter(Boolean).map(compactObject);
75+
}
76+
77+
const result = {};
78+
for (const key in obj) {
79+
const value = compactObject(obj[key]);
80+
if (Boolean(value)) {
81+
result[key] = value;
82+
}
83+
}
84+
return result;
85+
};
86+
```
87+
5388
### **TypeScript**
5489

5590
<!-- 这里可写当前语言的特殊实现逻辑 -->
5691

5792
```ts
58-
93+
type Obj = Record<any, any>;
94+
95+
function compactObject(obj: Obj): Obj {
96+
if (Array.isArray(obj)) {
97+
const temp = [];
98+
for (const item of obj) {
99+
if (item) {
100+
if (typeof item === 'object') temp.push(compactObject(item));
101+
else temp.push(item);
102+
}
103+
}
104+
return temp;
105+
}
106+
for (const [key, value] of Object.entries(obj)) {
107+
if (!value) delete obj[key];
108+
else if (typeof value === 'object') obj[key] = compactObject(value);
109+
}
110+
return obj;
111+
}
59112
```
60113

61114
<!-- tabs:end -->

solution/2700-2799/2705.Compact Object/README_EN.md

+42-1
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,51 @@
4444

4545
<!-- tabs:start -->
4646

47+
### **JavaScript**
48+
49+
```js
50+
var compactObject = function (obj) {
51+
if (obj === null || typeof obj !== 'object') {
52+
return obj;
53+
}
54+
55+
if (Array.isArray(obj)) {
56+
return obj.filter(Boolean).map(compactObject);
57+
}
58+
59+
const result = {};
60+
for (const key in obj) {
61+
const value = compactObject(obj[key]);
62+
if (Boolean(value)) {
63+
result[key] = value;
64+
}
65+
}
66+
return result;
67+
};
68+
```
69+
4770
### **TypeScript**
4871

4972
```ts
50-
73+
type Obj = Record<any, any>;
74+
75+
function compactObject(obj: Obj): Obj {
76+
if (Array.isArray(obj)) {
77+
const temp = [];
78+
for (const item of obj) {
79+
if (item) {
80+
if (typeof item === 'object') temp.push(compactObject(item));
81+
else temp.push(item);
82+
}
83+
}
84+
return temp;
85+
}
86+
for (const [key, value] of Object.entries(obj)) {
87+
if (!value) delete obj[key];
88+
else if (typeof value === 'object') obj[key] = compactObject(value);
89+
}
90+
return obj;
91+
}
5192
```
5293

5394
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var compactObject = function (obj) {
2+
if (obj === null || typeof obj !== 'object') {
3+
return obj;
4+
}
5+
6+
if (Array.isArray(obj)) {
7+
return obj.filter(Boolean).map(compactObject);
8+
}
9+
10+
const result = {};
11+
for (const key in obj) {
12+
const value = compactObject(obj[key]);
13+
if (Boolean(value)) {
14+
result[key] = value;
15+
}
16+
}
17+
return result;
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
type Obj = Record<any, any>;
2+
3+
function compactObject(obj: Obj): Obj {
4+
if (Array.isArray(obj)) {
5+
const temp = [];
6+
for (const item of obj) {
7+
if (item) {
8+
if (typeof item === 'object') temp.push(compactObject(item));
9+
else temp.push(item);
10+
}
11+
}
12+
return temp;
13+
}
14+
for (const [key, value] of Object.entries(obj)) {
15+
if (!value) delete obj[key];
16+
else if (typeof value === 'object') obj[key] = compactObject(value);
17+
}
18+
return obj;
19+
}

0 commit comments

Comments
 (0)