IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> LeetCode 535(C#) -> 正文阅读

[数据结构与算法]LeetCode 535(C#)

题目

TinyURL 是一种 URL 简化服务, 比如:当你输入一个 URL https://leetcode.com/problems/design-tinyurl 时,它将返回一个简化的URL http://tinyurl.com/4e9iAk 。请你设计一个类来加密与解密 TinyURL 。

加密和解密算法如何设计和运作是没有限制的,你只需要保证一个 URL 可以被加密成一个 TinyURL ,并且这个 TinyURL 可以用解密方法恢复成原本的 URL 。

实现 Solution 类:

Solution() 初始化 TinyURL 系统对象。
String encode(String longUrl) 返回 longUrl 对应的 TinyURL 。
String decode(String shortUrl) 返回 shortUrl 原本的 URL 。题目数据保证给定的 shortUrl 是由同一个系统对象加密的。

示例:
输入:url = “https://leetcode.com/problems/design-tinyurl”
输出:“https://leetcode.com/problems/design-tinyurl”

解释:
Solution obj = new Solution();
string tiny = obj.encode(url); // 返回加密后得到的 TinyURL 。
string ans = obj.decode(tiny); // 返回解密后得到的原本的 URL 。

代码

  • 直接寻址(哈希)
public class Codec 
{
    private const int K1 = 10007;
    private const int K2 = 1000000007;
    private Dictionary<string, int> url2Key = new Dictionary<string, int>();
    private Dictionary<int, string> key2Url = new Dictionary<int, string>();

    // Encodes a URL to a shortened URL
    public string encode(string longUrl)
    {
        if (url2Key.ContainsKey(longUrl))
            return "http://tinyurl.com/" + url2Key[longUrl];
        
        int key = 0;
        long b = 1;
        foreach (char c in longUrl)
        {
            key = (int)((long)key * b + c) % K2;
            b = (b * K1) % K2;
        }

        while (key2Url.ContainsKey(key))
            key++;
        
        url2Key.Add(longUrl, key);
        key2Url.Add(key, longUrl);

        return "http://tinyurl.com/" + key;
    }

    // Decodes a shortened URL to its original URL.
    public string decode(string shortUrl) 
    {
        int idx = shortUrl.LastIndexOf('/') + 1;
        int key = int.Parse(shortUrl[idx..]);
        return key2Url[key];
    }
}
  • 拉链寻址(哈希)
public class Codec 
{
    private const int K1 = 10007;
    private const int K2 = 1000000007;
    private Dictionary<string, (int, int)> url2Key = 
        new Dictionary<string, (int, int)>();
    private Dictionary<int, IList<string>> key2Url = 
        new Dictionary<int, IList<string>>();

    // Encodes a URL to a shortened URL
    public string encode(string longUrl)
    {
        if (url2Key.ContainsKey(longUrl))
        {
            var key = url2Key[longUrl];
            return "http://tinyurl.com/" + key.Item1.ToString() + '.' + key.Item2.ToString();
        }
        
        int key1 = 0;
        long P = 1;
        foreach (char c in longUrl)
        {
            key1 = (int)((long)(key1 * P + c) % K2);
            P = P * K1 % K2;
        }

        if (!key2Url.ContainsKey(key1)) key2Url.Add(key1, new List<string>());
        key2Url[key1].Add(longUrl);

        int key2 = key2Url[key1].Count - 1;
        url2Key.Add(longUrl, (key1, key2));
        
        return "http://tinyurl.com/" + key1.ToString() + '.' + key2.ToString();
    }

    // Decodes a shortened URL to its original URL.
    public string decode(string shortUrl) 
    {
        string[] strs = shortUrl[(shortUrl.LastIndexOf('/') + 1)..].Split('.');
        int key1 = int.Parse(strs[0]);
        int key2 = int.Parse(strs[1]);

        return key2Url[key1][key2];
    }
}
  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2022-07-03 11:03:35  更:2022-07-03 11:04:45 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/21 9:18:27-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码