leetcode 535. Encode and Decode TinyURL
2017-10-16 18:29
567 查看
这个题目如果自己写一个算法的话确实很难,但是这个是没有什么要求的,因此就简单多了。最简单的就是不做任何处理,直接返回
class Solution {
public:
// Encodes a URL to a shortened URL.
string encode(string longUrl) {
return longUrl;
}
// Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
return shortUrl;
}
};
但是这种解法很无聊,因此我用了一种简单映射的方法来实现,设置一个count用来保证不同url的不同id。
class Solution {
public:
map<string,string> m;
int count=0;
// Encodes a URL to a shortened URL.
string encode(string longUrl) {
string shortUrl=to_string(count);
m.insert(pair<string,string>(shortUrl,longUrl));
count++;
return shortUrl;
}
// Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
return m[shortUrl];
}
};
class Solution {
public:
// Encodes a URL to a shortened URL.
string encode(string longUrl) {
return longUrl;
}
// Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
return shortUrl;
}
};
但是这种解法很无聊,因此我用了一种简单映射的方法来实现,设置一个count用来保证不同url的不同id。
class Solution {
public:
map<string,string> m;
int count=0;
// Encodes a URL to a shortened URL.
string encode(string longUrl) {
string shortUrl=to_string(count);
m.insert(pair<string,string>(shortUrl,longUrl));
count++;
return shortUrl;
}
// Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
return m[shortUrl];
}
};
相关文章推荐
- 【LeetCode】535. Encode and Decode TinyURL
- 20170613-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 (HashMap)
- Leetcode刷题之路(Python)——535. Encode and Decode TinyURL
- Leetcode 535 Encode and Decode TinyURL
- LeetCode 535: Encode and Decode TinyURL
- LeetCode 535. Encode and Decode TinyURL对随机编码的思考
- leetcode535 Encode and Decode TinyURL java
- 【LeetCode】535. Encode and Decode TinyURL
- leetcode题解-535. Encode and Decode TinyURL
- LeetCode:535. Encode and Decode TinyURL
- Leetcode 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
- [LeetCode] Encode and Decode TinyURL 编码和解码精简URL地址
- 535. Encode and Decode TinyURL