Golang | Leetcode Golang题解之第535题TinyURL的加密与解密

题目:

题解:

Go 复制代码
import "math/rand"

type Codec map[int]string

func Constructor() Codec {
    return Codec{}
}

func (c Codec) encode(longUrl string) string {
    for {
        key := rand.Int()
        if c[key] == "" {
            c[key] = longUrl
            return "http://tinyurl.com/" + strconv.Itoa(key)
        }
    }
}

func (c Codec) decode(shortUrl string) string {
    i := strings.LastIndexByte(shortUrl, '/')
    key, _ := strconv.Atoi(shortUrl[i+1:])
    return c[key]
}
相关推荐
做怪小疯子4 小时前
LeetCode 热题 100——哈希——最长连续序列
算法·leetcode·哈希算法
Dream it possible!4 小时前
LeetCode 面试经典 150_二叉树_二叉树展开为链表(74_114_C++_中等)
c++·leetcode·链表·面试·二叉树
做怪小疯子4 小时前
LeetCode 热题 100——双指针——三数之和
算法·leetcode·职场和发展
sin_hielo4 小时前
leetcode 2536
数据结构·算法·leetcode
flashlight_hi4 小时前
LeetCode 分类刷题:203. 移除链表元素
算法·leetcode·链表
py有趣4 小时前
LeetCode算法学习之数组中的第K个最大元素
学习·算法·leetcode
吗~喽4 小时前
【LeetCode】将 x 减到 0 的最小操作数
算法·leetcode
stand_forever6 小时前
PHP客户端调用由Go服务端GRPC接口
rpc·golang·php
flashlight_hi8 小时前
LeetCode 分类刷题:3217. 从链表中移除在数组中存在的节点
javascript·数据结构·leetcode·链表
Tisfy8 小时前
LeetCode 2536.子矩阵元素加 1:二维差分数组
算法·leetcode·矩阵