-
Notifications
You must be signed in to change notification settings - Fork 0
/
1146.快照数组.cpp
50 lines (43 loc) · 951 Bytes
/
1146.快照数组.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* @lc app=leetcode.cn id=1146 lang=cpp
*
* [1146] 快照数组
*/
#include <vector>
#include <map>
using namespace std;
// @lc code=start
class SnapshotArray {
public:
SnapshotArray(int length) {
snapCnt = 0;
map<int,int> t;
t[0] = 0;
for(int i = 0;i < length; ++i){
arr.push_back(t);
}
}
void set(int index, int val) {
arr[index][snapCnt] = val;
}
int snap() {
snapCnt++;
return snapCnt-1;
}
int get(int index, int snap_id) {
auto it = arr[index].upper_bound(snap_id);
it--;
return arr[index][it->first];
}
private:
vector<map<int,int>> arr;
int snapCnt;
};
/**
* Your SnapshotArray object will be instantiated and called as such:
* SnapshotArray* obj = new SnapshotArray(length);
* obj->set(index,val);
* int param_2 = obj->snap();
* int param_3 = obj->get(index,snap_id);
*/
// @lc code=end