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 TinyURL service. There is no restriction on how your encode/decode algorithm should 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.

想了下可能的重复编码问题,直接用HashMap做了短URL和长URL的映射.将数字和字母混合编码,按题目的6位数的编码的话可以达到\(36^6\)种编码,理论上性能是ok的.

代码如下

public class Codec {

    HashMap<String, String> enIndex = new HashMap<String, String>();
    HashMap<String,String>deIndex = new HashMap<String, String>();
    String keyset = "0123456789abcdefghijklmnopqrstuvwyz";
    String tinyUrlBase = "http://tinyurl.com/";

    // Encodes a URL to a shortened URL.
    public String encode(String longUrl) {
        String key;
        if (enIndex.containsKey(longUrl)) return enIndex.get(longUrl);
        do{
            StringBuilder sb = new StringBuilder();
            for(int i =0 ; i<6;i++){
                sb.append(keyset.charAt((int) (Math.random()*keyset.length())));
            }
            key = sb.toString();
        }while(deIndex.containsKey(key));
        deIndex.put(key,longUrl);
        enIndex.put(longUrl, tinyUrlBase+key);
        return tinyUrlBase+key;
    }

    // Decodes a shortened URL to its original URL.
    public String decode(String shortUrl) {
        return deIndex.get(shortUrl.replace(tinyUrlBase,""));
    }
}

写完看了下排名.....貌似蛮低的.不过这题感觉是比较自由,应该不会太卡时间的,感觉就是考虑怎么编码好点.

前面的方案大致有:

  1. 用计数器编码

    那么当前服务器存了多少url就曝露出来了,也许会有安全隐患。而且计数器编码另一个缺点就是数字会不断的增大

  2. 直接返回参数(最快)

    ....这道题的OJ基本上是形同虚设,两个函数分别直接返回参数字符串也能通过OJ,囧

时间: 2024-11-05 22:43:25

Encode and Decode TinyURL的相关文章

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

[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 解题思路: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] 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

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

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

Python之encode与decode浅析

 Python之encode与decode浅析 在 python 源代码文件中,如果你有用到非ASCII字符,则需要在文件头部进行字符编码的声明,声明如下: # code: UTF-8 因为python 只检查 #.coding 和编码字符串,为了美观等原因可以如下写法: #-*-coding:utf-8-*- 常见编码介绍: GB2312编码:适用于汉字处理.汉字通信等系统之间的信息交换. GBK编码:是汉字编码标准之一,是在 GB2312-80 标准基础上的内码扩展规范,使用了双字节编码.