GeezerVison is showing github.com — feat: add solutions to lc problem: No.0893 · rabbitqyh/leetcode@8aa44a4
File treeExpand file treeCollapse file tree Open diff view settings basic/sorting/InsertionSort 0000-0099/0054.Spiral Matrix 0300-0399/0361.Bomb Enemy 0800-0899/0893.Groups of Special-Equivalent Strings
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: basic/sorting/InsertionSort/InsertionSort.py +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,9 +2,13 @@ def insertion_sort(array): | 2 | 2 | for i in range(len(array)): | 3 | 3 | cur_index = i | 4 | 4 | while array[cur_index - 1] > array[cur_index] and cur_index - 1 >= 0: | 5 | | - array[cur_index], array[cur_index - 1] = array[cur_index - 1], array[cur_index] | | 5 | + array[cur_index], array[cur_index - 1] = ( | | 6 | + array[cur_index - 1], | | 7 | + array[cur_index], | | 8 | + ) | 6 | 9 | cur_index -= 1 | 7 | 10 | return array | 8 | 11 |
| | 12 | + | 9 | 13 | array = [10, 17, 50, 7, 30, 24, 27, 45, 15, 5, 36, 21] | 10 | 14 | print(insertion_sort(array)) |
Collapse file Copy file name to clipboardExpand all lines: solution/0000-0099/0054.Spiral Matrix/Solution.py +2-4Lines changed: 2 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,9 +7,7 @@ def spiralOrder(self, matrix: List[List[int]]) -> List[int]: | 7 | 7 | ans.extend([matrix[top][j] for j in range(left, right + 1)]) | 8 | 8 | ans.extend([matrix[i][right] for i in range(top + 1, bottom + 1)]) | 9 | 9 | if left < right and top < bottom: | 10 | | - ans.extend([matrix[bottom][j] | 11 | | - for j in range(right - 1, left - 1, -1)]) | 12 | | - ans.extend([matrix[i][left] | 13 | | - for i in range(bottom - 1, top, -1)]) | | 10 | + ans.extend([matrix[bottom][j] for j in range(right - 1, left - 1, -1)]) | | 11 | + ans.extend([matrix[i][left] for i in range(bottom - 1, top, -1)]) | 14 | 12 | top, bottom, left, right = top + 1, bottom - 1, left + 1, right - 1 | 15 | 13 | return ans |
Collapse file Copy file name to clipboardExpand all lines: solution/0300-0399/0361.Bomb Enemy/Solution.py +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,4 +32,7 @@ def maxKilledEnemies(self, grid: List[List[str]]) -> int: | 32 | 32 | elif grid[i][j] == 'E': | 33 | 33 | t += 1 | 34 | 34 | g[i][j] += t | 35 | | - return max([g[i][j] for i in range(m) for j in range(n) if grid[i][j] == '0'], default=0) | | 35 | + return max( | | 36 | + [g[i][j] for i in range(m) for j in range(n) if grid[i][j] == '0'], | | 37 | + default=0, | | 38 | + ) |
Collapse file Copy file name to clipboardExpand all lines: solution/0800-0899/0893.Groups of Special-Equivalent Strings/README.md +85-1Lines changed: 85 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 70 | 70 | <!-- 这里可写当前语言的特殊实现逻辑 --> | 71 | 71 |
| 72 | 72 | ```python | 73 | | - | | 73 | +class Solution: | | 74 | + def numSpecialEquivGroups(self, words: List[str]) -> int: | | 75 | + s = {''.join(sorted(word[::2]) + sorted(word[1::2])) for word in words} | | 76 | + return len(s) | 74 | 77 | ``` | 75 | 78 |
| 76 | 79 | ### **Java** | 77 | 80 |
| 78 | 81 | <!-- 这里可写当前语言的特殊实现逻辑 --> | 79 | 82 |
| 80 | 83 | ```java | | 84 | +class Solution { | | 85 | + public int numSpecialEquivGroups(String[] words) { | | 86 | + Set<String> s = new HashSet<>(); | | 87 | + for (String word : words) { | | 88 | + s.add(convert(word)); | | 89 | + } | | 90 | + return s.size(); | | 91 | + } | | 92 | + | | 93 | + private String convert(String word) { | | 94 | + List<Character> a = new ArrayList<>(); | | 95 | + List<Character> b = new ArrayList<>(); | | 96 | + for (int i = 0; i < word.length(); ++i) { | | 97 | + char ch = word.charAt(i); | | 98 | + if (i % 2 == 0) { | | 99 | + a.add(ch); | | 100 | + } else { | | 101 | + b.add(ch); | | 102 | + } | | 103 | + } | | 104 | + Collections.sort(a); | | 105 | + Collections.sort(b); | | 106 | + StringBuilder sb = new StringBuilder(); | | 107 | + for (char c : a) { | | 108 | + sb.append(c); | | 109 | + } | | 110 | + for (char c : b) { | | 111 | + sb.append(c); | | 112 | + } | | 113 | + return sb.toString(); | | 114 | + } | | 115 | +} | | 116 | +``` | | 117 | + | | 118 | +### **C++** | | 119 | + | | 120 | +```cpp | | 121 | +class Solution { | | 122 | +public: | | 123 | + int numSpecialEquivGroups(vector<string>& words) { | | 124 | + unordered_set<string> s; | | 125 | + for (auto& word : words) | | 126 | + { | | 127 | + string a = "", b = ""; | | 128 | + for (int i = 0; i < word.size(); ++i) | | 129 | + { | | 130 | + if (i & 1) a += word[i]; | | 131 | + else b += word[i]; | | 132 | + } | | 133 | + sort(a.begin(), a.end()); | | 134 | + sort(b.begin(), b.end()); | | 135 | + s.insert(a + b); | | 136 | + } | | 137 | + return s.size(); | | 138 | + } | | 139 | +}; | | 140 | +``` | 81 | 141 |
| | 142 | +### **Go** | | 143 | +
| | 144 | +```go | | 145 | +func numSpecialEquivGroups(words []string) int { | | 146 | + s := map[string]bool{} | | 147 | + for _, word := range words { | | 148 | + a, b := []rune{}, []rune{} | | 149 | + for i, c := range word { | | 150 | + if i&1 == 1 { | | 151 | + a = append(a, c) | | 152 | + } else { | | 153 | + b = append(b, c) | | 154 | + } | | 155 | + } | | 156 | + sort.Slice(a, func(i, j int) bool { | | 157 | + return a[i] < a[j] | | 158 | + }) | | 159 | + sort.Slice(b, func(i, j int) bool { | | 160 | + return b[i] < b[j] | | 161 | + }) | | 162 | + s[string(a)+string(b)] = true | | 163 | + } | | 164 | + return len(s) | | 165 | +} | 82 | 166 | ``` | 83 | 167 |
| 84 | 168 | ### **...** | |
Collapse file Copy file name to clipboardExpand all lines: solution/0800-0899/0893.Groups of Special-Equivalent Strings/README_EN.md +85-1Lines changed: 85 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -59,13 +59,97 @@ Note that in particular, "zzxy" is not special equivalent to "zzy | 59 | 59 | ### **Python3** | 60 | 60 |
| 61 | 61 | ```python | 62 | | - | | 62 | +class Solution: | | 63 | + def numSpecialEquivGroups(self, words: List[str]) -> int: | | 64 | + s = {''.join(sorted(word[::2]) + sorted(word[1::2])) for word in words} | | 65 | + return len(s) | 63 | 66 | ``` | 64 | 67 |
| 65 | 68 | ### **Java** | 66 | 69 |
| 67 | 70 | ```java | | 71 | +class Solution { | | 72 | + public int numSpecialEquivGroups(String[] words) { | | 73 | + Set<String> s = new HashSet<>(); | | 74 | + for (String word : words) { | | 75 | + s.add(convert(word)); | | 76 | + } | | 77 | + return s.size(); | | 78 | + } | | 79 | + | | 80 | + private String convert(String word) { | | 81 | + List<Character> a = new ArrayList<>(); | | 82 | + List<Character> b = new ArrayList<>(); | | 83 | + for (int i = 0; i < word.length(); ++i) { | | 84 | + char ch = word.charAt(i); | | 85 | + if (i % 2 == 0) { | | 86 | + a.add(ch); | | 87 | + } else { | | 88 | + b.add(ch); | | 89 | + } | | 90 | + } | | 91 | + Collections.sort(a); | | 92 | + Collections.sort(b); | | 93 | + StringBuilder sb = new StringBuilder(); | | 94 | + for (char c : a) { | | 95 | + sb.append(c); | | 96 | + } | | 97 | + for (char c : b) { | | 98 | + sb.append(c); | | 99 | + } | | 100 | + return sb.toString(); | | 101 | + } | | 102 | +} | | 103 | +``` | | 104 | + | | 105 | +### **C++** | | 106 | + | | 107 | +```cpp | | 108 | +class Solution { | | 109 | +public: | | 110 | + int numSpecialEquivGroups(vector<string>& words) { | | 111 | + unordered_set<string> s; | | 112 | + for (auto& word : words) | | 113 | + { | | 114 | + string a = "", b = ""; | | 115 | + for (int i = 0; i < word.size(); ++i) | | 116 | + { | | 117 | + if (i & 1) a += word[i]; | | 118 | + else b += word[i]; | | 119 | + } | | 120 | + sort(a.begin(), a.end()); | | 121 | + sort(b.begin(), b.end()); | | 122 | + s.insert(a + b); | | 123 | + } | | 124 | + return s.size(); | | 125 | + } | | 126 | +}; | | 127 | +``` | 68 | 128 |
| | 129 | +### **Go** | | 130 | +
| | 131 | +```go | | 132 | +func numSpecialEquivGroups(words []string) int { | | 133 | + s := map[string]bool{} | | 134 | + for _, word := range words { | | 135 | + a, b := []rune{}, []rune{} | | 136 | + for i, c := range word { | | 137 | + if i&1 == 1 { | | 138 | + a = append(a, c) | | 139 | + } else { | | 140 | + b = append(b, c) | | 141 | + } | | 142 | + } | | 143 | + sort.Slice(a, func(i, j int) bool { | | 144 | + return a[i] < a[j] | | 145 | + }) | | 146 | + sort.Slice(b, func(i, j int) bool { | | 147 | + return b[i] < b[j] | | 148 | + }) | | 149 | + s[string(a)+string(b)] = true | | 150 | + } | | 151 | + return len(s) | | 152 | +} | 69 | 153 | ``` | 70 | 154 |
| 71 | 155 | ### **...** | |
Collapse file Copy file name to clipboard +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +class Solution { | | 2 | +public: | | 3 | + int numSpecialEquivGroups(vector<string>& words) { | | 4 | + unordered_set<string> s; | | 5 | + for (auto& word : words) | | 6 | + { | | 7 | + string a = "", b = ""; | | 8 | + for (int i = 0; i < word.size(); ++i) | | 9 | + { | | 10 | + if (i & 1) a += word[i]; | | 11 | + else b += word[i]; | | 12 | + } | | 13 | + sort(a.begin(), a.end()); | | 14 | + sort(b.begin(), b.end()); | | 15 | + s.insert(a + b); | | 16 | + } | | 17 | + return s.size(); | | 18 | + } | | 19 | +}; |
Collapse file Copy file name to clipboard +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +func numSpecialEquivGroups(words []string) int { | | 2 | + s := map[string]bool{} | | 3 | + for _, word := range words { | | 4 | + a, b := []rune{}, []rune{} | | 5 | + for i, c := range word { | | 6 | + if i&1 == 1 { | | 7 | + a = append(a, c) | | 8 | + } else { | | 9 | + b = append(b, c) | | 10 | + } | | 11 | + } | | 12 | + sort.Slice(a, func(i, j int) bool { | | 13 | + return a[i] < a[j] | | 14 | + }) | | 15 | + sort.Slice(b, func(i, j int) bool { | | 16 | + return b[i] < b[j] | | 17 | + }) | | 18 | + s[string(a)+string(b)] = true | | 19 | + } | | 20 | + return len(s) | | 21 | +} |
Collapse file Copy file name to clipboard +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +class Solution { | | 2 | + public int numSpecialEquivGroups(String[] words) { | | 3 | + Set<String> s = new HashSet<>(); | | 4 | + for (String word : words) { | | 5 | + s.add(convert(word)); | | 6 | + } | | 7 | + return s.size(); | | 8 | + } | | 9 | + | | 10 | + private String convert(String word) { | | 11 | + List<Character> a = new ArrayList<>(); | | 12 | + List<Character> b = new ArrayList<>(); | | 13 | + for (int i = 0; i < word.length(); ++i) { | | 14 | + char ch = word.charAt(i); | | 15 | + if (i % 2 == 0) { | | 16 | + a.add(ch); | | 17 | + } else { | | 18 | + b.add(ch); | | 19 | + } | | 20 | + } | | 21 | + Collections.sort(a); | | 22 | + Collections.sort(b); | | 23 | + StringBuilder sb = new StringBuilder(); | | 24 | + for (char c : a) { | | 25 | + sb.append(c); | | 26 | + } | | 27 | + for (char c : b) { | | 28 | + sb.append(c); | | 29 | + } | | 30 | + return sb.toString(); | | 31 | + } | | 32 | +} |
Collapse file Copy file name to clipboard +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +class Solution: | | 2 | + def numSpecialEquivGroups(self, words: List[str]) -> int: | | 3 | + s = {''.join(sorted(word[::2]) + sorted(word[1::2])) for word in words} | | 4 | + return len(s) |
|