Skip to content

Commit 1d02857

Browse files
authored
Update 1146-snapshot-array.js
1 parent d4f50c4 commit 1d02857

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

1146-snapshot-array.js

+56
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,59 @@
1+
/**
2+
* @param {number} length
3+
*/
4+
const SnapshotArray = function(length) {
5+
this.snaps = Array(length)
6+
this.snapId = 0
7+
};
8+
9+
/**
10+
* @param {number} index
11+
* @param {number} val
12+
* @return {void}
13+
*/
14+
SnapshotArray.prototype.set = function(index, val) {
15+
if(this.snaps[index] == null) {
16+
this.snaps[index] = {}
17+
}
18+
this.snaps[index][this.snapId] = val
19+
};
20+
21+
/**
22+
* @return {number}
23+
*/
24+
SnapshotArray.prototype.snap = function() {
25+
return this.snapId++
26+
};
27+
28+
/**
29+
* @param {number} index
30+
* @param {number} snap_id
31+
* @return {number}
32+
*/
33+
SnapshotArray.prototype.get = function(index, snap_id) {
34+
let res = 0
35+
let id = snap_id
36+
while(id >= 0) {
37+
if(this.snaps[index] == null || this.snaps[index][id] == null) id--
38+
else {
39+
res = this.snaps[index][id]
40+
break
41+
}
42+
}
43+
44+
return res
45+
};
46+
47+
/**
48+
* Your SnapshotArray object will be instantiated and called as such:
49+
* var obj = new SnapshotArray(length)
50+
* obj.set(index,val)
51+
* var param_2 = obj.snap()
52+
* var param_3 = obj.get(index,snap_id)
53+
*/
54+
55+
// another
56+
157
/**
258
* @param {number[]} nums
359
* @param {number} target

0 commit comments

Comments
 (0)