LeetCode solution ideas: 535. Encode and Decode TinyURL, leetcode535.encode
TinyURL is a URL shortening service where you enter a URL suchhttps://leetcode.com/problems/design-tinyurl
And it returns a short URL suchhttp://tinyurl.com/4e9iAk
.
Designencode
Anddecode
Methods for the TinyURL service. there is no restriction on how your encode/decode algorithm shold work. you just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL.
It is designed to encode and decode the url to make the long url into a short url. There is no strict limitation on the CODEC algorithm, as long as the length of the url can be converted to each other.
Basic Ideas:
Encode: generate a sequence number based on the url, and save the url with vector or unordered_map in the corresponding item of the sequence number. The returned string is http://tinyurl.com/#sequence number.
Decode: Get the serial number from the short URL and find the corresponding serial number from the container.
A simple example: Use longUrl to identify the storage location in the vector
1 class Solution {2 public: 3 4 // code: length to 5 string encode (string longUrl) {6 url. push_back (longUrl); 7 return "http://tinyurl.com/" + to_string (url. size ()-1); 8} 9 10 // decoding: Short Variable Length 11 string decode (string signed URL) {12 size_t pos = signed URL. find_last_of ("/"); 13 return url [stoi (Response url. substr (pos + 1)]; 14} 15 private: 16 vector <string> url; 17 };
But what should I do if it is different from the question? There is a small solution that has nothing to do with solving the problem. I just want to explain how to design tinyurl, but I have no idea to write code.
The basic idea remains unchanged and is still saved in the container, or the called interface is written in the database/redis.
Encoding: the question "4e9iAk" can be used to convert the URL into a 0 ~ 9 + 'A '~ 'Z' + 'A '~ 'Z' is replaced by a 5-digit 62-digit number, so that each url is saved once. A total of 62 ^ 5 = 916132832 different URLs can be saved, and the last one can be used separately;
You can use a hash function (self-written) to convert a url into an integer (<916132832), and store the url in the location corresponding to the integer in the container, and convert the integer to a 62-digit number;
The last one defaults to 0. If an address already exists at this position, the offset is 62. if the address is still occupied, the offset is 62*2, and so on, offset several times with the last record;
If the location is not found many times after the offset (it has already been saved many times), determine whether the current container size exceeds half of the container size. If yes, expand the container. If no, add the original hash value to the end of the url for another hash calculation, and save the url with the new hash value.
Decoding: decodes integer data according to the encoding rules, and then extracts the URLs in the container/database.
There is also a clever way to share it with some just-seeking students, of course, not bad, because the question does not have specific requirements, you can accept.
class Solution {public: string encode(string longUrl) { return longUrl; } string decode(string shortUrl) { return shortUrl; }};
Another idea is to compress the algorithm into binary to compress adjacent 0 and 1, for example, 00000000 to 080, or use RLE (Run Length Encoding) replace duplicate words. The advantage of this method is that there is no need for separate storage space, and all of them are directly converted, which meets both the requirements and is fast. The downside is that the amount of written code is a little large.