当前位置: 首页 > news >正文

福建网站建设有限公司线下推广方法有哪些

福建网站建设有限公司,线下推广方法有哪些,织梦与wordpress seo哪个好,广州小企业网站制作TinyURL 是一种 URL 简化服务, 比如:当你输入一个 URL https://leetcode.com/problems/design-tinyurl 时,它将返回一个简化的URL http://tinyurl.com/4e9iAk 。请你设计一个类来加密与解密 TinyURL 。 加密和解密算法如何设计和运作是没有限…

TinyURL 是一种 URL 简化服务, 比如:当你输入一个 URL https://leetcode.com/problems/design-tinyurl 时,它将返回一个简化的URL http://tinyurl.com/4e9iAk 。请你设计一个类来加密与解密 TinyURL 。

加密和解密算法如何设计和运作是没有限制的,你只需要保证一个 URL 可以被加密成一个 TinyURL ,并且这个 TinyURL 可以用解密方法恢复成原本的 URL 。

实现 Solution 类:

Solution() 初始化 TinyURL 系统对象。
String encode(String longUrl) 返回 longUrl 对应的 TinyURL 。
String decode(String shortUrl) 返回 shortUrl 原本的 URL 。题目数据保证给定的 shortUrl 是由同一个系统对象加密的。

示例:

输入:url = “https://leetcode.com/problems/design-tinyurl”
输出:“https://leetcode.com/problems/design-tinyurl”

解释:
Solution obj = new Solution();
string tiny = obj.encode(url); // 返回加密后得到的 TinyURL 。
string ans = obj.decode(tiny); // 返回解密后得到的原本的 URL 。

提示:

1 <= url.length <= 104
题目数据保证 url 是一个有效的 URL

解法一:可将长url存入数据库中,id为自增主键,每次存放后会得到数据库中一个自增长的id,然后将带有该id的url作为短url:

class Solution {
public:Solution () {id = 0;}// Encodes a URL to a shortened URL.string encode(string longUrl) {db[id] = longUrl;string shortUrl = string("http://tinyurl.com/") + to_string(id);++id;return shortUrl;}// Decodes a shortened URL to its original URL.string decode(string shortUrl) {int idStartIdx = shortUrl.rfind('/') + 1;int id = stoi(shortUrl.substr(idStartIdx, shortUrl.size() - idStartIdx));return db[id];}private:int id;unordered_map<int, string> db;
};// Your Solution object will be instantiated and called as such:
// Solution solution;
// solution.decode(solution.encode(url));

如果长url的长度为n,此算法中,encode方法的时间复杂度为O(n),空间复杂度为O(n);decode方法的时间复杂度为O(1),空间复杂度为O(1)。

解法二:哈希生成,选两个合适的质数k1_11=1117,k2_22=109^99+7,使用以下方法计算长url的哈希值:
在这里插入图片描述
然后encode函数将哈希值和长url存入数据库,并返回含有哈希值的短url。decode函数可根据短url中的哈希值从数据库中取出长url。

当发生哈希冲突时,采用线性探测再散列的方法,将key加1,直到没有冲突。相同的长url的哈希值相同,哈希冲突可能会频繁发生,为避免这一点,我们使用一个额外的哈希表记录从长url到哈希值的映射。

const long long k1 = 1117;
const long long k2 = 1e9 + 7;class Solution {
public:// Encodes a URL to a shortened URL.string encode(string longUrl) {if (urlToKey[longUrl] > 0) {return string("http://tinyurl.com/") + to_string(urlToKey[longUrl]);}int key = 0, base = 1;for (char c : longUrl) {int key = (key + c * base) % k2;int base = (base * k1) % k2;}while (db.find(key) != db.end()) {key = (key + 1) % k2;}db[key] = longUrl;return string("http://tinyurl.com/") + to_string(key);}// Decodes a shortened URL to its original URL.string decode(string shortUrl) {int idStartIdx = shortUrl.rfind('/') + 1;int id = stoi(shortUrl.substr(idStartIdx, shortUrl.size() - idStartIdx));return db[id];}private:unordered_map<int, string> db;unordered_map<string, int> urlToKey;
};// Your Solution object will be instantiated and called as such:
// Solution solution;
// solution.decode(solution.encode(url));

如果长url的长度为n,此算法中,encode方法的时间复杂度为O(n),在数据量远小于10e9+7的情况下,哈希冲突很少发生,空间复杂度为O(n);decode方法的时间复杂度为O(1),空间复杂度为O(1)。

计算字符串的哈希时,类似于计算一个数字,如1234,它等于1 * 10³ + 2 * 10² + 3 * 10 + 4 * 1

解法三:随机生成,随机生成一个key,如果key已存在,则继续生成,直到出现不存在的key:

class Solution {
public:// Encodes a URL to a shortened URL.string encode(string longUrl) {default_random_engine e(time(0));uniform_int_distribution<int> u(0, INT_MAX);int key = u(e);while (db.find(key) != db.end()) {key = u(e);}db[key] = longUrl;return string("http://tinyurl.com/") + to_string(key);}// Decodes a shortened URL to its original URL.string decode(string shortUrl) {int idStartIdx = shortUrl.rfind('/') + 1;int id = stoi(shortUrl.substr(idStartIdx, shortUrl.size() - idStartIdx));return db[id];}private:unordered_map<int, string> db;
};// Your Solution object will be instantiated and called as such:
// Solution solution;
// solution.decode(solution.encode(url));

如果长url的长度为n,此算法中,encode方法的时间复杂度为O(n),空间复杂度为O(n);decode方法的时间复杂度为O(1),空间复杂度为O(1)。

http://www.tj-hxxt.cn/news/79316.html

相关文章:

  • h5做的网站有哪些百度账号中心官网
  • wordpress 自动跳转广州网络seo公司
  • 网站更新后 需要更新 sitemap 吗百度收录是什么意思
  • 代理网络游戏唐山百度提升优化
  • 教务系统登录入口搜索引擎优化常用方法
  • 对招聘网站页面设计做建议培训心得
  • 郑州网站建设的公司哪家好新闻网站排行榜
  • 郴州网站seo网站运营是做什么的
  • 网页翻译网站武汉关键词排名提升
  • 做网站有什么关于财务的问题信息流优化师是什么
  • wordpress添加 下载东莞优化疫情防控措施
  • 新能源汽车价格走势seo技术培训泰州
  • 网站做博彩客服怎么样百度刷排名seo
  • 做窗帘网站免费seo排名优化
  • 网站制作素材seo人员的相关薪资
  • 进入 网站cmsseo外包公司排名
  • 深圳企业网站建设百度推广营销怎么做
  • 网页设计与网站开发项目泰州网站整站优化
  • 手机端网站建设备案网上竞价
  • 网站开发接口如何在百度上开店铺
  • 帮人做网站在徐州被敲诈五万免费制作网页的网站
  • 宿迁沭阳网站建设域名服务器ip查询网站
  • 什么样的公司开做网站北京网络营销外包公司哪家好
  • seo有哪些作用seo交流群
  • 潍坊企化网站建设网站维护的主要内容
  • 马鞍山网站建设开发营销策划书模板范文
  • 中小企业网站的建设实践报告seo做得比较好的企业案例
  • 做网站上海公司微信小程序开发流程
  • 做水果网站需要多钱免费网站搭建平台
  • 推荐一个可以做ppt的网站免费网站推广群发软件