535. Encode and Decode TinyURL - LeetCode

Question

535. Encode and Decode TinyURL

Solution

题目大意:实现长链接加密成短链接,短链接解密成长链接

思路:加密成短链接+key,将长链接按key保存到map,解密时根据短链接提取key,再从map中返回长链接

Java实现:

public class Codec {

    // https://leetcode.com/problems/design-tinyurl --> http://tinyurl.com/4e9iAk
    Map<Integer, String> map = new HashMap<>();
    int i = 0;

    // Encodes a URL to a shortened URL.
    public String encode(String longUrl) {
        map.put(i, longUrl);
        return "http://tinyurl.com/" + (i++);
    }

    // Decodes a shortened URL to its original URL.
    public String decode(String shortUrl) {
        int key = Integer.parseInt(shortUrl.substring(shortUrl.lastIndexOf("/") + 1));
        return map.get(key);
    }
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));

Ref

https://leetcode.com/problems/encode-and-decode-tinyurl/discuss/100270/Three-different-approaches-in-java

Using simple counter

public class Codec {
    Map<Integer, String> map = new HashMap<>();
    int i=0;
    public String encode(String longUrl) {
        map.put(i,longUrl);
        return "http://tinyurl.com/"+i++;
    }
    public String decode(String shortUrl) {
        return map.get(Integer.parseInt(shortUrl.replace("http://tinyurl.com/", "")));
    }
}

Using hashcode

public class Codec {
    Map<Integer, String> map = new HashMap<>();
    public String encode(String longUrl) {
        map.put(longUrl.hashCode(),longUrl);
        return "http://tinyurl.com/"+longUrl.hashCode();
    }
    public String decode(String shortUrl) {
        return map.get(Integer.parseInt(shortUrl.replace("http://tinyurl.com/", "")));
    }
}

Using random function

public class Codec {
    Map<Integer, String> map = new HashMap<>();
    Random r=new Random();
    int key=r.nextInt(10000);
    public String encode(String longUrl) {
        while(map.containsKey(key))
            key= r.nextInt(10000);
        map.put(key,longUrl);
        return "http://tinyurl.com/"+key;
    }
    public String decode(String shortUrl) {
        return map.get(Integer.parseInt(shortUrl.replace("http://tinyurl.com/", "")));
    }
}

原文地址:https://www.cnblogs.com/okokabcd/p/9383836.html

时间: 2024-11-05 22:49:39

535. Encode and Decode TinyURL - LeetCode的相关文章

LeetCode 解题思路:535.Encode and Decode TinyURL

TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk. Design the encode and decode methods for the TinyURL service. There is no res

[LeetCode] 535. Encode and Decode TinyURL 编码和解码短URL

Note: This is a companion problem to the System Design problem: Design TinyURL.TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk

535. Encode and Decode TinyURL(rand and srand)

Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iA

535. Encode and Decode TinyURL

class Solution { public: long max_id = 0; unordered_map<long,string> id_long; // Encodes a URL to a shortened URL. string encode(string longUrl) { id_long[max_id++] = longUrl; return to_string(max_id - 1); } // Decodes a shortened URL to its origina

Encode and Decode TinyURL

Encode and Decode TinyURL TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk. Design the encode and decode methods for the TinyUR

[LeetCode] Encode and Decode TinyURL 编码和解码小URL地址

Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iA

[LeetCode] Encode and Decode TinyURL

TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk. Design the encode and decode methods for the TinyURL service. There is no res

leetcode535 - Encode and Decode TinyURL - medium

TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk.Design the encode and decode methods for the TinyURL service. There is no rest

Encode and Decode Strings -- LeetCode

Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings. Machine 1 (sender) has the function: string encode(vector<string> strs) { // ... your