929. Unique Email Addresses (Easy)#

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

For example, in alice@leetcode.com, alice 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.

solution##

我的解法,较慢

class Solution {
public int numUniqueEmails(String[] emails) {
Set<String> set = new HashSet<String>();
for (String email : emails)
{
String local;
int plus = email.indexOf('+'); //记录'+'号位置
int at = email.indexOf('@'); //记录'@'号位置
if (plus == -1) //local name 里面不存在'+'号
local = email.substring(0,at).replace(".","");
else //local name 里面存在'+'号
local = email.substring(0,plus).replace(".",""); //注意,此处一定要用空字符串""代替,折腾我半天,头疼!!!
String domain = email.substring(at);
set.add(local+domain);
}
return set.size();
}
}

官方解法,还没我的解法快!改正错误后如下

class Solution {
public int numUniqueEmails(String[] emails) {
Set<String> seen = new HashSet();
for (String email: emails) {
int i = email.indexOf('@');
String local = email.substring(0, i);
String rest = email.substring(i);
if (local.contains("+")) {
local = local.substring(0, local.indexOf('+'));
}
local = local.replace(".","");
seen.add(local + rest);
}
return seen.size();
}
}

总结##

此题看起来很简单,但是有些细节很折腾人!思路是取一个邮箱地址,然后用indexOf('+')找到‘+’号的位置plus,用indexOf('@')找到‘@’号的位置at。再判断plus是否为-1,若是,则表明local name中没有‘+’号,则直接用substring(0,at)得到local,再调用replace(".","")方法得到去掉‘.’号的local字符串;若否,则表明local中有‘+’号,则直接用substring(0,plus)得到local,再按照上面的方法去掉‘.’号。随后调用substring(at)得到domain,最后将local与domain拼接后放入set集合。结束时,返回set集合的大小即可。

Notes:

1.此题细节较多,需要小心;

2.将字符替换为空字符,调用replace()方法时,必须这样使用,string.replace(".",""),不知道还有没有更好的方法;

3.这种类型的题尽量调用java自带方法;

461. Hamming Distance (Easy)#

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, calculate the Hamming distance.

Note:
0 ≤ x, y < 231. Example: Input: x = 1, y = 4 Output: 2 Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑ The above arrows point to positions where the corresponding bits are different.

solution##

我的解法

class Solution {
public int hammingDistance(int x, int y) {
int count = 0;
int bitxor = x^y; //异或运算直接将两个二进制数中位不同的变为1
while (bitxor > 0)
{
if (bitxor % 2 == 1)
count++;
bitxor = bitxor >> 1; //移位运算,相当于bitxor / 2
}
return count;
}
}

目前最简单的解法

class Solution {
public int hammingDistance(int x, int y) {
return Integer.bitCount(x ^ y);
}
}

总结##

此题思路较多。

第一种可以采用常规解法,先将两个整数转换为二进制数,然后统计对应位置不同的二进制位的个数即可;

第二种解法是直接调用java自带的方法,Integer.bitCount()即可;

第三种解法是先对两个整数进行异或运算,将对应二进制位不同的变为1,转换成整数即为bitxor,然后用循环除法得到每一位二进制位,统计其中1的个数即可。

Notes:

1.学会灵活使用位运算,<<左移,>>右移,^异或;

2.对两个整数进行位运算后,得到的新结果依然是整数;

3.位运算这块得好好学习下!!!!!!!!

LeetCode--Unique Email Addresses & Hamming Distance (Easy)的更多相关文章

  1. LeetCode - Unique Email Addresses

    Every email consists of a local name and a domain name, separated by the @ sign. For example, in ali ...

  2. [leetcode] 929. Unique Email Addresses (easy)

    统计有几种邮箱地址. 邮箱名类型:local@domain 规则:1. local中出现"."的,忽略. a.bc=abc 2. local中出现"+"的,+以 ...

  3. 929. Unique Email Addresses

    929. Unique Email Addresses Easy 22766FavoriteShare Every email consists of a local name and a domai ...

  4. 【Leetcode_easy】929. Unique Email Addresses

    problem 929. Unique Email Addresses solution: class Solution { public: int numUniqueEmails(vector< ...

  5. 【LeetCode】477. Total Hamming Distance 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...

  6. [LeetCode&Python] Problem 461. Hamming Distance

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  7. [LeetCode] 929. Unique Email Addresses 唯一的电邮地址

    Every email consists of a local name and a domain name, separated by the @ sign. For example, in ali ...

  8. LeetCode 929.Unique Email Addresses

    Description Every email consists of a local name and a domain name, separated by the @ sign. For exa ...

  9. LeetCode 929 Unique Email Addresses 解题报告

    题目要求 Every email consists of a local name and a domain name, separated by the @ sign. For example, i ...

随机推荐

  1. [算法总结]DFS(深度优先搜索)

    目录 一.关于DFS 1. 什么是DFS 2. DFS的搜索方式 二.DFS的具体实现 三.剪枝 1. 顺序性剪枝 2. 重复性剪枝 3. 可行性剪枝 4. 最优性剪枝 5. 记忆化剪枝 四.练习 一 ...

  2. 如何正确管理HBase的连接,从原理到实战

    本文将介绍HBase的客户端连接实现,并说明如何正确管理HBase的连接. 最近在搭建一个HBase的可视化管理平台,搭建完成后发现不管什么查询都很慢,甚至于使用api去listTable都要好几秒. ...

  3. 【Java】用IDEA搭建源码阅读环境

    用IDEA搭建源码阅读环境 参考自CodeSheep的Mac源码环境搭建, https://www.bilibili.com/video/BV1V7411U78L 但是实际上在Windows搭建的差别 ...

  4. git以及gitHub的使用说明书

    一.使用说明 1.Git与github的功能: Git是世界上最先进的分布式版本控制系统,也就是用来记录你的项目代码历史变更信息的工具:github就是用来存储你的代码以及变更信息的云端平台: 2.优 ...

  5. 详解 Properties类

    (请观看本人博文--<详解 I/O流>) Properties类: 概念: Properties 类的对象 是 一个持久的属性集 Properties 可 保存在流中 或 从流中加载 属性 ...

  6. PHP中的11个魔术方法

    1.__get.__set 这两个方法是为在类和他们的父类中没有声明的属性而设计的 __get( $property )       当调用一个未定义的属性时访问此方法__set( $property ...

  7. php中switch与ifelse的效率分析

    1.当被判断的值是常量(固定不变的值)时,switch的运行效率比ifelse的运行效率高: $jiejie=3;   // 变判断的值为常量 switch($jiejie){   case 1:   ...

  8. (第五篇)Linux操作系统基本结构介绍

    Linux操作系统基本结构介绍 Linux系统一般有4个主要部分:内核.shell.文件系统和应用程序.内核.shell和文件系统一起形成了基本的操作系统结构,它们使得用户可以运行程序.管理文件并使用 ...

  9. OpenCV学习(4)——动态结构

    学习一个新知识,无外乎学习它本身和它的工具.OpenCV提供许多内置的结构及处理函数,非常值得学习. 内存存储 在OpenCV中,内存存储器是一个可以用来存储序列.数组和图像的动态增长的数据结构.它由 ...

  10. SpringBoot @ConfigurationProperties详解

    文章目录 简介 添加依赖关系 一个简单的例子 属性嵌套 @ConfigurationProperties和@Bean 属性验证 属性转换 自定义Converter SpringBoot @Config ...