Reverse String

class Solution {
public:
    string reverseString(string s) {
        int str = 0;
        int end = s.length()-1;
        while (str<=end) {
            char tmp;
            tmp = s[str];
            s[str] = s[end];
            s[end] = tmp;
            str++;
            end--;
        }
        return s;
    }
};
2017/12/4 posted in  leetcode

Reverse Words in a String III

class Solution {
public:
    string reverseWords(string s) {
        string res;
        int str = 0;
        for (int i=0; i<s.length(); i++) {
            if (s[i]==' '||i==s.length()-1) {
                int j = i==s.length()-1 ? i:i-1;
                while (j>=str) {
                    char tmp = s[str];
                    s[str] = s[j];
                    s[j] = tmp;
                    j--;
                    str++;
                }
                str = i+1;
            }
        }

        return s;
    }
};
2017/12/4 posted in  leetcode

Judge Route Circle

class Solution {
public:
    bool judgeCircle(string moves) {
        int UD = 0 ,LR = 0;
        for(char a:moves){
            if (a=='U') {
                UD++;
            }else if (a=='D'){
                UD--;
            }else if (a=='L'){
                LR++;
            }else if (a=='R'){
                LR--;
            }
        }
        return (UD==0&&LR==0) ? 1:0;
    }
};
2017/12/2 posted in  leetcode

Self Dividing Numbers

class Solution {
public:
    vector<int> selfDividingNumbers(int left, int right) {
        vector<int> v;
        for (int i = left; i <= right; ++i) {
            int j= i;
            bool ok = 1;
            while (j) {
                int d= (j%10);
                if (d == 0 || i % d) ok = 0;
                j/=10;
            }
            if (ok) v.push_back(i);
        }
        return v;
    }
};
2017/12/2 posted in  leetcode

Non-decreasing Array

用到了is_sorted来判断是否升序排列

class Solution {
public:
    bool checkPossibility(vector<int>& nums) {
        for (int i=0; i < nums.size()-1; i++){
            if (nums[i] > nums[i+1]){
                
                int temp = nums[i];
                //
                // "erase" nums[i], then check if nums is sorted without nums[i]
                //
                nums[i] = nums[i+1];
                if (is_sorted(nums.begin(), nums.end())) { return true; }
                
                //
                // "erase" nums[i+1], then check if nums is sorted without nums[i+1]
                //
                nums[i+1] = nums[i] = temp;
                if (is_sorted(nums.begin(), nums.end())) { return true; }
                
                //
                // nums is NOT sorted (without nums[i] XOR without nums[i+1])
                //
                return false;
            }
        }
        return true;
    }
};
2017/12/2 posted in  leetcode

Third Maximum Number

利用set的自动排序和去除重复元素来解决问题。

class Solution {
public:
    int thirdMax(vector<int>& nums) {
        set<int> result;
        for (int num:nums) {
            result.insert(num);
            if (result.size()>3) {
                result.erase(result.begin());
            }
        }
        return result.size()<3? *result.rbegin():*result.begin();
    }
};
2017/12/2 posted in  leetcode