[Swift] LeetCode1146 snapshot array |. Snapshot Array

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤ 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 given index to be equal to val.
  • int snap() takes a snapshot of the array and returns the snap_id: the total number of times we called snap() minus 1.
  • int get(index, snap_id) returns the value at the given index, at the time we took the snapshot with the given snap_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 to setsnap, and get.
  • 0 <= index < length
  • 0 <= snap_id < (the total number of times we call snap())
  • 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 index  index element at the set  val.
  • int snap() - Get a snapshot of the array, and returns the number of snapshots  snap_id(snapshot number to call is  snap() the total number minus  1).
  • int get(index, snap_id) - according to the specified  snap_id select the snapshot, and the snapshot returns the specified index  index 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 times set, snapand  getthe calls.
  • 0 <= index < length
  • 0 <= snap_id < We call the  snap() total number of
  • 0 <= val <= 10^9

Guess you like

Origin www.cnblogs.com/strengthen/p/11297779.html