49 Encode and Decode TinyURL

题目

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.

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.

分析

题意:设计一个简化URL的编码算法,比如
https://leetcode.com/problems/design-tinyurl编码为http://tinyurl.com/4e9iAk
当然http://tinyurl.com/4e9iAk解码的结果也为https://leetcode.com/problems/design-tinyurl

这个是开放性的题目,自行设计即可。
个人想法(以https://leetcode.com/problems/design-tinyurl为例):
https://leetcode.com/保持原样。我们假设所有的URL都要进行编码,那么主域名就没有编码的必要了。

https://leetcode.com/problems/design-tinyurl为例,个人觉得难点有两点:
1.xxx/design-tinyurl和xxx/design-tinyuri也应当被识别,因此url的识别粒度在单个符号的层面。
2.URL如何缩短–>26个字母+n个符号如何转为更少的符号表示–>如何保证26个字母+n个符号的转换过程可逆。

比如说1,2,3将其转为一个数的方法为1+2+3=6,但是6并不能唯一的转为1,2,3,因此这种方法不行。
难点就在于如何缩短之后还能保证可逆?

有哪些运算是多元运算得出单个结果,并且该结果也能逆运算为多个元?
百度了解了下,应该跟要使用压缩算法。
了解了下zip算法,也有个想法,但是写了半天写不下去了。

去看看别人的做法。

看完别人的做法。。。看样子是我想多了。

算法

将原始的uri对应任意一个唯一的字符,存放到map中,如{原始uri:唯一字符},并备份一份{唯一字符:原始uri}。
编码的时候就是`原始uri映射到唯一字符`,解码的时候用`唯一字符到原始uri的map`即可。

解答

public class Codec {
	//{识别字符:原始uri}
    Map<String, String> index = new HashMap<String, String>();
    //{原始uri,识别字符}
    Map<String, String> revIndex = new HashMap<String, String>();
    static String BASE_HOST = "http://tinyurl.com/";
    
    // Encodes a URL to a shortened URL.
    public String encode(String longUrl) {
        if (revIndex.containsKey(longUrl)) return BASE_HOST + revIndex.get(longUrl);
        String charSet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        String key = null;
        do {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 6; i++) {
                int r = (int) (Math.random() * charSet.length());
                sb.append(charSet.charAt(r));
            }
            key = sb.toString();
        } while (index.containsKey(key));
        index.put(key, longUrl);
        revIndex.put(longUrl, key);
        return BASE_HOST + key;
    }

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

简化版

    Map<Integer, String> map = new HashMap();
    String host = "http://tinyurl.com/";

    public String encode(String longUrl) {
      int key = longUrl.hashCode();
      map.put(key, longUrl);
      return host+key;
    }

    public String decode(String shortUrl) {
      int key = Integer.parseInt(shortUrl.replace(host,""));
      return map.get(key);
    }
全部评论

相关推荐

12-19 22:04
武汉大学 Java
点赞 评论 收藏
分享
12-06 01:10
已编辑
哈尔滨工程大学 Java
一面问的真细,二面不知为啥变双机位。9.29快手主站平时怎么学习&nbsp;AI&nbsp;的,国内外知名大模型,实习公司都用的什么大模型,怎么评估效果的java池化思想,线程池构造方法的核心参数,线程池中阻塞队列注意事项,submit方法参数和执行逻辑,shutdown和shutdownnow,核心线程允许过期吗threadlocal底层,为什么key是弱引用,key回收了再get或者set这个value会怎样aqs,如何保证公平性java代理java堆划分,新生代还有别的晋升老年代的情况吗,什么时候触发gc,gc失败抛什么异常,如何排查oom,导出dump命令redis数据结构,哪个底层是跳表,和其他数据结构对比布隆过滤器会出现大key问题吗,你咋实现的布隆过滤器你怎么实现redis分布式锁,可重入,续期聚簇索引非聚簇索引select语句会加锁吗,怎么实现的不加锁undolog&nbsp;redolog&nbsp;binlog怎么能让select加锁,update这个范围加的什么锁,update一条呢手撕简单01背包,接雨水10.10快手主站意图识别用的哪个大模型,走到意图和rag的比例,faq是点击的吗自然语言怎么识别的gap一年干啥了,转正怎么样没跟组里提意向吗,研究生研究方向是传统算法吗,会大模型微调吗注册场景为什么用布隆过滤器,原理分布式锁底层的key怎么拼的,value里是什么redis持久化zset底层mysql索引结构,一个表三个字段有主键唯一索引和没索引的字段会有几个b+树,聚簇索引非聚簇索引存的啥无手撕
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务