LeetCode From Easy To Hard No10[Easy]: Unique Email Addresses 邮件地址的唯一性

本文探讨了如何处理和标准化电子邮件地址,特别是针对那些包含特殊字符如'.'和'+'的情况。通过实例演示,我们了解到在'@'符号左侧的部分,'.'会被忽略,而'+'之后的内容将被省略,这有助于减少邮件系统的复杂性并提高过滤效率。文章最后提供了一个Java代码示例,展示了如何使用HashSet来去除重复的电子邮件地址。

very email consists of a local name and a domain name, separated by the @ sign.

For example, in alice@leetcode.comalice is the local name, and leetcode.com is the domain name.

Besides lowercase letters, these emails may contain '.'s or '+'s.

If you add periods ('.') between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name.  For example, "alice.z@leetcode.com" and "alicez@leetcode.com" forward to the same email address.  (Note that this rule does not apply for domain names.)

If you add a plus ('+') in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered, for example m.y+name@email.com will be forwarded to my@email.com.  (Again, this rule does not apply for domain names.)

It is possible to use both of these rules at the same time.

Given a list of emails, we send one email to each address in the list.  How many different addresses actually receive mails? 

 

Example 1:

Input: ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
Output: 2
Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails

 

Note:

  • 1 <= emails[i].length <= 100
  • 1 <= emails.length <= 100
  • Each emails[i] contains exactly one '@' character.
  • All local and domain names are non-empty.
  • Local names do not start with a '+' character.

题目分析:给定一个email address数组, 输出有多少个不同的email adress,Email Adress 以“@”为分割点

①在"@"左边,假如出现".",则默认忽略,比如c.o.d.e@qq.com.cn其实等价于code@qq.com.cn;假如出现"+", 则"+"到"@"之间的内容则全部忽略,比如c.o.d.e+love@qq.com.c+n其实等价于code@qq.com.c+n; 

②在"@"右边,假如出现"."和"+",就不会有什么变化。

利用Set集合,里边不包含重复元素的特性进行去重:
 

import java.util.HashSet;
import java.util.Set;

class Solution {
    public static int numUniqueEmails(String[] emails) {
        Set<String> set = new HashSet<String>(); // 利用set元素
        for (String e : emails) {
            int atIndex = e.indexOf("@"); // 找到@所在的索引
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < atIndex; i++) { // 只需要循环@前边的字符
                if (e.charAt(i) == '.') continue; // 遇到“.”就直接进行下一轮循环
                if (e.charAt(i) == '+') { // 遇到“+”则退出循环
                    break;
                } else {
                    sb.append(e.charAt(i));
                }
            }
            set.add(sb.append("@").append(e.substring(atIndex)).toString()); // 拼接“@”和“@”后边的字符,然后添加进Set集合中。
        }
        return set.size();
    }

    public static void main(String[] args) {
        String[] emails = {"test.email+alex@leetcode.com", "test.email@leetcode.com"};
        System.out.println(new Solution().numUniqueEmails(emails));
    }
}

 

 

【四轴飞行器】非线性三自由度四轴飞行器模拟器研究(Matlab代码实现)内容概要:本文围绕非线性三自由度四轴飞行器模拟器的研究展开,重点介绍了基于Matlab的建模与仿真方法。通过对四轴飞行器的动力学特性进行分析,构建了非线性状态空间模型,并实现了姿态与位置的动态模拟。研究涵盖了飞行器运动方程的建立、控制系统设计及数值仿真验证等环节,突出非线性系统的精确建模与仿真优势,有助于深入理解飞行器在复杂工况下的行为特征。此外,文中还提到了多种配套技术如PID控制、状态估计与路径规划等,展示了Matlab在航空航天仿真中的综合应用能力。; 适合人群:具备一定自动控制理论基础和Matlab编程能力的高校学生、科研人员及从事无人机系统开发的工程技术人员,尤其适合研究生及以上层次的研究者。; 使用场景及目标:①用于四轴飞行器控制系统的设计与验证,支持算法快速原型开发;②作为教学工具帮助理解非线性动力学系统建模与仿真过程;③支撑科研项目中对飞行器姿态控制、轨迹跟踪等问题的深入研究; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注动力学建模与控制模块的实现细节,同时可延伸学习文档中提及的PID控制、状态估计等相关技术内容,以全面提升系统仿真与分析能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值