TinyURL 문제 풀이 보고서 인 코딩 및 디 코딩 (Python & C + +)

9166 단어 LeetCode알고리즘
저자: 음 설 명 초 id: fuxuemingzhu 개인 블 로그:http://fuxuemingzhu.cn/
목차
  • 제목 설명
  • 문제 풀이 방법
  • 방법 1: 배열
  • 방법 2: 사전
  • 날짜
  • 제목 주소: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.
    문제 풀이 방법
    방법 1: 배열
    이 문 제 는 불 성 문제 다.아무런 요구 도 하지 않 고 인 코딩, 디 코딩 만 할 수 있 으 면 됩 니 다.큰 신의 생각 으로 배열 로 저장 한 다음 에 모든 사이트 가 있 는 배열 의 번 호 를 짧 은 사이트 주소 로 인 코딩 하고 디 코딩 합 니 다.
    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))
    

    방법 2: 사전
    이 치 는 같 지만 사실 사전 은 좀 더 간단 하 다.
    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 일 - 또 일요일

    좋은 웹페이지 즐겨찾기