LeetCode: 继续easy题8

Nth Digit

"""蛋疼,老Time Limit Exceeded,结果最后是两个long的地方越界
"""
class Solution {
public:
    int findNthDigit(int n) {
        /*
        i=1: 1,...,9: 9     num_i
        i=2: 10,...,99: 90        
        i=3: 100,..,999: 900      

        */
        if(n<10)
            return n;

        int i=1;
        long num_i = 9;
        long num = n;

        num -= i*num_i;
        while(num>0){
            i ++;
            num_i *= 10;

            num -= i*num_i;
        } //在i位数


        //i位数的第一个数字:
        int start = pow(10,i-1);
        //i位数的第一个数字之前共累计了多少digit
        int start_n = n-(num + i*num_i);

        //在第几个数字
        int offset1 = (n-start_n)/i;
        int offset2 = (n-start_n)%i;

        if (offset2 == 0){
            int t_num = start + offset1 - 1;
            return t_num % 10;
        }
        else{
            int t_num = start + offset1;
            return (t_num / ((int)pow(10,i-offset2))%10);
        }
    }
};
"""更好的写法
"""
class Solution {  
public:  
    int findNthDigit(int n) {  
        long digit = 1, ith = 1, base = 9;  
        while(n > base*digit)  
        {  
            n -= base*(digit++);  
            ith += base;  
            base *= 10;  
        }  
        return to_string(ith+(n-1)/digit)[(n-1)%digit]-'0';  
    }  
};  

Binary Watch

"""比较困难
http://www.cnblogs.com/grandyang/p/4606334.html
"""
class Solution {
public:
    vector<string> readBinaryWatch(int num) {
        vector<string> res;
        for (int h = 0; h < 12; ++h) {
            for (int m = 0; m < 60; ++m) {
                if (bitset<10>((h << 6) + m).count() == num) {
                    res.push_back(to_string(h) + (m < 10 ? ":0" : ":") + to_string(m));
                }
            }
        }
        return res;
    }
};

Sum of Left Leaves

"""简单。
更多解法见http://www.cnblogs.com/grandyang/p/4606334.html
"""
/**
 * 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 cal(TreeNode* root, bool if_left){
        if(root==NULL)
            return 0;        
        if(root->left==NULL && root->right==NULL && if_left == true)
            return root->val;

        return cal(root->left, true) + cal(root->right, false);

    }
    int sumOfLeftLeaves(TreeNode* root) {
        if(root==NULL)
            return 0;
        return cal(root->left, true) + cal(root->right, false);        
    }
};

Convert a Number to Hexadecimal

"""
"""
class Solution {
public:
    string toHex(int num) {
        if (num == 0)
            return "0";
        string re = "";
        for(int i=0;num && i<8;i++){
            int t = num & 0xf;
            if(t>=10)
                re = char('a'+t-10) + re;
            else
                re = char('0' + t) + re;
                // or to_string(t) + re;
            num >>= 4;
        }

        return re.empty()? "-1":re;
    }
};

Longest Palindrome

"""统计出现元素的对数,hash table
"""
class Solution {
public:
    int longestPalindrome(string s) {
        unordered_map<char,int> map;
        int num_pairs=0;
        for(char c:s){
            if(map.find(c)==map.end())
                map.insert({c,1});
            else{
                map[c] += 1;
                if(map[c]%2==0)
                    num_pairs += 1;
            }
        }

        for(auto i:map)
            if(i.second%2==1){
                return 2*num_pairs+1;
            }

        return 2*num_pairs;
    }
};

Fizz Buzz

"""
"""
class Solution {
public:
    vector<string> fizzBuzz(int n) {
        vector<string> re;
        for(int i=1;i<=n;i++){
            if(i%3==0 && i%5==0)
                re.push_back("FizzBuzz");
            else if(i%3==0)
                re.push_back("Fizz");
            else if(i%5==0)
                re.push_back("Buzz");
            else
                re.push_back(to_string(i));
        }

        return re;
    }
};

Third Maximum Number

"""
"""
class Solution {
public:
    int thirdMax(vector<int>& nums) {
        long first = LONG_MIN, second = LONG_MIN, third = LONG_MIN;
        for(int n : nums){
            if(n==first||n==second)
                ;
            else if(n>first){
                third = second;
                second = first;
                first = n;
            }
            else if(n>second){
                third = second;
                second = n;
            }
            else if(n>third)
                third = n;
        }

        return third==LONG_MIN? first: third;
    }
};

Add Strings

"""
"""
class Solution {
public:
    string addStrings(string num1, string num2) {
        string re="";
        int i = num1.size()-1;
        int j = num2.size()-1;
        int plus = 0;

        while(i>=0||j>=0){
            int n1 = i>=0?num1[i--]-'0':0;
            int n2 = j>=0?num2[j--]-'0':0;
            int sum = n1+n2+plus;
            if(sum>=10){
                plus = 1;
                sum = sum%10;
            }
            else 
                plus = 0;

            re = to_string(sum) + re;
        }

        if(plus>0)
            re = '1' + re;

        return re;
    }
};

Number of Segments in a String

"""不如统计开头简单
"""
class Solution {
public:
    int countSegments(string s) {
        if(s.size()==0)
            return 0;

        bool begin = false;
        int n = 0;

        for(char c:s){
            if(c!=' ')
                begin = true;

            //前面没有空格了
            if(begin){
                if(c==' '){
                    n ++;
                    begin = false;
                }
            }
        }

        return s[s.size()-1] == ' '? n:n+1;
    }
};

Rotated Digits

class Solution {
public:
    int isGood(int n, unordered_map<int,int> map){
        int t = 0;
        int i = 1;
        int temp = n;
        while(n){
            if(map.find(n%10)==map.end())
                return 0;
            t += map[n%10]*i;
            n /= 10;
            i *= 10;
        }
        return t==temp? 0:1;
    }


    int rotatedDigits(int N) {
        int num = 0;
        unordered_map<int,int> map;
        map.insert({0,0});
        map.insert({1,1});
        map.insert({8,8});
        map.insert({2,5});
        map.insert({5,2});
        map.insert({6,9});
        map.insert({9,6});

        for(int i=2;i<=N;i++){
            num += isGood(i, map);
        }
        return num;
    }
};

Custom Sort String

class Solution {
public:
    string customSortString(string S, string T) {
        unordered_map<char,int> map;
        for(char c : T){
            if(map.find(c)==map.end())
                map.insert({c,1});
            else
                map[c] += 1;
        }

        vector<char>  a;
        for(char c:S)
            a.push_back(c);

        string re="";
        for (char c : a){
            if(map.find(c)!=map.end()){
                for(int i=0;i<map[c];i++)
                    re += c;
            map.erase(c);
            }
        }

        for(auto m: map)
            for(int i=0;i<m.second;i++)
                re += m.first;

        return re;
    }
};

Domino and Tromino Tiling

"""其实是个组合数学问题==
https://cs.stackexchange.com/questions/66658/domino-and-tromino-combined-tiling
递归超时,改用迭代!
越界问题特别蛋疼!用python很容易通过--
"""
class Solution {
public:
    /*
    long cal(int N){
        if(N==3)
            return 5;
        if(N==2)
            return 2;
        if(N==1)
            return 1;
        return 2*cal(N-1) + cal(N-3);             
    }
    */
    int numTilings(int N) {
        int n = pow(10,9)+7;
        vector<long> dp(1001);
        dp[0] = 1;
        dp[1] = 1;
        dp[2] = 2;
        dp[3] = 5;
        for(int i=4;i<=N;i++)
            dp[i] = (dp[i-1]%n + (dp[i-1]%n + dp[i-3]%n)%n)%n;
        return dp[N];
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值