|
38 | 38 |
|
39 | 39 | <!-- 这里可写通用的实现逻辑 -->
|
40 | 40 |
|
| 41 | +**方法一:递归 + 排序** |
| 42 | + |
| 43 | +我们可以把特殊的二进制序列看作“有效的括号”,$1$ 代表左括号,$0$ 代表右括号。例如,"11011000" 可以看作:"(()(()))"。 |
| 44 | + |
| 45 | +交换两个连续非空的特殊子串,相当于交换两个相邻的有效括号,我们可以使用递归来解决这个问题。 |
| 46 | + |
| 47 | +我们将字符串 $s$ 中的每个“有效的括号”都看作一部分,递归处理,最后进行排序,得到最终答案。 |
| 48 | + |
| 49 | +时间复杂度 $O(n^2)$。 |
| 50 | + |
41 | 51 | <!-- tabs:start -->
|
42 | 52 |
|
43 | 53 | ### **Python3**
|
44 | 54 |
|
45 | 55 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
46 | 56 |
|
47 | 57 | ```python
|
48 |
| - |
| 58 | +class Solution: |
| 59 | + def makeLargestSpecial(self, s: str) -> str: |
| 60 | + if s == '': |
| 61 | + return '' |
| 62 | + ans = [] |
| 63 | + cnt = 0 |
| 64 | + i = j = 0 |
| 65 | + while i < len(s): |
| 66 | + cnt += 1 if s[i] == '1' else -1 |
| 67 | + if cnt == 0: |
| 68 | + ans.append('1' + self.makeLargestSpecial(s[j + 1: i]) + '0') |
| 69 | + j = i + 1 |
| 70 | + i += 1 |
| 71 | + ans.sort(reverse=True) |
| 72 | + return ''.join(ans) |
49 | 73 | ```
|
50 | 74 |
|
51 | 75 | ### **Java**
|
52 | 76 |
|
53 | 77 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
54 | 78 |
|
55 | 79 | ```java
|
| 80 | +class Solution { |
| 81 | + public String makeLargestSpecial(String s) { |
| 82 | + if ("".equals(s)) { |
| 83 | + return ""; |
| 84 | + } |
| 85 | + List<String> ans = new ArrayList<>(); |
| 86 | + int cnt = 0; |
| 87 | + for (int i = 0, j = 0; i < s.length(); ++i) { |
| 88 | + cnt += s.charAt(i) == '1' ? 1 : -1; |
| 89 | + if (cnt == 0) { |
| 90 | + String t = "1" + makeLargestSpecial(s.substring(j + 1, i)) + "0"; |
| 91 | + ans.add(t); |
| 92 | + j = i + 1; |
| 93 | + } |
| 94 | + } |
| 95 | + ans.sort(Comparator.reverseOrder()); |
| 96 | + return String.join("", ans); |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +### **C++** |
| 102 | + |
| 103 | +```cpp |
| 104 | +class Solution { |
| 105 | +public: |
| 106 | + string makeLargestSpecial(string s) { |
| 107 | + if (s == "") return s; |
| 108 | + vector<string> ans; |
| 109 | + int cnt = 0; |
| 110 | + for (int i = 0, j = 0; i < s.size(); ++i) |
| 111 | + { |
| 112 | + cnt += s[i] == '1' ? 1 : -1; |
| 113 | + if (cnt == 0) |
| 114 | + { |
| 115 | + ans.push_back("1" + makeLargestSpecial(s.substr(j + 1, i - j - 1)) + "0"); |
| 116 | + j = i + 1; |
| 117 | + } |
| 118 | + } |
| 119 | + sort(ans.begin(), ans.end(), greater<string>{}); |
| 120 | + return accumulate(ans.begin(), ans.end(), ""s); |
| 121 | + } |
| 122 | +}; |
| 123 | +``` |
56 | 124 |
|
| 125 | +### **Go** |
| 126 | +
|
| 127 | +```go |
| 128 | +func makeLargestSpecial(s string) string { |
| 129 | + if s == "" { |
| 130 | + return "" |
| 131 | + } |
| 132 | + ans := sort.StringSlice{} |
| 133 | + cnt := 0 |
| 134 | + for i, j := 0, 0; i < len(s); i++ { |
| 135 | + if s[i] == '1' { |
| 136 | + cnt++ |
| 137 | + } else { |
| 138 | + cnt-- |
| 139 | + } |
| 140 | + if cnt == 0 { |
| 141 | + ans = append(ans, "1"+makeLargestSpecial(s[j+1:i])+"0") |
| 142 | + j = i + 1 |
| 143 | + } |
| 144 | + } |
| 145 | + sort.Sort(sort.Reverse(ans)) |
| 146 | + return strings.Join(ans, "") |
| 147 | +} |
57 | 148 | ```
|
58 | 149 |
|
59 | 150 | ### **...**
|
|
0 commit comments