File tree 2 files changed +48
-0
lines changed
2 files changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -509,6 +509,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
509
509
| [ 2542. Maximum Subsequence Score] [ lc2542 ] | 🟠 Medium | [ ![ rust] ( res/rs.png )] [ lc2542rs ] |
510
510
| [ 2543. Check if Point Is Reachable] [ lc2543 ] | 🔴 Hard | [ ![ python] ( res/py.png )] [ lc2543py ] [ ![ rust] ( res/rs.png )] [ lc2543rs ] |
511
511
| [ 2585. Number of Ways to Earn Points] [ lc2585 ] | 🔴 Hard | [ ![ python] ( res/py.png )] [ lc2585py ] [ ![ rust] ( res/rs.png )] [ lc2585rs ] |
512
+ | [ 2618. Check if Object Instance of Class] [ lc2618 ] | 🟠 Medium | [ ![ js] ( res/js.png )] [ lc2618js ] |
512
513
| [ 2619. Array Prototype Last] [ lc2619 ] | 🟢 Easy | [ ![ js] ( res/js.png )] [ lc2619js ] |
513
514
| [ 2620. Counter] [ lc2620 ] | 🟢 Easy | [ ![ js] ( res/js.png )] [ lc2620js ] |
514
515
| [ 2621. Sleep] [ lc2621 ] | 🟢 Easy | [ ![ js] ( res/js.png )] [ lc2621js ] |
@@ -1638,6 +1639,8 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
1638
1639
[ lc2585 ] : https://leetcode.com/problems/number-of-ways-to-earn-points/
1639
1640
[ lc2585py ] : leetcode/number-of-ways-to-earn-points.py
1640
1641
[ lc2585rs ] : leetcode/number-of-ways-to-earn-points.rs
1642
+ [ lc2618 ] : https://leetcode.com/problems/check-if-object-instance-of-class/
1643
+ [ lc2618js ] : leetcode/check-if-object-instance-of-class.js
1641
1644
[ lc2619 ] : https://leetcode.com/problems/array-prototype-last/
1642
1645
[ lc2619js ] : leetcode/array-prototype-last.js
1643
1646
[ lc2620 ] : https://leetcode.com/problems/counter/
Original file line number Diff line number Diff line change
1
+ // 2618. Check if Object Instance of Class
2
+ // 🟠 Medium
3
+ //
4
+ // https://leetcode.com/problems/check-if-object-instance-of-class/
5
+ //
6
+ // Tags: Javascript
7
+
8
+ // Get the provided object prototype and check if that matches the given class,
9
+ // keep going up the prototype chain until we find one that it matches or
10
+ // the chain ends.
11
+ //
12
+ // Time complexity: O(n) - It can go up the entire prototype chain or until it
13
+ // finds a class that matches.
14
+ // Space complexity: O(1) - The iterative version uses constant extra memory,
15
+ // we could use a recursive version with similar logic, but it would require
16
+ // extra memory.
17
+ //
18
+ // Runtime 115 ms Beats 27.90%
19
+ // Memory 51.1 MB Beats 95.40%
20
+ /**
21
+ * @param {any } obj
22
+ * @param {any } classFunction
23
+ * @return {boolean }
24
+ */
25
+ var checkIfInstanceOf = function ( obj , classFunction ) {
26
+ if (
27
+ obj === null ||
28
+ obj === undefined ||
29
+ typeof classFunction !== "function"
30
+ ) {
31
+ return false ;
32
+ }
33
+ let prot = Object . getPrototypeOf ( obj ) ;
34
+ while ( prot !== null ) {
35
+ if ( prot === classFunction . prototype ) {
36
+ return true ;
37
+ }
38
+ prot = Object . getPrototypeOf ( prot ) ;
39
+ }
40
+ return false ;
41
+ } ;
42
+
43
+ /**
44
+ * checkIfInstanceOf(new Date(), Date); // true
45
+ */
You can’t perform that action at this time.
0 commit comments