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]
}
相关推荐
h7997101 小时前
go资深之路笔记(八) 基准测试
golang·压力测试
Seven972 小时前
剑指offer-35、数组中的逆序对
java·leetcode
熬了夜的程序员2 小时前
【LeetCode】74. 搜索二维矩阵
线性代数·算法·leetcode·职场和发展·矩阵·深度优先·动态规划
坚持编程的菜鸟2 小时前
LeetCode每日一题——矩阵置0
c语言·算法·leetcode·矩阵
坚持编程的菜鸟8 小时前
LeetCode每日一题——困于环中的机器人
c语言·算法·leetcode·机器人
Achou.Wang13 小时前
源码分析 golang bigcache 高性能无 GC 开销的缓存设计实现
开发语言·缓存·golang
Yeats_Liao15 小时前
Go语言技术与应用(二):分布式架构设计解析
开发语言·分布式·golang
蓝婴天使15 小时前
基于 React + Go + PostgreSQL + Redis 的管理系统开发框架
react.js·postgresql·golang
脚踏实地的大梦想家15 小时前
【Go】P6 Golang 基础:流程控制
开发语言·golang
QX_hao15 小时前
【Go】--数组和切片
后端·golang·restful