Skip to content

Commit 20a47a9

Browse files
committed
add 659
1 parent a0df80d commit 20a47a9

File tree

4 files changed

+149
-4
lines changed

4 files changed

+149
-4
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
5151
| [] | [leetcode-top-100-liked.md](./list/leetcode-top-100-liked.md) | 100/100 | - |
5252
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
5353
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 14/129 | 3 vips |
54-
| [🔲] | [9c-top.md](./list/9c-top.md) | 13/52 | 1 vip |
54+
| [🔲] | [9c-top.md](./list/9c-top.md) | 14/52 | 1 vip |
5555

56-
**Solved**: 481 problems
56+
**Solved**: 482 problems
5757

5858
## 类型/Category
5959

@@ -910,10 +910,11 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
910910

911911
## String
912912

913-
| Link | Problem(3) | Solution | Tag | Time | Space | Ref |
913+
| Link | Problem(4) | Solution | Tag | Time | Space | Ref |
914914
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
915915
| [Leetcode-3340](https://leetcode.com/problems/check-balanced-string/) | Check Balanced String | [c++](./leetcode/3340.check-balanced-string.cpp), [python3](./leetcode/3340.check-balanced-string.py) | String | O\(N\) | O\(1\) | - |
916916
| [Leetcode-796](https://leetcode.com/problems/rotate-string/) | Rotate String | [c++](./leetcode/796.rotate-string.cpp), [python3](./leetcode/796.rotate-string.py) | String | O\(N\) | O\(N\) | - |
917+
| [Lintcode-659](https://www.lintcode.com/problem/encode-and-decode-strings/) | Encode And Decode Strings | [c++](./lintcode/659.encode-and-decode-strings.cpp), [python3](./lintcode/659.encode-and-decode-strings.py) | String | O\(N\) | O\(1\) | Leetcode-271 |
917918
| [Lintcode-1790](https://www.lintcode.com/problem/rotate-string-ii/) | Rotate String II | [c++](./lintcode/1790.rotate-string-ii.cpp), [python3](./lintcode/1790.rotate-string-ii.py) | String | O\(N\) | O\(N\) | - |
918919

919920
## Other
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Tag: String
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: Leetcode-271
5+
// Note: -
6+
7+
// Design an algorithm to encode a list of strings to a string.
8+
// The encoded string is then sent over the network and is decoded back to the original list of strings.
9+
//
10+
// Please implement `encode` and `decode`
11+
//
12+
// **Example1**
13+
// ```
14+
// Input: ["lint","code","love","you"]
15+
// Output: ["lint","code","love","you"]
16+
// Explanation:
17+
// One possible encode method is: "lint:;code:;love:;you"
18+
// ```
19+
// **Example2**
20+
// ```
21+
// Input: ["we", "say", ":", "yes"]
22+
// Output: ["we", "say", ":", "yes"]
23+
// Explanation:
24+
// One possible encode method is: "we:;say:;:::;yes"
25+
// ```
26+
//
27+
// Because the string may contain any of the **256** legal ASCII characters, your algorithm must be able to handle any character that may appear
28+
//
29+
// Do not rely on any libraries, the purpose of this problem is to implement the "encode" and "decode" algorithms on your own
30+
31+
class Solution {
32+
public:
33+
/*
34+
* @param strs: a list of strings
35+
* @return: encodes a list of strings to a single string.
36+
*/
37+
string encode(vector<string> &strs) {
38+
// write your code here
39+
string res;
40+
for (string &str: strs) {
41+
for (char c: str) {
42+
if (c == ':') {
43+
res += "::";
44+
} else {
45+
res += c;
46+
}
47+
}
48+
res += ":;";
49+
}
50+
return res;
51+
}
52+
53+
/*
54+
* @param str: A string
55+
* @return: decodes a single string to a list of strings
56+
*/
57+
vector<string> decode(string &str) {
58+
// write your code here
59+
vector<string> res;
60+
string word;
61+
int i = 0;
62+
while (i < str.size()) {
63+
if (str[i] == ':') {
64+
if (str[i + 1] == ':') {
65+
word += ':';
66+
} else {
67+
res.push_back(word);
68+
word = "";
69+
}
70+
i += 2;
71+
} else {
72+
word += str[i];
73+
i += 1;
74+
}
75+
76+
77+
}
78+
return res;
79+
}
80+
};
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Tag: String
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: Leetcode-271
5+
# Note: -
6+
7+
# Design an algorithm to encode a list of strings to a string.
8+
# The encoded string is then sent over the network and is decoded back to the original list of strings.
9+
#
10+
# Please implement `encode` and `decode`
11+
#
12+
# **Example1**
13+
# ```
14+
# Input: ["lint","code","love","you"]
15+
# Output: ["lint","code","love","you"]
16+
# Explanation:
17+
# One possible encode method is: "lint:;code:;love:;you"
18+
# ```
19+
# **Example2**
20+
# ```
21+
# Input: ["we", "say", ":", "yes"]
22+
# Output: ["we", "say", ":", "yes"]
23+
# Explanation:
24+
# One possible encode method is: "we:;say:;:::;yes"
25+
# ```
26+
#
27+
# Because the string may contain any of the **256** legal ASCII characters, your algorithm must be able to handle any character that may appear
28+
#
29+
# Do not rely on any libraries, the purpose of this problem is to implement the "encode" and "decode" algorithms on your own
30+
31+
class Solution:
32+
"""
33+
@param: strs: a list of strings
34+
@return: encodes a list of strings to a single string.
35+
"""
36+
def encode(self, strs):
37+
# write your code here
38+
res = ''
39+
for s in strs:
40+
res += s.replace(':', '::') + ':;'
41+
return res
42+
43+
"""
44+
@param: str: A string
45+
@return: decodes a single string to a list of strings
46+
"""
47+
def decode(self, str):
48+
# write your code here
49+
res = []
50+
word = ''
51+
i = 0
52+
while i < len(str):
53+
if str[i] == ':':
54+
if str[i + 1] == ':':
55+
word += ':'
56+
else:
57+
res.append(word)
58+
word = ''
59+
i += 2
60+
else:
61+
word += str[i]
62+
i += 1
63+
64+
return res

list/9c-top.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
12. https://www.lintcode.com/problem/one-edit-distance/
2020
13. https://www.lintcode.com/problem/read-n-characters-given-read4/
2121
14. https://www.lintcode.com/problem/read-n-characters-given-read4-ii-call-multiple-times/
22+
15. https://www.lintcode.com/problem/encode-and-decode-strings/
2223

23-
- https://www.lintcode.com/problem/strings-serialization/
2424
- https://www.lintcode.com/problem/binary-tree-serialization/
2525
- https://www.lintcode.com/problem/system-longest-file-path/
2626
- https://www.lintcode.com/problem/roman-to-integer/

0 commit comments

Comments
 (0)