Leetcode Problem Solving ideas: 535. Encode and Decode TinyURL

Source: Internet
Author: User

TinyURL is a URL shortening service where you enter a URL such as and https://leetcode.com/problems/design-tinyurl it returns a short URL such as http://tinyurl.com/4e9iAk .

Design the and methods for the encode decode tinyurl service. There is no restriction on what your encode/decode algorithm should work. You just need to ensure, that a URL can is encoded to a tiny URL and the tiny URL can is decoded to the original URL.

Test instructions is the URL design encoding and decoding, so that the long URL into a short URL. There are no strict restrictions on codec algorithms, as long as the length of the URL can be guaranteed to convert each other.

Basic ideas:

Encode: Generates a sequence number based on the URL, and the URL is stored in the sequence number corresponding to the vector or unordered_map, returning a string of http://tinyurl.com/+ ordinal

Decode: Get the serial number from the short URL and find the corresponding sequence number from the container.

Simple example: Use Longurl to identify the storage location in the vector

1 classSolution {2  Public:3 4     //coding: Long and short5     stringEncodestringLongurl) {6 Url.push_back (longurl);7         return "http://tinyurl.com/"+ to_string (url.size ()-1);8     }9 Ten     //decoding: Short variable length One     stringDecodestringShorturl) { Asize_t pos = shorturl.find_last_of ("/"); -         returnUrl[stoi (SHORTURL.SUBSTR (pos +1))]; -     } the Private: -vector<string>URL; -};

But what if it's not the same as the title? There is a small solution, the problem is unrelated, simply say how to design tinyurl, only the idea did not write code.

The basic idea remains unchanged, still stored in the container, or the calling interface is written in the database/redis.

Coding: Using the title "4e9iak" such as the code can be considered to replace the URL with a 0 ~ 9 + ' a ' ~ ' Z ' + ' a ' ~ ' Z ' to a 5-bit 62-decimal number, so that each URL is saved once, a total can be saved 62^5 = 916,132,832 different URLs, the most The latter one has his other use;

The URL can be used as a hash function (write itself) into an integer number (<916132832), the URL is stored in the container in the position corresponding to the number of positions, and the integer number into a 62 binary number;

The last one defaults to 0, if the location already has an address, it offsets 62 bits, if it is still occupied, will offset 62*2 and so on, with the last record is offset a few times;

If the offset is still not found location (that has been saved a lot), determine whether the current container size is more than half of the container, if the expansion, if not, the original hash is added to the URL followed by a hash calculation, with a new hash value to save the URL.

Decode: Follow the encoding rules to the integer number, and then the container/database URL out.

There is a trickery method, to share to some of the classmates, of course, is not bad, because the topic has no specific requirements, can accept.

class Solution {public:    string encode (string  longurl) {         return longurl;    }     string decode (string  shorturl) {        return  shorturl;    }};

Leetcode Problem Solving ideas: 535. Encode and Decode TinyURL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.