Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 37cc227

Browse files
committed
🐳(array): 49. 字母异位词分组
补充字符串序列化写法
1 parent 834f9d8 commit 37cc227

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

docs/data-structure/array/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,24 @@ class Solution(object):
816816
return ans.values()
817817
```
818818

819+
普通写法,序列化为字符串:
820+
821+
```python
822+
class Solution:
823+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
824+
table = dict()
825+
for string in strs:
826+
sort_string = "".join(sorted(string))
827+
if sort_string not in table:
828+
table[sort_string] = []
829+
table[sort_string].append(string)
830+
ans = [x for x in table.values()]
831+
return ans
832+
```
833+
834+
- 时间复杂度:O(n*klogk)(`n``strs` 长度,`k``strs` 中字符串的最大长度)
835+
- 空间复杂度:O(n*k),存储在 `ans` 中的所有信息内容
836+
819837
### 参考资料
820838

821839
- [Python中collections.defaultdict()使用](https://www.jianshu.com/p/26df28b3bfc8)

0 commit comments

Comments
 (0)