forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDKhan.cpp
More file actions
43 lines (34 loc) · 1.05 KB
/
PDKhan.cpp
File metadata and controls
43 lines (34 loc) · 1.05 KB
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
class Solution {
public:
/*
* @param strs: a list of strings
* @return: encodes a list of strings to a single string.
*/
string encode(vector<string> &strs) {
// write your code here
string code;
for(const string& s : strs){
code += to_string(s.size()) + ":" + s;
}
return code;
}
/*
* @param str: A string
* @return: decodes a single string to a list of strings
*/
vector<string> decode(string &str) {
// write your code here
vector<string> result;
int i;
while(i < str.size()){
int j = i;
while(str[j] != ':')
j++;
int len = stoi(str.substr(i, j - i);
string word = str.substr(j + 1, len);
result.push_back(word);
i = j + 1 + len;
}
return result;
}
};