-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinyURL.cpp
More file actions
115 lines (104 loc) · 3.96 KB
/
tinyURL.cpp
File metadata and controls
115 lines (104 loc) · 3.96 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Self-increase ID
class Solution {
public:
// Encodes a URL to a shortened URL.
string encode(string longUrl) {
mp[id++] = longUrl;
return base + to_string(id - 1);
}
// Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
return mp[stoi(shortUrl.substr(19))];
}
private:
int id = 0;
unordered_map<int, string> mp;
string base = "http://tinyurl.com/";
};
// Your Solution object will be instantiated and called as such:
// Solution solution;
// solution.decode(solution.encode(url));
class Solution {
const string constLeading = "http://tinyurl.com/";
const string code = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
unordered_map<size_t, string> tiny2origin;
const int constLeadingLength = 19;
hash<string> stringHash;
public:
// Encodes a URL to a shortened URL.
string encode(string longUrl) {
size_t lengthOfLongUrl = longUrl.size();
size_t longUrlHashValue = stringHash(longUrl);
tiny2origin[longUrlHashValue] = longUrl;
string longUrlHashString;
//longUrlHashValue to a base 62 string
while (longUrlHashValue) {
size_t currentCharOffset = longUrlHashValue % 62;
// longUrlHashString += to_string(code[currentCharOffset]);
longUrlHashString.append(1, code[currentCharOffset]);
longUrlHashValue /= 62;
}
// add 2 char to the end。
return constLeading + longUrlHashString.append(longUrl.substr(lengthOfLongUrl - 2,2));
}
// Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
size_t lengthOfShortUrl = shortUrl.size();
size_t hashCodeOfShortUrl = 0;
size_t availabelHashEnd = lengthOfShortUrl - 3;
size_t currentCharIndex = constLeadingLength;
size_t currentWeight = 1;
while (currentCharIndex <= availabelHashEnd) {
char currentChar = shortUrl[currentCharIndex];
if (currentChar <= '9' && currentChar >= '0') {
hashCodeOfShortUrl += currentWeight * (currentChar - '0');
}
else {
if (currentChar <= 'z' && currentChar >= 'a')
hashCodeOfShortUrl += currentWeight * (currentChar - 'a' + 10);
else if(currentChar <= 'Z' && currentChar >= 'A')
hashCodeOfShortUrl += currentWeight * (currentChar - 'A' + 36);
}
currentWeight *= 62;
currentCharIndex++;
}
if (tiny2origin.count(hashCodeOfShortUrl)) {
return tiny2origin[hashCodeOfShortUrl];
}
return "";
}
};
// 作者:da-xiong-ge-ge
// 链接:https://leetcode-cn.com/problems/encode-and-decode-tinyurl/solution/qiu-zi-fu-chuan-hashzhi-zai-shi-yong-62jin-zhi-bia/
// 来源:力扣(LeetCode)
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
class Solution {
const string base = "http://tinyurl.com/";
const string code = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
unordered_map<string, string> tiny2origin;
const int BASE_LEN = 19, KEY_LEN = 6;
public:
string generateFixLenRandKey() {
string key;
srand(time(0));
for (int i = 0; i < KEY_LEN; i++) {
key += code[random() % 62];
}
return key;
}
// Encodes a URL to a shortened URL.
string encode(string longUrl) {
string randKey = generateFixLenRandKey();
while (tiny2origin.find(randKey) != tiny2origin.end())
randKey = generateFixLenRandKey();
tiny2origin[randKey] = longUrl;
return base + randKey;
}
// Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
string key = shortUrl.substr(BASE_LEN);
if (tiny2origin.find(key) == tiny2origin.end())
return "";
return tiny2origin[key];
}
};