題目
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);
}