每封电子邮件都由一个本地名称和一个域名组成,以 @ 符号分隔。
例如,在 [email protected]
中, alice
是本地名称,而 leetcode.com
是域名。
除了小写字母,这些电子邮件还可能包含 ','
或 '+'
。
如果在电子邮件地址的本地名称部分中的某些字符之间添加句点('.'
),则发往那里的邮件将会转发到本地名称中没有点的同一地址。例如,"[email protected]”
和 “[email protected]”
会转发到同一电子邮件地址。 (请注意,此规则不适用于域名。)
如果在本地名称中添加加号('+'
),则会忽略第一个加号后面的所有内容。这允许过滤某些电子邮件,例如 [email protected]
将转发到 [email protected]
。 (同样,此规则不适用于域名。)
可以同时使用这两个规则。
给定电子邮件列表 emails
,我们会向列表中的每个地址发送一封电子邮件。实际收到邮件的不同地址有多少?
示例:
输入:["[email protected]","[email protected]","[email protected]"] 输出:2 解释:实际收到邮件的是 "[email protected]" 和 "[email protected]"。
提示:
扫描二维码关注公众号,回复:
3860207 查看本文章
1 <= emails[i].length <= 100
1 <= emails.length <= 100
- 每封
emails[i]
都包含有且仅有一个'@'
字符。
利用set来排重,加上一些字符串的处理,个人实现上用了flag来处理遇到‘+’后面的情况
class Solution {
public int numUniqueEmails(String[] emails) {
HashSet<String> set = new HashSet<>();
for(int i=0;i<emails.length;i++){
char[] ch = new char[emails[i].length()];
int t=0,k=0;
boolean flag = true;
for(int j=0;j<emails[i].length();j++){
if(emails[i].charAt(j)=='+')
flag = false;
if(emails[i].charAt(j)=='@'){
k=j;
break;
}
if(emails[i].charAt(j)!='.' && flag)
ch[t++]=emails[i].charAt(j);
}
for(int j=k;j<emails[i].length();j++)
ch[t++]=emails[i].charAt(j);
char[] ans = new char[t];
for(int j=0;j<t;j++)
ans[j] = ch[j];//这个比较重要,ch的内存是开大了,直接转string后面会有空格?
set.add(new String(ans));
}
return set.size();
}
}
贴一些优质代码,Beat率前3的范例
class Solution {
public int numUniqueEmails(String[] emails) {
HashSet<String> hashSet = new HashSet<>();
for (int i = 0; i < emails.length; i++) {
String s1, s2;
StringBuilder builder = new StringBuilder();
int length = emails[i].length();
while (--length >= 0 && emails[i].charAt(length) != '@')
;
s1 = emails[i].substring(length);
for (int j = 0; j < length && emails[i].charAt(j) != '+'; j++) {
if (emails[i].charAt(j) != '.') {
builder.append(emails[i].charAt(j));
}
}
s2 = builder.toString();
hashSet.add(s1 + s2);
}
return hashSet.size();
}
}
class Solution {
public int numUniqueEmails(String[] emails) {
Map<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < emails.length; i++) {
String str = emails[i];
int idx = str.indexOf('@');
String pre = str.substring(0, idx);
int plus_idx = pre.indexOf('+');
if (plus_idx != -1) {
pre = pre.substring(0, plus_idx);
}
StringBuffer sb = new StringBuffer();
for (int j = 0; j < pre.length(); j++) {
char ch = pre.charAt(j);
if (ch == '.') {
continue;
}
sb.append(ch);
}
String final_str = sb.toString() + str.substring(idx+1);
if (map.containsKey(final_str) == false) {
map.put(final_str, new Integer(1));
}
}
return map.size();
}
}
class Solution {
public int numUniqueEmails(String[] emails) {
Map<String, Set<String>> eh = new HashMap<>();
for(String email : emails){
String[] ab = getAB(email);
String a = ab[0];
String b = ab[1];
Set<String> strings = null;
if (eh.containsKey(b)){
strings = eh.get(b);
}else {
strings = new HashSet<>();
}
strings.add(a);
eh.put(b, strings);
}
Set<Map.Entry<String, Set<String>>> entries = eh.entrySet();
int sum = 0;
for(Map.Entry<String, Set<String>> entry : entries){
sum += entry.getValue().size();
}
return sum;
}
public String[] getAB(String email){
StringBuilder a = new StringBuilder("");
StringBuilder b = new StringBuilder("");
int j = email.length();
int k = 0;
for(int i = 0; i < email.length(); i++){
if(email.charAt(i) == '+'){
j = i;
}
if (email.charAt(i) == '@'){
k = i;
}
if(email.charAt(i) != '.' && j > i){
a.append(email.charAt(i));
}
if(k != 0){
b.append(email.charAt(i));
}
}
return new String[]{a.toString(), b.toString()};
}
}