精准 优雅

我从很小的时候就开始接触计算机了。

刚开始学习编程的时候,我惊叹于系统运作的分毫不差,总能带给我想要的结果。

后来进了校队,进了省队,去了无数大公司实习,设计了足够复杂的系统。计算机在我心中任然精准,但偶尔也会发现她缺了几斯灵性,少了几分优雅。之后又看到很多人说机器的冰冷无情,我不能理解,因为在我眼里计算机科学是一门精致的艺术,完美到丝毫不差。

直到再后来,我经历了世事,才发现其实机器的精准优雅一成不变,只是我们触到了他冰冷的心。

Excel Sheet Column Title

Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:

    1 -> A
    2 -> B
    3 -> C
    ...
    26 -> Z
    27 -> AA
    28 -> AB

这题看起来不难,但居然做了我两个小时。其中还是有很多值得注意一下的地方的。

def convertToTitle2(n):
    ret = ""
    while(n):
        ret = chr( (n-1)%26 + 65 ) + ret
        n = (n-1)/26 #核心
    return ret

先贴一下最后AC的代码。我之前用了很长的代码去判断字符进位的情况,但效果都不理想。

Power of Three

Given an integer, write a function to determine if it is a power of three.

class Solution(object):
    def isPowerOfThree(self, n):
        #while (n and (n % 3 == 0)):
            #n = n / 3
        #return n == 1
        
        
        #return (n > 0 and 1162261467 % n == 0)
        
        return (n > 0 and int(math.log10(n) / math.log10(3)) - math.log10(n) / math.log10(3) == 0)

Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode* root)
    {
        if(root == NULL)
            return 0;  
        int res = 1;  
        int l = maxDepth(root->left);  
        int r = maxDepth(root->right);  
        return l > r? l + 1:r+ 1;  
    }
};

Contains Duplicate

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

class Solution {
public:
    bool containsDuplicate(vector<int>& nums)
    {
        if(nums.empty())
            return false;
            
        set<int> s;
        vector<int>::iterator itr = nums.begin();
        
        while(itr != nums.end())
        {
            if(s.count(*itr) == 1)
                return true;
            s.insert(*itr);
            itr++;
        }
        return false;
    }
};

Power of two

Given an integer, write a function to determine if it is a power of two.

class Solution(object):
    def isPowerOfTwo(self, n):
        while (n and (n % 2 == 0)):
            n = n / 2
            
        return n == 1
        
        #Certainly,you can also use the following method
        #return (n and (n&(n-1))) == 0

Ugly number

Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.
Note that 1 is typically treated as an ugly number.

class Solution(object):
    def isUgly(self, number):
        if number == 1:
            return True
        if number == 0:
            return False
        
        while(number % 2 == 0):
            number /= 2
        while(number % 3 == 0):
            number /= 3
        while(number % 5 == 0):
            number /= 5
    
        return number == 1

Ugly number II

Write a program to find the n-th ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.
Note that 1 is typically treated as an ugly number.

class Solution(object):
    def min_three(self, a, b, c):
        minNum = min(a,b)
        return min(minNum,c)
    def nthUglyNumber(self, n):
        ugly = [1]
        factor2 = 2
        factor3 = 3
        factor5 = 5
        index2 = index3 = index5 = 0
        for i in range(1,n):
            minNum = min(factor2, factor3, factor5)
            ugly.append(minNum)
            if(factor2 == minNum):
                factor2 = 2 * ugly[index2+1]
                index2 += 1
            if(factor3 == minNum):
                factor3 = 3 * ugly[index3+1]
                index3 += 1
            if(factor5 == minNum):
                factor5 = 5 * ugly[index5+1]
                index5 += 1
        return ugly[n-1]

Bulls and Cows

You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called “bulls”) and how many digits match the secret number but locate in the wrong position (called “cows”). Your friend will use successive guesses and hints to eventually derive the secret number.
For example:

Secret number: “1807”
Friend’s guess: “7810”

Hint: 1 bull and 3 cows. (The bull is 8, the cows are 0, 1 and 7.)
Write a function to return a hint according to the secret number and friend’s guess, use A to indicate the bulls and B to indicate the cows. In the above example, your function should return “1A3B”.
Please note that both secret number and friend’s guess may contain duplicate digits, for example:

Secret number: “1123”
Friend’s guess: “0111”

In this case, the 1st 1 in friend’s guess is a bull, the 2nd or 3rd 1 is a cow, and your function should return “1A1B”.
You may assume that the secret number and your friend’s guess only contain digits, and their lengths are always equal.

class Solution {
public:
    string getHint(string secret, string guess)
    {
        int cntA = 0, cntB = 0;
        unordered_map<char, int> hash;
        vector<bool> tag(secret.size(), false);
        
        for (auto a : secret)
        {
            ++hash[a];
        };
        
        for (int i = 0; i < secret.size(); ++i)
        {
            if (secret[i] == guess[i])
            {
                ++cntA;
                --hash[secret[i]];
                tag[i] = true;
            }
        }
        for (int i = 0; i < guess.size(); ++i)
        {
            if (!tag[i] && hash[guess[i]] > 0)
            {
                ++cntB;
                --hash[guess[i]];
            }
        }
        return to_string(cntA) + "A" + to_string(cntB) + "B";
    }
};

Google镜像站列表

网址转载自: http://www.itechzero.com/google-mirror-sites-collect.html
感谢Techzero为天朝人民所做的贡献。
以下是直接使用谷歌的方法,如需科学上网戳这里(VPN | Shadowsocks

原版镜像:

[推荐]https://www.psea.in/
[推荐]https://www.guge.link/
[推荐]https://www.guge.click/
[推荐]https://www.souguge.com/
http://pds.gs/
http://g.kvm.la/
https://g1.wen.lu/
https://opennet.xyz/
https://gg.knight.ren/
http://guge.cytbj.com/
http://www.fcczp.com/
https://i.xueshugu.com/
https://search.chun.pro/
http://tokillgoogle.com/
https://google.xface.me/
http://www.baidu.com.se/
https://google.checkme.com.cn/
http://google.sidney-aldebaran.me/

非原版镜像:

[推荐]微搜:http://www.wesou.org/
[推荐]谷歌363:http://www.g363.com/
[推荐]图灵搜索:https://www.tulingss.com/
G搜索:http://www.xpan.so/
Ask:http://home.tb.ask.com/
AOL:http://m.search.aol.com/
谷歌婊:http://www.gugebiao.com/
Avira:https://safesearch.avira.com/
丸子138:http://www.wanzi138.com/
谷粉138:http://www.gufen138.com/
丸子163:http://www.wanzi163.com/
谷粉163:http://www.gufen163.com/
Babylon:http://isearch.babylon.com/
Disconnect:https://search.disconnect.me/

谷歌学术

[推荐]https://www.scholar.live/
[推荐]http://guge.cytbj.com/scholar/
[推荐]https://www.souguge.com/scholar/
http://g.linkscholar.org/
https://www.roolin.com/
http://scholar.g363.com/
http://www.scholarnet.cn/
http://scholar.searcher.top/
https://www.psea.in/scholar/

谷歌图片

[推荐]https://www.psea.in/imghp
[推荐]https://www.guge.link/imghp
[推荐]https://www.souguge.com/imghp
http://guge.cytbj.com/imghp
http://www.fcczp.com/imghp
https://www.guge.click/imghp
https://google.xface.me/imghp