★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤ micro-channel public number: Shan Wing Chi ( shanqingyongzhi)
➤ blog Park address: San-ching Wing Chi ( https://www.cnblogs.com/strengthen/ )
➤GitHub address: https://github.com/strengthen/LeetCode
➤ original address: HTTPS: // the WWW. cnblogs.com/strengthen/p/11297779.html
➤ If the address is not a link blog Park Yong Shan Chi, it may be crawling author of the article.
➤ text has been modified update! Click strongly recommended that the original address read! Support authors! Support the original!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Implement a SnapshotArray that supports the following interface:
SnapshotArray(int length)
initializes an array-like data structure with the given length. Initially, each element equals 0.void set(index, val)
sets the element at the givenindex
to be equal toval
.int snap()
takes a snapshot of the array and returns thesnap_id
: the total number of times we calledsnap()
minus1
.int get(index, snap_id)
returns the value at the givenindex
, at the time we took the snapshot with the givensnap_id
Example 1:
Input: ["SnapshotArray","set","snap","set","get"] [[3],[0,5],[],[0,6],[0,0]] Output: [null,null,0,null,5] Explanation: SnapshotArray snapshotArr = new SnapshotArray(3); // set the length to be 3 snapshotArr.set(0,5); // Set array[0] = 5 snapshotArr.snap(); // Take a snapshot, return snap_id = 0 snapshotArr.set(0,6); snapshotArr.get(0,0); // Get the value of array[0] with snap_id = 0, return 5
Constraints:
1 <= length <= 50000
- At most
50000
calls will be made toset
,snap
, andget
. 0 <= index < length
0 <= snap_id <
(the total number of times we callsnap()
)0 <= val <= 10^9
The implementation supports the following interfaces' snapshot array "- SnapshotArray:
SnapshotArray(int length)
- initializing a data structure equivalent to the length of the specified array of classes. Initially, each element is equal to zero.void set(index, val)
- will be specified indexindex
element at the setval
.int snap()
- Get a snapshot of the array, and returns the number of snapshotssnap_id
(snapshot number to call issnap()
the total number minus1
).int get(index, snap_id)
- according to the specifiedsnap_id
select the snapshot, and the snapshot returns the specified indexindex
value.
Example:
Input: [ "SnapshotArray", "SET", "SNAP", "SET", "GET"] [[. 3], [0,5], [], [0,6], [0,0]] Output : [null, null, 0, null, 5] explains: SnapshotArray snapshotArr = new new SnapshotArray (3); // initialize a snapshot array of length 3 snapshotArr.set (0,5); // make the array [0] = . 5 snapshotArr.snap (); // take a snapshot, returns snap_id = 0 snapshotArr.set (0,6); snapshotArr.get (0,0); // get the value of the snapshot snap_id = 0 in the array [0], and returns 5
prompt:
1 <= length <= 50000
- Topics were most
50000
timesset
,snap
andget
the calls. 0 <= index < length
0 <= snap_id <
We call thesnap()
total number of0 <= val <= 10^9