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]
}
相关推荐
踩坑记录6 小时前
leetcode hot100 2.两数相加 链表 medium
leetcode·链表
历程里程碑9 小时前
滑动窗口---- 无重复字符的最长子串
java·数据结构·c++·python·算法·leetcode·django
TracyCoder12310 小时前
LeetCode Hot100(18/100)——160. 相交链表
算法·leetcode
放荡不羁的野指针11 小时前
leetcode150题-滑动窗口
数据结构·算法·leetcode
女王大人万岁12 小时前
Go标准库 io与os库详解
服务器·开发语言·后端·golang
TracyCoder12312 小时前
LeetCode Hot100(13/100)——238. 除了自身以外数组的乘积
算法·leetcode
Anastasiozzzz12 小时前
LeetCode Hot100 215. 数组中的第K个最大元素
数据结构·算法·leetcode
让我上个超影吧12 小时前
【力扣76】最小覆盖子串
算法·leetcode·职场和发展
求梦82014 小时前
【力扣hot100题】合并两个有序链表(22)
算法·leetcode·链表
女王大人万岁14 小时前
Go语言time库核心用法与实战避坑
服务器·开发语言·后端·golang