LeetCode Design TinyURL

原题链接在这里:https://leetcode.com/problems/design-tinyurl/description/

题目:

How would you design a URL shortening service that is similar to TinyURL?

Background:
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.

Requirements:

  1. For instance, "http://tinyurl.com/4e9iAk" is the tiny url for the page "https://leetcode.com/problems/design-tinyurl". The identifier (the highlighted part) can be any string with 6 alphanumeric characters containing 0-9a-zA-Z.
  2. Each shortened URL must be unique; that is, no two different URLs can be shortened to the same URL.

 

Note about Questions:
Below are just a small subset of questions to get you started. In real world, there could be many follow ups and questions possible and the discussion is open-ended (No one true or correct way to solve a problem). If you have more ideas or questions, please ask in Discuss and we may compile it here!

Questions:

    1. How many unique identifiers possible? Will you run out of unique URLs?
    2. Should the identifier be increment or not? Which is easier to design? Pros and cons?
    3. Mapping an identifier to an URL and its reversal - Does this problem ring a bell to you?
    4. How do you store the URLs? Does a simple flat file database work?
    5. What is the bottleneck of the system? Is it read-heavy or write-heavy?
    6. Estimate the maximum number of URLs a single machine can store.
    7. Estimate the maximum number of queries per second (QPS) for decoding a shortened URL in a single machine.
    8. How would you scale the service? For example, a viral link which is shared in social media could result in a peak QPS at a moment's notice.
    9. How could you handle redundancy? i,e, if a server is down, how could you ensure the service is still operational?
    10. Keep URLs forever or prune, pros/cons? How we do pruning? (Contributed by @alex_svetkin)
    11. What API would you provide to a third-party developer? (Contributed by @alex_svetkin)
    12. If you can enable caching, what would you cache and what's the expiry time? (Contributed by @Humandroid)

题解:

是System Design题目. 参考了这篇帖子.

按照SNAKE的方法逐个分析.

AC Java:

 1 public class URLService{
 2     HashMap<String, Integer> ltos;
 3     HashMap<Integer, String> stol;
 4     static int COUNTER;
 5     String elements;
 6     
 7     URLService(){
 8         ltos = new HashMap<String, Integer>();
 9         stol = new HashMap<Integer, String>();
10         COUNTER = 1;
11         elements = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
12     }
13     
14     public String longToShort(String url){
15         String shortUrl = base10ToBase62(COUNTER);
16         COUNTER++;
17         ltos.put(url, COUNTER);
18         stol.put(COUNTER, url);
19         return "http://tinyurl.com/" + shortUrl;
20     }
21     
22     public String shortToLong(String url){
23         url = url.substring("http://tiny.url/".length());
24         int n = base62ToBase10(url);
25         return stol.get(n);
26     }
27     
28     private int base62ToBase10(String s){
29         int n = 0;
30         for(int i = 0; i<s.length(); i++){
31             n = n*62 + convert(s.charAt(i));
32         }
33         return n;
34     }
35     
36     private int convert(char c){
37         if(c>='0' && c<='9'){
38             return c-'0';
39         }else if(c>='a' && c<='z'){
40             return c-'a'+10;
41         }else if(c>='A' && c<='Z'){
42             return c-'A'+36;
43         }
44         
45         return -1;
46     }
47     
48     private String base10ToBase62(int n){
49         StringBuilder sb = new StringBuilder();
50         while(n != 0){
51             sb.insert(0, elements.charAt(n%62));
52             n /= 62;
53         }
54         
55         while(sb.length() != 6){
56             sb.insert(0, '0');
57         }
58         
59         return sb.toString();
60     }
61 }

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/7776511.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值