版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hsx1612727380/article/details/85240357
Title:Unique Email Addresses 929
Difficulty:Easy
原题leetcode地址: https://leetcode.com/problems/unique-email-addresses/
1. Set集合的唯一性
时间复杂度:O(n),一次一层for循环。
空间复杂度:O(n),申请Set集合。
/**
* Set集合的唯一性(注意的方法:indexOf()、contains()、replaceAll())
* @param emails
* @return
*/
public static int numUniqueEmails(String[] emails) {
Set<String> set = new HashSet<>();
for (String email : emails) {
int atIndex = email.indexOf('@');
String name = email.substring(0, atIndex);
String domain = email.substring(atIndex);
if (name.contains("+")) {
name = name.substring(0, name.indexOf('+'));
}
name = name.replaceAll(".", "");
set.add(name + domain);
}
return set.size();
}