日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LeetCode Design TinyURL

發布時間:2025/3/20 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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:

  • 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-9,?a-z,?A-Z.
  • 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:

  • How many unique identifiers possible? Will you run out of unique URLs?
  • Should the identifier be increment or not? Which is easier to design? Pros and cons?
  • Mapping an identifier to an URL and its reversal - Does this problem ring a bell to you?
  • How do you store the URLs? Does a simple flat file database work?
  • What is the bottleneck of the system? Is it?read-heavy?or?write-heavy?
  • Estimate the maximum number of URLs a single machine can store.
  • Estimate the maximum number of queries per second (QPS) for decoding a shortened URL in a single machine.
  • 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.
  • How could you handle redundancy? i,e, if a server is down, how could you ensure the service is still operational?
  • Keep URLs forever or prune, pros/cons? How we do pruning? (Contributed by @alex_svetkin)
  • What API would you provide to a third-party developer? (Contributed by @alex_svetkin)
  • 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

    與50位技術專家面對面20年技術見證,附贈技術全景圖

    總結

    以上是生活随笔為你收集整理的LeetCode Design TinyURL的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。