作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/encode-and-decode-tinyurl/description/

题目描述

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.

解题方法

方法一:数组

这个题是个佛性题。不给任何要求,只要能编码、解码出来就行。用了大神的思路,直接用数组进行保存,然后把每个网址所在数组中的编号作为短网址进行编码和解码。

class Codec:

    def __init__(self):
self.urls = [] def encode(self, longUrl):
"""Encodes a URL to a shortened URL. :type longUrl: str
:rtype: str
"""
self.urls.append(longUrl)
return "http://tinyurl.com/" + str(len(self.urls) - 1) def decode(self, shortUrl):
"""Decodes a shortened URL to its original URL. :type shortUrl: str
:rtype: str
"""
return self.urls[int(shortUrl.split('/')[-1])] # Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.decode(codec.encode(url))

方法二:字典

道理是一样的,其实字典更简单一点。

class Codec:
def __init__(self):
self.count = 0
self.d = dict() def encode(self, longUrl):
"""Encodes a URL to a shortened URL. :type longUrl: str
:rtype: str
"""
self.count += 1
self.d[self.count] = longUrl
return str(self.count) def decode(self, shortUrl):
"""Decodes a shortened URL to its original URL. :type shortUrl: str
:rtype: str
"""
return self.d[int(shortUrl)] # Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.decode(codec.encode(url))

C++版本的代码如下:

class Solution {
public: // Encodes a URL to a shortened URL.
string encode(string longUrl) {
count++;
d[count] = longUrl;
return to_string(count);
} // Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
return d[stoi(shortUrl)];
}
private:
int count = 0;
map<int, string> d;
}; // Your Solution object will be instantiated and called as such:
// Solution solution;
// solution.decode(solution.encode(url));

日期

2018 年 2 月 5 日
2018 年 12 月 2 日 —— 又到了周日

【LeetCode】535. Encode and Decode TinyURL 解题报告(Python & C++)的更多相关文章

  1. [LeetCode] 535. Encode and Decode TinyURL 编码和解码短网址

    Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...

  2. 535. Encode and Decode TinyURL - LeetCode

    Question 535. Encode and Decode TinyURL Solution 题目大意:实现长链接加密成短链接,短链接解密成长链接 思路:加密成短链接+key,将长链接按key保存 ...

  3. LC 535. Encode and Decode TinyURL

    Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...

  4. 【Leetcode】535. Encode and Decode TinyURL

    Question: TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/pro ...

  5. 535. Encode and Decode TinyURL

    ▶ 要求给出一种对 URL 网址进行压缩和解压的算法,例如 https://leetcode.com/problems/design-tinyurl ←→ http://tinyurl.com/4e9 ...

  6. 535. Encode and Decode TinyURL 长短URL

    [抄题]: TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problem ...

  7. 535. Encode and Decode TinyURL(rand and srand)

    Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...

  8. 535 Encode and Decode TinyURL 编码和解码精简URL地址

    详见:https://leetcode.com/problems/encode-and-decode-tinyurl/description/ C++: class Solution { public ...

  9. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...

随机推荐

  1. Metabolomics Workfolw

    推荐R语言界的国内大佬于淼写的代谢组学workflow,包含了大部分代谢组学(以及暴露组)的数据分析方法. Meta-Workflow 主要内容包括: Sample collection Pretre ...

  2. 在Kubernetes上安装MySQL-PXC集群

    官方部署文档地址:https://www.percona.com/doc/kubernetes-operator-for-pxc/kubernetes.html 一.部署方式 示例在k8s集群(至少3 ...

  3. 【leetcode】598. Range Addition II

    You are given an m x n matrix M initialized with all 0's and an array of operations ops, where ops[i ...

  4. Android Https相关完全解析

    转载: 转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/48129405: 本文出自:[张鸿洋的博客] 一.概述 其实这篇文章理论 ...

  5. Spring整合Ibatis之SqlMapClientDaoSupport

    前言 HibernateDaoSupport   SqlMapClientDaoSupport . 其实就作用而言两者是一样的,都是为提供DAO支持,为访问数据库提供支持. 只不过HibernateD ...

  6. mysql index 8.0

    创建表 use vodb; drop table if exists test1; create table test1(id int NOT NULL AUTO_INCREMENT primary ...

  7. LVS nat模型+dr模型

    nat模型 在 rs1 和 rs2  安装httpd  并配置测试页,启动 [root@rs1 ~]# yum install httpd -y[root@rs1 ~]# echo "thi ...

  8. 【Java多线程】CompletionService

    什么是CompletionService? 当我们使用ExecutorService启动多个Callable时,每个Callable返回一个Future,而当我们执行Future的get方法获取结果时 ...

  9. 使用ajax对用户注册时,用户名进行检验

    package cn.hopetesting.com.servlet;import com.fasterxml.jackson.databind.ObjectMapper;import javax.p ...

  10. 【Jenkins系列】-备份机制

    Jenkins是主从模式,从节点可以做集群.负载,从而实现从节点的高可用,但是主节点是单节点,一旦主节点宕机,会导致Jenkins服务不可用.Jenkins主节点本身是不支持集群的,需要通过其他变通方 ...