Base 7

class Solution {
public:
    string convertToBase7(int num) {
        int x = abs(num); string res;
        do {
          res = to_string(x%7)+res;
        } while (x/=7);
        
        return (num>=0? "" : "-") + res;
    }
};
2017/12/9 posted in  leetcode

License Key Formatting

第一版:

    int log = 0;
    string ele="";
    string re="";
    for (int i = S.length()-1; i>=0; i--) {
        if (S[i]!='-') {
            log++;
            S[i]=toupper(S[i]);
            ele = S[i]+ele;
        }
        if (log==K||i==0) {
            if (i==0) {
                re = ele+re;
            }else{
                re = '-'+ele+re;
            }
            log = 0;
            ele = "";
        }
    }
    return re[0]=='-'?re.substr(1,re.length()-1):re;

第二版:运用栈

class Solution {
public:
    string licenseKeyFormatting(string S, int K) {
        stack<string> st;
        string line;
        for(int i=S.length()-1;i>=0;i--){
            if(S[i]=='-') continue;
            
            if(line.length()<K){
                if(S[i]>='a' && S[i]<='z') line.insert(0, 1, S[i]-'a'+'A');
                else line.insert(0, 1, S[i]);
                
                if(line.length()==K){
                    st.push(line);
                    line="";
                }
            }
        }
        if(st.empty()) return line;
        
        if(line.length() == 0){
            line += st.top();
            st.pop();
        }
        while(!st.empty()){
            line+='-';
            line+=st.top();
            st.pop();
        }
        return line;
    }
};
2017/12/9 posted in  leetcode

Happy Number

class Solution {
public:
    int happy(int num){
        int a = 0;
        while (num!=0) {
            a += int(pow(num%10, 2));
            num/=10;
        }
        return a;
    }


    bool isHappy(int n) {
        int slow, fast;
        slow = fast = n;
        do {
            slow = happy(slow);
            fast = happy(fast);
            fast = happy(fast);
        } while(slow != fast);

        return slow==1?1:0;
    }
};
2017/12/8 posted in  leetcode

Find the Difference

class Solution {
public:
    char findTheDifference(string s, string t) {
        char sum = 0;
        for (char c: s) {
            sum^=c;
        }
        for (char c: t) {
            sum^=c;
        }
        return sum;
    }
};
2017/12/8 posted in  leetcode

Distribute Candies

class Solution {
public:
    int distributeCandies(vector<int>& candies) {
        int res = 0;
        unordered_map<int, vector<int>> map;
        for (int i =0; i<candies.size(); i++) {
            map[candies[i]].push_back(candies[i]);
        }
        if (map.size()<=candies.size()/2) {
            return map.size();
        }else{
            return candies.size()/2;
        }
    }
};
2017/12/8 posted in  leetcode

Fizz Buzz

Write a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

class Solution {
public:
    vector<string> fizzBuzz(int n) {
        vector<string> res;
        for (int i=1; i<=n; i++) {
            if (i%3==0&&i%5==0) {
                res.push_back("FizzBuzz");
            }else if (i%5==0){
                res.push_back("Buzz");
            }else if(i%3==0){
                res.push_back("Fizz");
            }else{
                res.push_back(to_string(i));
            }
        }
        return res;
    }
};
2017/12/8 posted in  leetcode