61
61
62
62
<!-- 这里可写通用的实现逻辑 -->
63
63
64
+ ** 方法一:哈希表**
65
+
64
66
利用哈希表存放转换后的电子邮件,最后返回哈希表的 size 即可。
65
67
66
68
<!-- tabs:start -->
72
74
``` python
73
75
class Solution :
74
76
def numUniqueEmails (self , emails : List[str ]) -> int :
75
- ans = 0
76
77
s = set ()
77
78
for email in emails:
78
79
local, domain = email.split(' @' )
79
80
local = local.replace(' .' , ' ' )
80
- if ' +' in local :
81
- local = local[:local.find( ' + ' ) ]
81
+ if (i := local.find( ' +' )) != - 1 :
82
+ local = local[:i ]
82
83
s.add(local + ' @' + domain)
83
84
return len (s)
84
85
```
@@ -93,9 +94,8 @@ class Solution {
93
94
Set<String > s = new HashSet<> ();
94
95
for (String email : emails) {
95
96
String [] t = email. split(" @" );
96
- String local = t[0 ];
97
+ String local = t[0 ]. replace( " . " , " " ) ;
97
98
String domain = t[1 ];
98
- local = local. replace(" ." , " " );
99
99
int i = local. indexOf(' +' );
100
100
if (i != - 1 ) {
101
101
local = local. substring(0 , i);
@@ -130,6 +130,22 @@ public:
130
130
};
131
131
```
132
132
133
+ ### **Go**
134
+
135
+ ```go
136
+ func numUniqueEmails(emails []string) int {
137
+ s := map[string]bool{}
138
+ for _, email := range emails {
139
+ i := strings.IndexByte(email, '@')
140
+ local := strings.SplitN(email[:i], "+", 2)[0]
141
+ local = strings.ReplaceAll(local, ".", "")
142
+ domain := email[i:]
143
+ s[local+domain] = true
144
+ }
145
+ return len(s)
146
+ }
147
+ ```
148
+
133
149
### ** ...**
134
150
135
151
```
0 commit comments