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]
}
相关推荐
快去睡觉~32 分钟前
力扣73:矩阵置零
算法·leetcode·矩阵
岁忧1 小时前
(nice!!!)(LeetCode 每日一题) 679. 24 点游戏 (深度优先搜索)
java·c++·leetcode·游戏·go·深度优先
小欣加油1 小时前
leetcode 3 无重复字符的最长子串
c++·算法·leetcode
元亓亓亓6 小时前
LeetCode热题100--101. 对称二叉树--简单
算法·leetcode·职场和发展
1白天的黑夜19 小时前
链表-24.两两交换链表中的结点-力扣(LeetCode)
数据结构·leetcode·链表
快去睡觉~13 小时前
力扣48:旋转矩阵
算法·leetcode·矩阵
卡洛斯(编程版14 小时前
(1) 哈希表全思路-20天刷完Leetcode Hot 100计划
python·算法·leetcode
MrZhangBaby16 小时前
SQL-leetcode—3374. 首字母大写 II
linux·sql·leetcode
bianshaopeng16 小时前
ubuntu go 环境变量配置
开发语言·ubuntu·golang
元清加油16 小时前
【Goland】:协程和通道
服务器·开发语言·后端·网络协议·golang