[Swift]LeetCode761. 特殊的二進制序列 | Special Binary String

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

Special binary strings are binary strings with the following two properties:git

  • The number of 0's is equal to the number of 1's.
  • Every prefix of the binary string has at least as many 1's as 0's.

Given a special string S, a move consists of choosing two consecutive, non-empty, special substrings of S, and swapping them.(Two strings are consecutive if the last character of the first string is exactly one index before the first character of the second string.)github

At the end of any number of moves, what is the lexicographically largest resulting string possible?數組

Example 1:微信

Input: S = "11011000"
Output: "11100100"
Explanation:
The strings "10" [occuring at S[1]] and "1100" [at S[3]] are swapped.
This is the lexicographically largest string possible after some number of swaps.

Note:app

  1. S has length at most 50.
  2. S is guaranteed to be a special binary string as defined above.

特殊的二進制序列是具備如下兩個性質的二進制序列:函數

  • 0 的數量與 1 的數量相等。
  • 二進制序列的每個前綴碼中 1 的數量要大於等於 0 的數量。

給定一個特殊的二進制序列 S,以字符串形式表示。定義一個操做 爲首先選擇 S 的兩個連續且非空的特殊的子串,而後將它們交換。(兩個子串爲連續的當且僅當第一個子串的最後一個字符剛好爲第二個子串的第一個字符的前一個字符。)spa

在任意次數的操做以後,交換後的字符串按照字典序排列的最大的結果是什麼?code

示例 1:htm

輸入: S = "11011000"
輸出: "11100100"
解釋:
將子串 "10" (在S[1]出現) 和 "1100" (在S[3]出現)進行交換。
這是在進行若干次操做後按字典序排列最大的結果。

說明:

  1. S 的長度不超過 50
  2. S 保證爲一個知足上述定義的特殊 的二進制序列。

Runtime: 8 ms
Memory Usage: 19.9 MB
 1 class Solution {
 2     func makeLargestSpecial(_ S: String) -> String {
 3         var cnt:Int = 0
 4         var i:Int = 0
 5         var v:[String] = [String]()
 6         var res:String = String()
 7         for j in 0..<S.count
 8         {
 9             cnt += (S[j] == "1") ? 1 : -1
10             if cnt == 0
11             {
12                 v.append("1" + makeLargestSpecial(S.subString(i + 1, j - i - 1)) + "0");
13                 i = j + 1
14             }
15         }
16         v = v.sorted(by:>)
17         for i in 0..<v.count
18         {
19             res += v[i]
20         }
21         return res
22     }
23 }
24 
25 extension String {
26     //subscript函數能夠檢索數組中的值
27     //直接按照索引方式截取指定索引的字符
28     subscript (_ i: Int) -> Character {
29         //讀取字符
30         get {return self[index(startIndex, offsetBy: i)]}
31     }
32     
33     // 截取字符串:指定索引和字符數
34     // - begin: 開始截取處索引
35     // - count: 截取的字符數量
36     func subString(_ begin:Int,_ count:Int) -> String {
37         let start = self.index(self.startIndex, offsetBy: max(0, begin))
38         let end = self.index(self.startIndex, offsetBy:  min(self.count, begin + count))
39         return String(self[start..<end]) 
40     }
41     
42 }

12ms

 1 class Solution {    
 2     func makeLargestSpecial(_ S: String) -> String {
 3         if (S.count == 0) {return S}
 4         var cnt = 0, i = 0
 5         var S = Array(S)
 6         var v = [String]()
 7         var res = ""
 8         for j in 0 ..< S.count {
 9             cnt += (S[j] == "1" ? 1 : -1)
10             if cnt == 0 {
11                 if (i + 1 <= j) {
12                     let tempStr = "1" + makeLargestSpecial(String(S[i + 1 ..< j])) + "0"
13                     v.append(tempStr)
14                     i = j + 1 
15                 }
16             }
17         }    
18         v.sort{$0 > $1}
19         for i in 0 ..< v.count {
20             res += v[i]
21         }
22         return res  
23     }
24 }

16ms

 1 class Solution {
 2     func makeLargestSpecial(_ str: String) -> String {
 3         var count = 0
 4         var i = 0
 5 
 6         var strings = [String]()
 7 
 8         for j in 0..<str.count {
 9             if str[j] == "1" {
10                 count += 1
11             } else {
12                 count -= 1
13             }
14 
15             if count == 0 {
16                 strings.append("1\(makeLargestSpecial(str[i + 1, j]))0")
17                 i = j + 1
18             }
19         }
20 
21         strings.sort { $0 > $1 }
22         return strings.joined()
23     }
24 }
25 
26 extension String {
27     subscript (i: Int) -> Character {
28         return self[index(startIndex, offsetBy: i)]
29     }
30 
31     subscript (start: Int, end: Int) -> String {
32         let s = self.index(self.startIndex, offsetBy: start)
33         let e = self.index(self.startIndex, offsetBy: end)
34 
35         return String(self[s...e])
36     }
37 }
相關文章
相關標籤/搜索