-
Notifications
You must be signed in to change notification settings - Fork 0
/
#134.cpp
52 lines (48 loc) · 986 Bytes
/
#134.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
50
51
52
#include<iostream>
#include<unordered_map>
#include<string>
#include<sstream>
using namespace std;
class SparseArray {
unordered_map<int, int> nonzero;
public:
SparseArray(int arr[], int size) {
for (int i = 0; i < size; i++)
if (arr[i] != 0) nonzero[i] = arr[i];
}
void set(int i, int val) {
if (val == 0 && nonzero.find(i) != nonzero.end())
nonzero.erase(i);
else if (val != 0)
nonzero[i] = val;
}
int get(int i) {
if (nonzero.find(i) != nonzero.end())
return nonzero[i];
else
return 0;
}
};
int main() {
int arr[] = {1,0,0,0,5,3,2,0,0,4};
SparseArray a(arr, 10);
for (int i = 0; i < 10; i++)
cout << a.get(i) << ",";
cout << endl;
string inp;
while (getline(cin, inp)) {
istringstream ss(inp);
ss >> inp;
if (inp == "get") {
ss >> inp;
int i = stoi(inp);
cout << "value: " << a.get(i) << endl;
} else if (inp == "set") {
ss >> inp;
int i = stoi(inp);
ss >> inp;
int val = stoi(inp);
a.set(i, val);
}
}
}