File tree 6 files changed +135
-4
lines changed
0200-0299/0226.Invert Binary Tree
2700-2799/2705.Compact Object
6 files changed +135
-4
lines changed Original file line number Diff line number Diff line change @@ -233,7 +233,7 @@ public:
233
233
// pub left: Option<Rc<RefCell<TreeNode>>>,
234
234
// pub right: Option<Rc<RefCell<TreeNode>>>,
235
235
// }
236
- //
236
+ //
237
237
// impl TreeNode {
238
238
// #[inline]
239
239
// pub fn new(val: i32) -> Self {
Original file line number Diff line number Diff line change @@ -215,7 +215,7 @@ public:
215
215
// pub left: Option<Rc<RefCell<TreeNode>>>,
216
216
// pub right: Option<Rc<RefCell<TreeNode>>>,
217
217
// }
218
- //
218
+ //
219
219
// impl TreeNode {
220
220
// #[inline]
221
221
// pub fn new(val: i32) -> Self {
Original file line number Diff line number Diff line change 48
48
49
49
<!-- 这里可写通用的实现逻辑 -->
50
50
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
+
51
61
<!-- tabs:start -->
52
62
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
+
53
88
### ** TypeScript**
54
89
55
90
<!-- 这里可写当前语言的特殊实现逻辑 -->
56
91
57
92
``` 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
+ }
59
112
```
60
113
61
114
<!-- tabs:end -->
Original file line number Diff line number Diff line change 44
44
45
45
<!-- tabs:start -->
46
46
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
+
47
70
### ** TypeScript**
48
71
49
72
``` 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
+ }
51
92
```
52
93
53
94
<!-- tabs:end -->
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments