Leetcode 535 Encode and Decode TinyURL
来源:互联网 发布:c语言迷宫程序 编辑:程序博客网 时间:2024/06/08 00:38
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.
编码的方式可以有很多种,因为一般都会受到Integer范围的限制,所以说可以选择固定长度的编码形式
public class Codec { String alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; Random r = new Random(); Map<String, String> map = new HashMap<>(); String key = getString(); private String getString(){ StringBuffer sb = new StringBuffer(); for(int i = 0; i < 6; i++){ sb.append(alphabet.charAt(r.nextInt(62))); } return sb.toString(); } // Encodes a URL to a shortened URL. public String encode(String longUrl) { while(map.containsKey(key)){ key = getString(); } map.put(key, longUrl); return "http://tinyurl.com/" + key; } // Decodes a shortened URL to its original URL. public String decode(String shortUrl) { return map.get(shortUrl.replace("http://tinyurl.com/", "")); }}// Your Codec object will be instantiated and called as such:// Codec codec = new Codec();// codec.decode(codec.encode(url));
阅读全文
0 0
- 20170613-leetcode-535-Encode and Decode TinyURL
- LeetCode 535 Encode and Decode TinyURL (HashMap)
- Leetcode 535 Encode and Decode TinyURL
- LeetCode-Encode and Decode TinyURL
- 535 Encode and Decode TinyURL
- leetcode题解-535. Encode and Decode TinyURL
- 【LeetCode】535. Encode and Decode TinyURL
- LeetCode:535. Encode and Decode TinyURL
- Leetcode 535. Encode and Decode TinyURL
- leetcode 535. Encode and Decode TinyURL
- Leetcode 535. Encode and Decode TinyURL
- LeetCode 535. Encode and Decode TinyURL
- 【LeetCode】535. Encode and Decode TinyURL
- python--leetcode 535. Encode and Decode TinyURL
- leetcode 535. Encode and Decode TinyURL
- Encode and Decode TinyURL
- Encode and Decode TinyURL
- Encode and Decode TinyURL
- POJ 1338 & UVA 136
- google chrome浏览器离线小恐龙游戏刷分bug
- 文章标题
- phpstorm 光标设置
- windows下python 缺少cv2
- Leetcode 535 Encode and Decode TinyURL
- 字符串加解密例子
- python2用pip进行安装时报错Fatal error in launcher: Unable to create process using '"'
- 【大神评论】小米是怎么火起来的
- linux pwd命令查看当前路径命令
- Nginx/LVS/HAProxy负载均衡软件的优缺点详解
- php 文件上传$_FILES中error返回值详解
- jQuery:我爱banner
- 101个鲜为人知的超实用网站