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]
}
相关推荐
姚不倒9 小时前
Go语言进阶:接口、错误处理与并发编程(goroutine/channel/context)
云原生·golang
失去的青春---夕阳下的奔跑12 小时前
560. 和为 K 的子数组
数据结构·算法·leetcode
m0_6294947313 小时前
LeetCode 热题 100-----25.回文链表
数据结构·算法·leetcode·链表
吃着火锅x唱着歌15 小时前
LeetCode 1019.链表中的下一个更大节点
算法·leetcode·链表
凌波粒16 小时前
LeetCode--404.左叶子之和(二叉树)
算法·leetcode·职场和发展
绝知此事16 小时前
【算法突围 03】核心算法思想:分治/递归/动态规划与 LeetCode 高频真题解析
算法·leetcode·面试·动态规划
宇明一不急16 小时前
go 链表 (标准库实现)
开发语言·链表·golang
阿Y加油吧17 小时前
两道字符串 DP 模板题复盘:最长公共子序列 & 编辑距离
leetcode
~|Bernard|18 小时前
GO语言中哪些类型是可比较类型的(==和!=)
开发语言·后端·golang
我爱cope18 小时前
【力扣hot100:76. 最小覆盖子串】
算法·leetcode·职场和发展