535. Encode and Decode TinyURL
来源:互联网 发布:车牌识别算法源代码 编辑:程序博客网 时间:2024/06/08 19:14
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.
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));
所以出现了下面的joke版答案,还ac了:
public class Codec { // Encodes a URL to a shortened URL. public String encode(String longUrl) { return longUrl; } // Decodes a shortened URL to its original URL. public String decode(String shortUrl) { return shortUrl; }}// Your Codec object will be instantiated and called as such:// Codec codec = new Codec();// codec.decode(codec.encode(url));相对正规的解法代码如下:
public class Codec { List<String> urls = new ArrayList<String>(); // Encodes a URL to a shortened URL. public String encode(String longUrl) { urls.add(longUrl); return String.valueOf(urls.size() - 1); } // Decodes a shortened URL to its original URL. public String decode(String shortUrl) { int index = Integer.parseInt(shortUrl); return urls.get(index); }}
0 0
- 535. Encode and Decode TinyURL
- 535. Encode and Decode TinyURL
- 535. Encode and Decode TinyURL
- 535. Encode and Decode TinyURL
- 535. Encode and Decode TinyURL
- 535. Encode and Decode TinyURL
- 535. Encode and Decode TinyURL
- 535. Encode and Decode TinyURL
- 535. Encode and Decode TinyURL
- 535. Encode and Decode TinyURL
- 535. Encode and Decode TinyURL
- Encode and Decode TinyURL
- Encode and Decode TinyURL
- 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
- http和https区别
- ORACLE 创建视图时,提示用户权限不足
- 我的 VMvare Workstation Pro 和 Ubuntu 的解决方案(持续更新)
- 欢迎使用CSDN-markdown编辑器
- Activity(活动)
- 535. Encode and Decode TinyURL
- Leetcode 155. Min Stack
- Lucene+hibernateSearch入门
- [深入剖析Tomcat]一个简单的servlet容器实现
- [深入剖析Tomcat]一个简单的servlet容器实现2
- java基础学习建议
- 音视频支持和音视频播放
- LeetCode 35. Search Insert Position
- h264视频解码