zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    【Difference Between Primes HDU
    【Pet HDU
    《Java程序设计实验》 软件工程18-1,3 OO实验2
    【数据结构作业】-【带头结点的单链表就地逆置】
    【Miscalculation UVALive
    【Bit String Reordering UVALive
    【Bazinga HDU
    (转载)博弈汇总【巴什博奕,威佐夫博弈,尼姆博弈,斐波那契博弈】
    【Audiophobia UVA
    【Calling Circles UVA
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/7776511.html
Copyright © 2011-2022 走看看