-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path0271-EncodeAndDecodeStrings.cs
64 lines (55 loc) · 1.72 KB
/
0271-EncodeAndDecodeStrings.cs
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
53
54
55
56
57
58
59
60
61
62
63
64
//-----------------------------------------------------------------------------
// Runtime: 284ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Text;
namespace LeetCode
{
public class _0271_EncodeAndDecodeStrings
{
// Encodes a list of strings to a single string.
public string encode(IList<string> strs)
{
var sb = new StringBuilder();
foreach (var str in strs)
{
sb.Append(IntToString(str.Length));
sb.Append(str);
}
return sb.ToString();
}
// Decodes a single string to a list of strings.
public IList<string> decode(string s)
{
var index = 0;
var result = new List<string>();
while (index < s.Length)
{
var length = StringToint(s.Substring(index, 4));
index += 4;
result.Add(s.Substring(index, length));
index += length;
}
return result;
}
private string IntToString(int num)
{
var bytes = new char[4];
for (int i = 3; i >= 0; i--)
bytes[3 - i] = (char)(num >> (i * 8) & 0xff);
return new string(bytes);
}
private int StringToint(string num)
{
var result = 0;
foreach (var ch in num)
result = (result << 8) + (int)ch;
return result;
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(strs));
}