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]
}
相关推荐
鹏北海-RemHusband9 小时前
Go 语言进阶笔记 — 面向 JS/TS 前端开发者
笔记·golang
罗超驿13 小时前
9.LeetCode 209. 长度最小的子数组 | 滑动窗口专题详解
java·算法·leetcode·面试
水蓝烟雨13 小时前
0135. 分发糖果
算法·leetcode
如竟没有火炬14 小时前
乘法表中第K小的数——二分
开发语言·数据结构·python·算法·leetcode·职场和发展·动态规划
诚威_lol_中大努力中17 小时前
Hot-146 LRU(最近最少使用Least Recent Use)缓存
leetcode
x_xbx17 小时前
LeetCode:739. 每日温度
算法·leetcode·职场和发展
Starry-sky(jing)17 小时前
Hermes Agent 接入 Qwen3.7-Max 报 401?OpenCode Go 模型路由源码级排查与修复
开发语言·人工智能·chrome·golang
鹏北海-RemHusband18 小时前
Go 语言基础笔记 — 面向 JS/TS 前端开发者
笔记·golang
圣保罗的大教堂18 小时前
leetcode 3121. 统计特殊字母的数量 II 中等
leetcode
圣保罗的大教堂18 小时前
leetcode 3120. 统计特殊字母的数量 I 简单
leetcode