LeetCode 929. 【Java】929. Unique Email Addresses
原题链接
简单
作者:
tt2767
,
2020-02-11 10:01:16
,
所有人可见
,
阅读 661
/*
1. 首先处理local name 为最终的情况,然后和domain一起加到set里即可
2. testCase: null, 空
local正常, local 带. , local 带+, local 带.和+ , 空
domain正常, domain 带. , domain 带+, domain 带.和+, 空
*/
class Solution {
StringBuilder buffer = new StringBuilder();
public int numUniqueEmails(String[] emails) {
Set<String> set = new HashSet<>();
for (int i = 0 ; i < emails.length ; i++){
String realAddress = getRealAddress(emails[i]);
set.add(realAddress);
}
return set.size();
}
public String getRealAddress(String email){
buffer.setLength(0);
boolean ignore = false;
int i = 0;
for ( ; i < email.length(); i++){
if (email.charAt(i) == '.') continue;
if (email.charAt(i) == '+') ignore = true;
if (email.charAt(i) == '@') break;
if (ignore) continue;
buffer.append(email.charAt(i));
}
for ( ; i < email.length(); i++){
buffer.append(email.charAt(i));
}
return buffer.toString();
}
}