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]
}
相关推荐
YuTaoShao26 分钟前
【LeetCode 热题 100】994. 腐烂的橘子——BFS
java·linux·算法·leetcode·宽度优先
好易学·数据结构9 小时前
可视化图解算法56:岛屿数量
数据结构·算法·leetcode·力扣·回溯·牛客网
墨染点香11 小时前
LeetCode Hot100【5. 最长回文子串】
算法·leetcode·职场和发展
ん贤12 小时前
如何加快golang编译速度
后端·golang·go
riverz122713 小时前
Go 程序无法使用 /etc/resolv.conf 的 DNS 配置排查记录
golang
im_AMBER14 小时前
Leetcode 03 java
算法·leetcode·职场和发展
轮到我狗叫了14 小时前
力扣.1312让字符串成为回文串的最少插入次数力扣.105从前序和中序遍历构造二叉树牛客.拼三角力扣.57插入区间编辑
算法·leetcode·职场和发展
As_wind_17 小时前
Go 语言学习之测试
开发语言·学习·golang
科大饭桶18 小时前
数据结构自学Day8: 堆的排序以及TopK问题
数据结构·c++·算法·leetcode·二叉树·c
木子.李34718 小时前
记录Leetcode中的报错问题
算法·leetcode·职场和发展