[Swift]LeetCode271. 加碼解碼字符串 $ Encode and Decode Strings

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-rcxtnvjw-kz.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings.git

Machine 1 (sender) has the function:github

string encode(vector<string> strs) {
  // ... your code
  return encoded_string;
}

Machine 2 (receiver) has the function:算法

vector<string> decode(string s) {
  //... your code
  return strs;
}

So Machine 1 does:微信

string encoded_string = encode(strs);

and Machine 2 does:網絡

vector<string> strs2 = decode(encoded_string);

strs2 in Machine 2 should be the same as strs in Machine 1.app

Implement the encode and decode methods.less

Note:編碼

  • The string may contain any possible characters out of 256 valid ascii characters. Your algorithm should be generalized enough to work on any possible characters.
  • Do not use class member/global/static variables to store states. Your encode and decode algorithms should be stateless.
  • Do not rely on any library method such as eval or serialize methods. You should implement your own encode/decode algorithm.

設計將字符串列表編碼爲字符串的算法。編碼後的字符串經過網絡發送,並被解碼回原始的字符串列表。spa

機器1(發送器)具備如下功能:

string encode(vector<string> strs) {
  // ... your code
  return encoded_string;
}

機器2(接收器)具備如下功能:

vector<string> decode(string s) {
  //... your code
  return strs;
}

因此機器1:

string encoded_string = encode(strs);


機器2:

vector<string> strs2 = decode(encoded_string);

機器2中的strs2應與機器1中的strs相同。

實現編碼和解碼方法。

注:

  • 字符串能夠包含256個有效ASCII字符中的任何可能字符。您的算法應該足夠通用,能夠處理任何可能的字符。
  • 不要使用類成員/全局/靜態變量來存儲狀態。您的編碼和解碼算法應該是無狀態的。
  • 不要依賴任何庫方法,如eval或serialize方法。您應該實現本身的編碼/解碼算法。

Solution

 1 class Codec
 2 {
 3     func encode(_ strs:inout [String]) -> String
 4     {
 5         var res:String = String()
 6         for a in strs
 7         {
 8             res += (String(a.count) + "/" + a)
 9         }
10         return res
11     }
12     
13     func decode(_ s:String) -> [String]
14     {
15         var s = s
16         var res:[String] = [String]()
17         while(!s.isEmpty)
18         {
19             var found:Int = s.find("/")
20             var len:Int = Int(s.subString(0, found)) ?? 0
21             s = s.subString(found + 1)
22             res.append(s.subString(0, len))
23             s = s.subString(len)
24         }
25         return res
26     }
27 }
28 
29 extension String {
30     
31     func find(_ char:Character) -> Int
32     {
33         var arr:[Character] = Array(self)
34         for i in 0..<arr.count
35         {
36             if arr[i] == char
37             {
38                 return i
39             }
40         }
41         return -1
42     }
43     
44     // 截取字符串:從index到結束處
45     // - Parameter index: 開始索引
46     // - Returns: 子字符串
47     func subString(_ index: Int) -> String {
48         let theIndex = self.index(self.endIndex, offsetBy: index - self.count)
49         return String(self[theIndex..<endIndex])
50     }
51     
52     // 截取字符串:指定索引和字符數
53     // - begin: 開始截取處索引
54     // - count: 截取的字符數量
55     func subString(_ begin:Int,_ count:Int) -> String {
56         let start = self.index(self.startIndex, offsetBy: max(0, begin))
57         let end = self.index(self.startIndex, offsetBy:  min(self.count, begin + count))
58         return String(self[start..<end]) 
59     }
60 }
相關文章
相關標籤/搜索