原题链接在这里: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:

 public class URLService{
HashMap<String, Integer> ltos;
HashMap<Integer, String> stol;
static int COUNTER;
String elements; URLService(){
ltos = new HashMap<String, Integer>();
stol = new HashMap<Integer, String>();
COUNTER = 1;
elements = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
} public String longToShort(String url){
String shortUrl = base10ToBase62(COUNTER);
COUNTER++;
ltos.put(url, COUNTER);
stol.put(COUNTER, url);
return "http://tinyurl.com/" + shortUrl;
} public String shortToLong(String url){
url = url.substring("http://tiny.url/".length());
int n = base62ToBase10(url);
return stol.get(n);
} private int base62ToBase10(String s){
int n = 0;
for(int i = 0; i<s.length(); i++){
n = n*62 + convert(s.charAt(i));
}
return n;
} private int convert(char c){
if(c>='0' && c<='9'){
return c-'0';
}else if(c>='a' && c<='z'){
return c-'a'+10;
}else if(c>='A' && c<='Z'){
return c-'A'+36;
} return -1;
} private String base10ToBase62(int n){
StringBuilder sb = new StringBuilder();
while(n != 0){
sb.insert(0, elements.charAt(n%62));
n /= 62;
} while(sb.length() != 6){
sb.insert(0, '0');
} return sb.toString();
}
}

LeetCode Design TinyURL的更多相关文章

  1. [LeetCode] Design TinyURL 设计精简URL地址

    Note: For the coding companion problem, please see: Encode and Decode TinyURL. How would you design ...

  2. [LeetCode] 534. Design TinyURL 设计短网址

    Note: For the coding companion problem, please see: Encode and Decode TinyURL. How would you design ...

  3. [LeetCode] Design Phone Directory 设计电话目录

    Design a Phone Directory which supports the following operations: get: Provide a number which is not ...

  4. [LeetCode] Design Hit Counter 设计点击计数器

    Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...

  5. [LeetCode] Design Twitter 设计推特

    Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...

  6. [LeetCode] Design Snake Game 设计贪吃蛇游戏

    Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...

  7. [LeetCode] Design Tic-Tac-Toe 设计井字棋游戏

    Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the fol ...

  8. LeetCode Design Hit Counter

    原题链接在这里:https://leetcode.com/problems/design-hit-counter/. 题目: Design a hit counter which counts the ...

  9. [LeetCode] Design Search Autocomplete System 设计搜索自动补全系统

    Design a search autocomplete system for a search engine. Users may input a sentence (at least one wo ...

随机推荐

  1. MySQL-5.7 游标及DECLARE

    1.cursor游标 用来声明一个数据集 游标的声明必须在变量和条件声明之后,在handler声明之前 游标特性: 不灵敏:服务器可以或不复制其结果 只读:不可更新 不可滚动的:只能在一个方向上遍历, ...

  2. Java 中15种锁的介绍:公平锁,可重入锁,独享锁,互斥锁,乐观锁,分段锁,自旋锁等等

    Java 中15种锁的介绍 Java 中15种锁的介绍:公平锁,可重入锁,独享锁,互斥锁,乐观锁,分段锁,自旋锁等等,在读很多并发文章中,会提及各种各样锁如公平锁,乐观锁等等,这篇文章介绍各种锁的分类 ...

  3. MySQL详解--锁,事务(转)

    锁是计算机协调多个进程或线程并发访问某一资源的机制.在数据库中,除传统的计算资源(如CPU.RAM.I/O等)的争用以外,数据也是一种供许多用户共享的资源.如何保证数据并发访问的一致性.有效性是所有数 ...

  4. Book Review of “The practice of programming” (Ⅳ)

    The practice of programming Chapter 4 Interfaces A good programmer should always be good at designin ...

  5. JavaWeb 自定义标签库开发传统标签

    自定义标签主要用于移除Jsp页面中的java代码. 移除jsp页面中的java代码,只需要完成两个步骤: 编写一个实现Tag接口的Java类,并覆盖doStartTag方法,把jsp页面中的java代 ...

  6. Java 四大作用域总结

    一.ServletContext 1.生命周期:当Web应用被加载进容器时创建代表整个web应用的ServletContext对象,当服务器关闭或Web应用被移除时,ServletContext对象跟 ...

  7. redhat 6.8 配置yum源

    一般安装好redhat后,不能注册的话,不能使用系统自带的yum源.但是我们可以自己配置yum源来解决这一问题.下面介绍下redhat配置163yum源. 1. 检查是否安装yum包 rpm -qa ...

  8. AngularJS的思考

    AngularJS实践 什么是AngularJS AngularJS的核心理念是什么? 在我看来,Angualr的核心思想是:Template + Scope => HTML, Template ...

  9. ThinkPHP3.2添加scws中文分词

    前言 前一段时间,公司网站做站内搜索,只简单针对输入的文字进行搜索,作全匹配检索,搜索出来的内容很少.如何达到模糊搜索,匹配到更多的内容成了需要解决的问题.于是,今天想到可以做分词检索,如何对输入的一 ...

  10. ReflectionZ_测试_01

    1.Java代码 public class TreflectionZ { public static void main(String[] args) throws Exception { Class ...