LeetCode刷題

#7.反轉整數

class Solution {
public:
    int reverse(int x) {
        string s = to_string(x);
        std::reverse(s.begin(), s.end());
        int res = 0;
        try {
            if (x < 0)
                res = -stoi(s);
            else
                res = stoi(s);
        } catch (exception e) {
            return 0;
        }
        return res;
    }
};

#9.迴文數

class Solution {
public:
    bool isPalindrome(int x) {
        if (x < 0) return false;
        string s = to_string(x);
        size_t len = s.size();
        size_t i = 0, j = len - 1;
        while (i < j) {
            if (s[i++] != s[j--]) return false;
        }
        return true;
    }
};

#13.羅馬數字轉整數

class Solution {
private:
    unordered_map<int, int> ROMAN;
public:
    Solution() {
        ROMAN['I'] = 1;
        ROMAN['V'] = 5;
        ROMAN['X'] = 10;
        ROMAN['L'] = 50;
        ROMAN['C'] = 100;
        ROMAN['D'] = 500;
        ROMAN['M'] = 1000;
    }

    int romanToInt(string s) {
        int num = 0;
        for (int i = 0; i < s.size(); ++i) {
            switch (s[i]) {
                case 'I':
                    if (i + 1 < s.size()) {
                        if (s[i + 1] == 'V') {
                            num += 4;
                            i++;
                        } else if (s[i + 1] == 'X') {
                            num += 9;
                            i++;
                        } else {
                            num += 1;
                        }
                    } else {
                        num += 1;
                    }
                    break;
                case 'X':
                    if (i + 1 < s.size()) {
                        if (s[i + 1] == 'L') {
                            num += 40;
                            i++;
                        } else if (s[i + 1] == 'C') {
                            num += 90;
                            i++;
                        } else {
                            num += 10;
                        }
                    } else {
                        num += 10;
                    }
                    break;
                case 'C':
                    if (i + 1 < s.size()) {
                        if (s[i + 1] == 'D') {
                            num += 400;
                            i++;
                        } else if (s[i + 1] == 'M') {
                            num += 900;
                            i++;
                        } else {
                            num += 100;
                        }
                    } else {
                        num += 100;
                    }
                    break;
                default:
                    num += ROMAN[s[i]];
                    break;
            }
        }
        return num;
    }
};

#14.最長公共前綴

class Solution {
public:
    string longestCommonPrefix(vector<string> &strs) {
        if (strs.size() == 0) return "";
        if (strs.size() == 1) return strs[0];

        string prefix = "";
        for (int i = 0; i < strs[0].size(); ++i) {
            int c = strs[0][i];
            bool yes = true;
            for (int j = 1; j < strs.size(); ++j)
                if (strs[j][i] != c) {
                    yes = false;
                    break;
                }
            if (yes)
                prefix += c;
            else
                break;
        }
        return prefix;
    }
};

#21.合併兩個有序鏈表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
        if (!l1)return l2;
        if (!l2) return l1;

        ListNode *dummyHead1 = new ListNode(-1);
        dummyHead1->next = l1;
        ListNode *dummyHead2 = new ListNode(-1);
        dummyHead2->next = l2;
        ListNode *p1 = l1;
        ListNode *p2 = l2;

        ListNode *p = dummyHead1;
        while (p1 && p2) {
            if (p1->val < p2->val) {
                p->next = p1;
                p = p1;
                p1 = p1->next;
            } else {
                p->next = p2;
                p = p2;
                p2 = p2->next;
            }
        }
        if (p1)
            p->next = p1;
        if (p2)
            p->next = p2;
        return dummyHead1->next;
    }
};

#28.實現strStr()

class Solution {
public:
    int strStr(string haystack, string needle) {
        if (needle.size() == 0)return 0;
        if (needle.size() > haystack.size())return -1;

        int j = 0;
        for (int i = 0; i < haystack.size(); ++i) {
            if (haystack[i] == needle[j]) {
                j++;
                if (j >= needle.size())
                    return (int) (i - needle.size() + 1);
            } else {
                i = i - j; // 回溯,如 mississippi , issip
                j = 0;
            }
        }
        return -1;
    }
};

#35.搜索插入位置

class Solution {
public:
    int searchInsert(vector<int> &nums, int target) {
        int size = (int) nums.size();
        for (int i = 0; i < size; ++i) {
            if (nums[i] >= target) {
                return i;
            }
        }
        return size;
    }
};

#38.Count and Say

class Solution {
public:
    string countAndSay(int n) {
        vector<string> SEQ;

        SEQ.push_back("0");
        SEQ.push_back("1");
        string curSeq = SEQ[1];
        for (int i = 2; i <= n; ++i) {
            string tmp;
            int count = 1;
            char curNum = curSeq[0];
            for (int j = 1; j < curSeq.size(); ++j)
                if (curSeq[j] == curNum)
                    count++;
                else {
                    tmp += to_string(count);
                    tmp += curNum;
                    count = 1;
                    curNum = curSeq[j];
                }

            tmp += to_string(count);
            tmp += curNum;
            SEQ.push_back(tmp);
            curSeq = SEQ[i];
        }
        return SEQ[n];
    }
};

#53.最大子序和

class Solution {
public:
    int maxSubArray(vector<int> &nums) {
        int thisSum = 0;
        int maxSum = 0;
        bool isAllNegative = true;
        int maxValue = nums[0];
        for (int i = 0; i < nums.size(); ++i) {
            if (nums[i] > 0) isAllNegative = false;
            if (nums[i] > maxValue)maxValue = nums[i];

            thisSum += nums[i];
            if (thisSum > maxSum)
                maxSum = thisSum;
            else if (thisSum < 0)
                thisSum = 0;
        }
        if (isAllNegative)
            return maxValue;
        else
            return maxSum;
    }
};

#58.最後一個單詞的長度

class Solution {
public:
    int lengthOfLastWord(string s) {
        if (s.size() == 0) return 0;
        int r;
        for (r = (int) (s.size() - 1); r >= 0; --r)
            if (s[r] != ' ')
                break;

        int l;
        for (l = r - 1; l >= 0; --l)
            if (s[l] == ' ') {
                l += 1;
                break;
            }

        if (l < 0)
            l = 0;
        return r - l + 1;
    }
};

#66.加一

class Solution {
public:
    vector<int> plusOne(vector<int> &digits) {
        int carry = 1;
        for (int i = (int) (digits.size() - 1); i >= 0; --i) {
            digits[i] += carry;
            if (digits[i] >= 10) {
                digits[i] %= 10;
                carry = 1;
            } else
                carry = 0;
        }
        if (carry)
            digits.insert(digits.begin(), 1);
        return digits;
    }
};

#67.二進制求和

class Solution {
public:
    string addBinary(string a, string b) {
        if (a.size() < b.size()) {
            string tmp = a;
            a = b;
            b = tmp;
        }

        int carry = 0;
        int j = (int) (b.size() - 1);
        for (int i = (int) (a.size() - 1); i >= 0; --i) {
            int digit;
            if (j < 0)
                digit = c2i(a[i]) + carry;
            else
                digit = c2i(a[i]) + c2i(b[j--]) + carry;
            if (digit >= 2) {
                digit %= 2;
                carry = 1;
            } else {
                carry = 0;
            }
            a[i] = i2c(digit);
        }
        if (carry)
            a.insert(a.begin(), '1');
        return a;
    }

    int c2i(char c) {
        return c - '0';
    }

    char i2c(int x) {
        return (char) (x + '0');
    }
};

#69.x 的平方根

class Solution {
public:
    int mySqrt(int x) {
        if (x <= 1) return x;
        int low = 0, high = x;
        while (low <= high) {
            int mid = (low + high) / 2;
            if (mid <= x / mid && (mid + 1) > x / (mid + 1)) {
                return mid;
            } else if (mid > x / mid) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return 0;
    }
};

#70.爬樓梯

class Solution {
private:
    unordered_map<int, int> STAIR;
public:
    Solution() {
        STAIR[1] = 1;
        STAIR[2] = 2;
    }

    int climbStairs(int n) {
        if (STAIR.find(n) != STAIR.end())
            return STAIR[n];
        else
            return STAIR[n] = climbStairs(n - 1) + climbStairs(n - 2);
    }
};

#83.刪除排序鏈表中的重複元素

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
        if (head == NULL || head->next == NULL) return head;
        ListNode *pre = head;
        ListNode *p = head->next;
        while (p) {
            if (p->val == pre->val) {
                ListNode *delNode = p;
                pre->next = p->next;
                p = p->next;
                delete delNode;
            } else {
                pre = p;
                p = p->next;
            }
        }
        return head;
    }
};

#136.只出現一次的數字

class Solution {
public:
    int singleNumber(vector<int> &nums) {
        int ans = 0;
        for (int i = 0; i < nums.size(); ++i)
            ans ^= nums[i];
        return ans;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章