Ransom Note

class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) {
        unordered_map<char, int> map;
        for (int i = 0; i<magazine.length(); i++) {
            map[magazine[i]]++;
        }
        for (int j = 0; j<ransomNote.length(); j++) {
            if(--map[ransomNote[j]]<0) return 0;
        }
        return 1;
    }
};
2017/12/9 posted in  leetcode

Reverse String II

运用内置的reverse函数

class Solution {
public:
    string reverseStr(string s, int k) {
        for (int i = 0; i<s.length(); i+=2*k) {
            reverse(s.begin()+i, min(s.begin()+i+k, s.end()));
        }
        return s;
    }
};
2017/12/9 posted in  leetcode

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