分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

535. Encode and Decode TinyURL(rand and srand)

发布时间:2023-09-06 02:23责任编辑:胡小海关键词:暂无标签
Note: This is a companion problem to the System Design problem: Design TinyURL.

TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk.

Design the encode and decode methods for the TinyURL service. There is no restriction on how your encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL.

Approach #1: C++.

class Solution {public: ???// Encodes a URL to a shortened URL. ???string encode(string longUrl) { ???????if (longToTiny.count(longUrl)) return baseUrl + longToTiny[longUrl]; ???????string tinyString = ""; ???????do { ???????????for (int i = 0; i < 6; ++i) { ???????????????//srand((unsigned)time(0)); ???????????????int index = rand() % characters.length(); ???????????????tinyString += characters[index]; ???????????} ???????????} while (longToTiny.count(tinyString)); ???????????????longToTiny[longUrl] = tinyString; ???????tinyToLong[baseUrl+tinyString] = longUrl; ???????????????return baseUrl + tinyString; ???} ???// Decodes a shortened URL to its original URL. ???string decode(string shortUrl) { ???????return tinyToLong[shortUrl]; ???} ???private: ???string baseUrl = "http://www.leetcode.com/"; ???string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"; ???unordered_map<string, string> longToTiny; ???unordered_map<string, string> tinyToLong;};// Your Solution object will be instantiated and called as such:// Solution solution;// solution.decode(solution.encode(url));

  

Approach #2: Python.

class Codec: ???alphabet = string.ascii_letters + ‘0123456789‘ ???????def __init__(self): ???????self.url2code = {} ???????self.code2url = {} ???def encode(self, longUrl): ???????"""Encodes a URL to a shortened URL. ???????????????:type longUrl: str ???????:rtype: str ???????""" ???????while longUrl not in self.url2code: ???????????code = ‘‘.join(random.choice(Codec.alphabet) for _ in range(6)) ???????????if code not in self.code2url: ???????????????self.code2url[code] = longUrl ???????????????self.url2code[longUrl] = code ???????return ‘http://tinyurl.com/‘ + self.url2code[longUrl] ???????????def decode(self, shortUrl): ???????"""Decodes a shortened URL to its original URL. ???????????????:type shortUrl: str ???????:rtype: str ???????""" ???????return self.code2url[shortUrl[-6:]] ???????# Your Codec object will be instantiated and called as such:# codec = Codec()# codec.decode(codec.encode(url))

  

Time SubmittedStatusRuntimeLanguage
a few seconds agoAccepted28 mspython
2 minutes agoAccepted36 mspython
13 minutes agoAccepted8 mscpp

535. Encode and Decode TinyURL(rand and srand)

原文地址:https://www.cnblogs.com/ruruozhenhao/p/9982455.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved