C++算法零基础100题-TinyUrl的加密和解密

94 阅读1分钟

题目链接: TinyURL 的加密与解密

题目描述

image.png

解体思路

  1. 我们加密的字符串,我们让第一个元素添加一个asc码位
  2. 我们解密的字符串。我们让第一个元素减少一个asc码位

代码实现

class Solution {
public:

    // Encodes a URL to a shortened URL.
    string encode(string longUrl) {
        longUrl[0]++;
        return longUrl;
    }

    // Decodes a shortened URL to its original URL.
    string decode(string shortUrl) {
        shortUrl[0]--;
        return shortUrl;
    }
};

// Your Solution object will be instantiated and called as such:
// Solution solution;
// solution.decode(solution.encode(url));