Skip to content

Latest commit

 

History

History
119 lines (103 loc) · 4.5 KB

ARTS26-1229.md

File metadata and controls

119 lines (103 loc) · 4.5 KB

ARTS26-20181229

Algorithm

package org.nocoder.leetcode.solution;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * Every email consists of a local name and a domain name, separated by the @ sign.
 * <p>
 * For example, in `alice@leetcode.com`, `alice` is the local name, and `leetcode.com` is the domain name.
 * <p>
 * Besides lowercase letters, these emails may contain `'.'`s or `'+'`s.
 * <p>
 * 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.)
 * <p>
 * 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.)
 * <p>
 * It is possible to use both of these rules at the same time.
 * <p>
 * Given a list of `emails`, we send one email to each address in the list.
 * How many different addresses actually receive mails?
 * <p>
 * <p>
 * <p>
 * **Example 1:**
 * <p>
 * ```shell
 * 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
 * ```
 * <p>
 * **Note:**
 * <p>
 * - `1 <= emails[i].length <= 100`
 * - `1 <= emails.length <= 100`
 * - Each `emails[i]` contains exactly one `'@'` character.
 *
 * @author yangjinlong
 * @date 2019-01-02
 */
public class UniqueEmailAddress {
    public int numUniqueEmails(String[] emails) {
        if (emails == null || emails.length > 100) {
            throw new IllegalArgumentException();
        }
        String pattern = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
        Set emailSet = new HashSet();
        for (int i = 0; i < emails.length; i++) {
            if (Pattern.matches(pattern, emails[i])) {
                int plusIdx = emails[i].indexOf("+");
                int atIdx = emails[i].indexOf("@");
                String ignoreContent = emails[i].substring(plusIdx, atIdx);
                String email = emails[i].replace(ignoreContent, "");
                String localName = email.substring(0, email.indexOf("@"));
                String domainName = email.substring(email.indexOf("@"), email.length() - 1);
                email = localName.replace(".", "").concat(domainName);
                emailSet.add(email);
            }
        }
        return emailSet.size();
    }

    public static void main(String[] args) {
        String[] emails = new String[]{"test.email+alex@leetcode.com", "test.e.mail+bob.cathy@leetcode.com", "testemail+david@lee.tcode.com"};
        UniqueEmailAddress uniqueEmailAddress = new UniqueEmailAddress();
        int n = uniqueEmailAddress.numUniqueEmails(emails);
        System.out.println(n);
    }
}

Review

https://netty.io/wiki/user-guide-for-4.x.html#wiki-h2-0

Tip

python 爬取当当图书信息

import requests
from bs4 import BeautifulSoup
from fake_useragent import UserAgent

def crawl_book_info(isbn):
    agent = UserAgent()
    headers = {'User-Agent': agent.random}
    url = 'http://search.dangdang.com/?act=input&key=' + isbn
    data = requests.get(url, headers=headers)
    soup = BeautifulSoup(data.text, 'lxml')
    ul = soup.find_all('ul', {'class': 'bigimg'})
    title = ''
    image = ''
    if len(ul) > 0:
        li = ul[0].find_all('li')
        if len(li) > 0:
            title = li[0].find('a').get('title')
            image = li[0].find('img').get('src')
    return title, isbn, image

Share

假如你是个医生,病人要求你在手术前不要洗手,因为病人认为洗手太耽误时间了,你会照做吗。当然不会,因为医生是专业的,知道病菌的危害有多大,手术风险有多大。同理,程序员盲目遵从不明风险的经理的要求,也是不专业的。 --《Clean Code》

如果你让你自己陷入忙忙忙的境地,你要特别小心了。你没有时间好好思考,你就不会成长,你就不会渐渐做出调整,你就可能无法适应外面的世界,你就会在危机来临时不堪一击。... 保持思考的习惯,思考一些大的东西。「打小算盘」并不是有价值的思考。--蔡学镛