【Lintcode】python - [659] Encode and Decode Strings 個人解法筆記

題目出處

659 · Encode and Decode Strings

難度

medium

個人範例程式碼

class Solution:
    """
    @param: strs: a list of strings
    @return: encodes a list of strings to a single string.
    """
    def encode(self, strs):
        # write your code here
        return " ".join(strs)

    """
    @param: str: A string
    @return: dcodes a single string to a list of strings
    """
    def decode(self, str):
        # write your code here
        return str.split(" ")

算法說明

這題是應用的 design 類問題,
因為我們在傳輸數據時,大部分都沒有辦法直接傳遞「我們定義好的資料結構」,
通常我們都會先進行 encoding 把資料加密,例如把資料轉成文字化。

等待送達目的後,再對資料進行 decoding 解密,把文字化資料轉回「我們要的資料結構」,
而這題概念上大概就是要我們實作這樣的概念,方法有無限多種,
但以考題來說,這題應該是理解概念就好。

input handling

x (design 類問題)

Boundary conditions

x (design 類問題)

Reference