本文主要是介绍Unique Email Address,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
思路1:面试的时候可以自己写process method
class Solution {public int numUniqueEmails(String[] emails) {if(emails == null || emails.length == 0) {return 0;}HashSet<String> set = new HashSet<String>();for(String email: emails) {String unique = parseEmail(email);set.add(unique);}return set.size();}private String parseEmail(String email) {String[] splits = email.split("@");String localname = splits[0];String domain = splits[1];return process(localname) + "@" + domain;}private String process(String localname) {StringBuilder sb = new StringBuilder();for(int i = 0; i < localname.length(); i++) {char c = localname.charAt(i);if(c == '.') {continue;} else if(c == '+'){break;} else {sb.append(c);}}return sb.toString();}
}
思路2:如果知道split("\\+");那么取第一个split[0]就可以了,然后replace(".","");
class Solution {public int numUniqueEmails(String[] emails) {if(emails == null || emails.length == 0) {return 0;}HashSet<String> set = new HashSet<String>();for(String email: emails) {String unique = parseEmail(email);set.add(unique);}return set.size();}private String parseEmail(String email) {String[] splits = email.split("@");String[] localname = splits[0].split("\\+");String domain = splits[1];return localname[0].replace(".","") + "@" + domain;}
}
这篇关于Unique Email Address的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!