LeetCode--Unique Email Addresses & Hamming Distance (Easy)
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)的更多相关文章
- LeetCode - Unique Email Addresses
Every email consists of a local name and a domain name, separated by the @ sign. For example, in ali ...
- [leetcode] 929. Unique Email Addresses (easy)
统计有几种邮箱地址. 邮箱名类型:local@domain 规则:1. local中出现"."的,忽略. a.bc=abc 2. local中出现"+"的,+以 ...
- 929. Unique Email Addresses
929. Unique Email Addresses Easy 22766FavoriteShare Every email consists of a local name and a domai ...
- 【Leetcode_easy】929. Unique Email Addresses
problem 929. Unique Email Addresses solution: class Solution { public: int numUniqueEmails(vector< ...
- 【LeetCode】477. Total Hamming Distance 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...
- [LeetCode&Python] Problem 461. Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] 929. Unique Email Addresses 唯一的电邮地址
Every email consists of a local name and a domain name, separated by the @ sign. For example, in ali ...
- LeetCode 929.Unique Email Addresses
Description Every email consists of a local name and a domain name, separated by the @ sign. For exa ...
- LeetCode 929 Unique Email Addresses 解题报告
题目要求 Every email consists of a local name and a domain name, separated by the @ sign. For example, i ...
随机推荐
- 第十节:xml、re、logging模块
XML模块:(用到的时候再看)tree=xml.parse('xmltest.xml')root= tree.getroot()print(root.tag) 打印对象的标签root.attrib 获 ...
- 利用SSIS进行SharePoint 列表数据的ETL
好几年前写了一篇<SSIS利用Microsoft Connector for Oracle by Attunity组件进行ETL!>,IT技术真是日新月异,这种方式对于新的SQL SERV ...
- 作为python开发者,这几个PyCharm 技巧你必须掌握!
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取htt ...
- 【简单了解系列】从基础的使用来深挖HashMap
HashMap定义 说的专业一点,HashMap是常用的用于存储key-value键值对数据的一个集合,底层是基于对Map的接口实现.每一个键值对又叫Entry,这些Entry分散的存储在一个由数组和 ...
- 并发工具——CyclicBarrier
本博客系列是学习并发编程过程中的记录总结.由于文章比较多,写的时间也比较散,所以我整理了个目录贴(传送门),方便查阅. 并发编程系列博客传送门 CyclicBarrier简介 CyclicBarrie ...
- 华为鲲鹏服务器安装 k3s+rancher
华为鲲鹏服务器安装 k3s+rancher 华为鲲鹏服务器 华为鲲鹏服务器采用华为自研cpu ARMv8架构,提供 Windows 和多个Linux 系统,作为服务器使用我一直使用Centos系统(不 ...
- php 全局变量和超全局变量
global 全局变量(在当前页面全部地方有效) $GLOBALS['name'] 超全局变量(在整个网站全部地方有效) 一个包含了全部变量的全局组合数组.变量的名字就是数组的键.还有_GET,_PO ...
- 讲讲python中函数的参数
python中函数的参数 形参:定义函数时代表函数的形式参数 实参:调用函数时传入的实际参数 列如: def f(x,y): # x,y形参 print(x, y) f(1, 2) # 1, 2 实参 ...
- (转)ATOM介绍和使用
一,Atom介绍 Atom 是 Github 开源的文本编辑器,这个编辑器完全是使用Web技术构建的(基于Node-Webkit).启动速度快,提供很多常用功能的插件和主题,可以说Atom已经足以胜任 ...
- java之重载与重写
重写(override) 重写是子类对父类的允许访问的方法的重新编写,方法名,返回值类型和形参列表都不能改变,唯一恩能够改变的是方法体. 重写的好处是可以根据子类的需要的行为来实现父类的方法. 重写方 ...