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]
}
相关推荐
o0o_-_10 小时前
【go/gopls/mcp】官方gopls内置mcp server使用
开发语言·后端·golang
_不会dp不改名_12 小时前
leetcode_21 合并两个有序链表
算法·leetcode·链表
吃着火锅x唱着歌12 小时前
LeetCode 3302.字典序最小的合法序列
leetcode
睡不醒的kun12 小时前
leetcode算法刷题的第三十四天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划
吃着火锅x唱着歌12 小时前
LeetCode 978.最长湍流子数组
数据结构·算法·leetcode
爱编程的化学家15 小时前
代码随想录算法训练营第十一天--二叉树2 || 226.翻转二叉树 / 101.对称二叉树 / 104.二叉树的最大深度 / 111.二叉树的最小深度
数据结构·c++·算法·leetcode·二叉树·代码随想录
吃着火锅x唱着歌16 小时前
LeetCode 1446.连续字符
算法·leetcode·职场和发展
愚润求学16 小时前
【贪心算法】day10
c++·算法·leetcode·贪心算法
Tisfy17 小时前
LeetCode 0966.元音拼写检查器:三个哈希表实现
leetcode·字符串·散列表·题解·哈希表
ゞ 正在缓冲99%…17 小时前
leetcode35.搜索插入位置
java·算法·leetcode·二分查找