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]
}
相关推荐
小雅痞18 分钟前
[Java][Leetcode middle] 167. 两数之和 II - 输入有序数组
java·算法·leetcode
xin_nai1 小时前
LeetCode热题100(Java)(6)矩阵
java·leetcode·矩阵
geovindu1 小时前
go: Visitor Pattern
开发语言·设计模式·golang·访问者模式
会编程的土豆7 小时前
洛谷题单入门1 顺序结构
数据结构·算法·golang
圣保罗的大教堂9 小时前
leetcode 1855. 下标对中的最大距离 中等
leetcode
脱氧核糖核酸__13 小时前
LeetCode热题100——234.回文链表(两种解法)
c++·算法·leetcode·链表
生信研究猿13 小时前
leetcode 416. 分割等和子集
算法·leetcode·职场和发展
6Hzlia14 小时前
【Hot 100 刷题计划】 LeetCode 24. 两两交换链表中的节点 | C++ 精准指针舞步
c++·leetcode·链表
亿牛云爬虫专家15 小时前
Go爬虫进阶:如何优雅地在Colly框架中实现无缝代理切换?
爬虫·中间件·golang·爬虫代理·colly框架·代理切换·api提取
会编程的土豆17 小时前
洛谷题单 入门1 顺序结构(go语言)
开发语言·后端·golang·洛谷