solution.md 3.1 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7
# TinyURL 的加密与解密

<p>TinyURL是一种URL简化服务, 比如:当你输入一个URL&nbsp;<code>https://leetcode.com/problems/design-tinyurl</code>&nbsp;时,它将返回一个简化的URL&nbsp;<code>http://tinyurl.com/4e9iAk</code>.</p>

<p>要求:设计一个 TinyURL 的加密&nbsp;<code>encode</code>&nbsp;和解密&nbsp;<code>decode</code>&nbsp;的方法。你的加密和解密算法如何设计和运作是没有限制的,你只需要保证一个URL可以被加密成一个TinyURL,并且这个TinyURL可以用解密方法恢复成原本的URL。</p>


每日一练社区's avatar
每日一练社区 已提交
8
<p>以下<font color="red">错误</font>的选项是?</p>
每日一练社区's avatar
每日一练社区 已提交
9 10

## aop
11

每日一练社区's avatar
每日一练社区 已提交
12
### before
13

每日一练社区's avatar
每日一练社区 已提交
14 15 16 17 18
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
19

每日一练社区's avatar
每日一练社区 已提交
20 21 22 23 24 25 26
```cpp
// Your Solution object will be instantiated and called as such:
// Solution solution;
// solution.decode(solution.encode(url));
```

## 答案
27

每日一练社区's avatar
每日一练社区 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
```cpp
class Solution
{
public:
    map<string, string> mp;

    string encode(string longUrl)
    {
        string str = "tinyurl" + to_string(key);
        mp[str] = longUrl;
        return str;
    }

    string decode(string shortUrl)
    {
        return mp[shortUrl];
    }
};
```
## 选项

### A
```cpp
class Solution
{
public:
    unordered_map<string, string> hashMap;
    string TINYURL_PREFIX = "http://tinyurl.com/";
    string INDEX = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    string encode(string longUrl)
    {
        string newShortStr = "";
        while (true)
        {

            for (int i = 0; i < 6; ++i)
            {
                newShortStr += INDEX[rand() % 62];
            }

            string resStr = TINYURL_PREFIX + newShortStr;
            if (hashMap.count(resStr) == 0)
            {
                hashMap[resStr] = longUrl;
                return resStr;
            }
        }
    }
    string decode(string shortUrl)
    {
        return hashMap[shortUrl];
    }
};
```

### B
```cpp
class Solution
{
public:
    using ull = unsigned long long;
    const ull base = 11;
    const string code = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    const ull len = code.size();
    unordered_map<string, string> m;

    ull hashCode(string url)
    {
        ull hash = 0;
        for (auto c : url)
        {
            hash *= base;
            hash += c;
        }
        return hash;
    }
    string hashToString(ull n)
    {
        string ans;
        while (n)
        {
            ans += code[n % len];
            n /= len;
        }
        return ans;
    }

    string encode(string longUrl)
    {
        string shortUrl = hashToString(hashCode(longUrl));
        m[shortUrl] = longUrl;
        return shortUrl;
    }

    string decode(string shortUrl)
    {
        return m[shortUrl];
    }
};
```

### C
```cpp
class Solution
{
public:
    unordered_map<string, string> mp;
    int i = 0;
    string encode(string longUrl)
    {
        mp[to_string(i)] = longUrl;
        string res = "http://tinyurl.com/" + to_string(i++);
        return res;
    }
    string decode(string shortUrl)
    {
        return mp[shortUrl.substr(19, shortUrl.size() - 19)];
    }
};
```