LeetCode刷算法题-简单难度(一)题号1-88

1.两数之和
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        map<int, int> m;
        vector<int> res;
        for(int i = 0; i < nums.size(); ++i){
            m[nums[i]] = i;
        }
        for(int i = 0; i < nums.size(); ++i){
            int temp = target - nums[i];
            if(m.count(temp) && m[temp] != i){
                res.push_back(i);
                res.push_back(m[temp]);
                break;
            }
        }
        return res;    
    }
};
7.整数反转
class Solution {
public:
    int reverse(int x) {
        int temp = 0;
        while(x != 0){
            if(temp > INT_MAX / 10){
                return false;
            }
            temp = temp*10 + x % 10;
            x /= 10;
        }
        return temp;
    }
};
9.回文数
class Solution {
public:
    bool isPalindrome(int x) {
        if(x < 0)
            return false;
        if(x < 10)
            return true;
        long long temp = 0;
        int xx = x;
        while(xx != 0){
            temp = temp * 10 + xx % 10;
            xx /= 10;
        }
        return (temp == x);
    }
};
13.罗马数字转换
class Solution {
public:
    int romanToInt(string s) {
        int tagList[128];
        tagList['I'] = 1;
        tagList['V'] = 5; 
        tagList['X'] = 10; 
        tagList['L'] = 50; 
        tagList['C'] = 100; 
        tagList['D'] = 500; 
        tagList['M'] = 1000;
        int res = 0;
        for(int i = 0; i < s.length(); ++i){
            if(i+1 >= s.length() || tagList[s[i+1]] <= tagList[s[i]]){
                res += tagList[s[i]];
            }else{
                res -= tagList[s[i]];
            }
        }   
        return res;
    }
};
14.最长公共前缀
class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
       if(strs.empty()){
           return "";
       } 
       int i;
       for(i = 0; i < strs[0].length(); ++i){
           char c = strs[0][i];
           for(string str : strs){
               if(str.length() == i || str[i] != c){
                   return str.substr(0,i);
               }
           }
       }
       return strs[0].substr(0,i);
    }
};
20.有效的括号
class Solution {
public:
    bool isValid(string s) {
        stack<char> t;
        map<char,char> cm{{')','('},{'}','{'},{']','['}};
        for(auto c : s){
            switch(c){
                case '(':
                case '{':
                case '[':
                    t.push(c);
                    break;
                case ')':
                case '}':
                case ']':
                    if(t.empty() || t.top() != cm[c]){
                        return false;
                    }
                    t.pop();
                    break;
            }
        }
        return t.empty();
    }
};
21.合并两个有序链表
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if(l1 == NULL){
            return l2;
        }else if(l2 == NULL){
            return l1;
        }else{
            if(l1->val <= l2->val){
                l1->next = mergeTwoLists(l1->next, l2);
                return l1;
            }
            else{
                l2->next = mergeTwoLists(l1, l2->next);
                return l2;
            }
        }
    }
};
26.删除排序数组中的重复项
class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        if(nums.size() == 0)
            return 0;
        int len = 1;
        for(int i = 1; i < nums.size(); ++i){
            if(nums[i] != nums[len-1]){
                nums[len] = nums[i];
                len++;
            }
        }
        return len;
    }
};
27.移除元素
class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int j = 0;
        for(int i = 0; i < nums.size(); ++i){
            if(nums[i] != val){
                nums[j++] = nums[i];
            }
        }
        return j;
    }
};
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 i = 0, j = 0;
        for(i = 0; i < haystack.size(); ++i){
            if(j == needle.size()){
                return i - j;
            }
            if(haystack[i] == needle[j]){
                j++;
            }else{
                i -= j;
                j = 0;
            }
        }
        if(j == needle.size()){
            return i-j;
        }
        return -1;
    }
};
35.搜索插入位置
class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        int left = 0;
        int right = nums.size() - 1;
        while(left <= right){
            int mid = (left + right) / 2;
            if(nums[mid] == target){
                return mid;
            }else if(nums[mid] < target){
                left = mid + 1;
            }else{
                right = mid - 1;
            }
        }
        return left;
    }
};
38.外观数列 (报数)
class Solution {
public:
    string countAndSay(int n) {
        if(n <= 0){
            return "";
        }
        string res = "1";
        while(--n){
            string cur = "";
            for(int i = 0; i < res.size(); ++i){
                int cnt = 1;
                while((i+1) < res.size() && res[i] == res[i+1]){
                    cnt++;
                    i++;
                }
                cur += to_string(cnt) + res[i];
            }
            res = cur;
        }
        return res;
    }
};
53.最大子序和
class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int sum = nums[0];
        int n = nums[0];
        for(int i = 1; i < nums.size(); ++i){
            if(n < 0){
                n = nums[i];
            }else{
                n += nums[i];
            }
            if(sum < n){
                sum = n;
            }
        }
        return sum;
    }
};
58.最后一个单词的长度
class Solution {
public:
    int lengthOfLastWord(string s) {
       int res = 0;
       int len = s.size();
       if(len == 0)
            return res;
        int index = len -1;
        while(index >= 0 && s[index]==' ')
            index--; //trim功能,排除两端的空格
        for(int i=index;i>=0;i--){
            if(s[i] !=' ')
                res++;
            else
                break;
        }         
        return res;
    }
};
66.加一
class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int len = digits.size();
        for(int i = len - 1; i >= 0; --i){
            if(digits[i] == 9){
                digits[i] = 0;
            }else{
                digits[i]++;
                return digits;
            }
        }
        digits[0] = 1;
        digits.push_back(0);
        return digits;
    }
};
67.二进制求和
class Solution {
public:
    string addBinary(string a, string b) {
        string res = "";
        int m = a.size() - 1;
        int n = b.size() - 1;
        int carry = 0;
        while(m >= 0 || n >= 0){
            int p = m >= 0 ? a[m--] - '0' : 0;
            int q = n >= 0 ? b[n--] - '0' : 0;
            int sum = p + q + carry;
            res = to_string(sum % 2) + res;
            carry = sum / 2;
        }
        return carry == 1 ? "1" + res : res;
    }
};
69.x的平方根:经典的二分法思想
class Solution {
public:
    int mySqrt(int x) {
        if(x == 1)
            return 1;
        if(x == 0)
            return 0;
        if(x < 0)
            return -1;

        int left = 0, right = x;
        int mid;
        while(left < right){
            mid = (left + right) >> 1;
            if(x / mid == mid){
                return mid;
            }else if(x / mid < mid){
                right = mid - 1;
            }else{
                left = mid + 1;
            }
        }
        return x / left < left ? left - 1 : left;
    }
};
70.爬楼梯 斐波那契数列 递归转循环
class Solution {
public:
    int climbStairs(int n) {
        if(n == 0 || n == 1)
            return 1;
        if(n == 2)
            return 2;
        
        int sum = 2;
        int num_1 = 1;
        int num_2 = 2;
        for(int i = 2; i < n; ++i){
            int tmp = num_2;
            num_2 += num_1;
            num_1 = tmp;
        }
        return num_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)
            return head;
        ListNode *cur = head;
        while(cur->next){
            if(cur->val == cur->next->val)
                cur->next = cur->next->next;
            else
                cur = cur->next;
        }   
        return head;
    }
};
88.合并两个有序数组
class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        int i = m - 1;
        int j = n - 1;
        int k = m + n - 1;
        while(i >= 0 && j >= 0){
            if(nums1[i] > nums2[j]){
                nums1[k--] = nums1[i--];
            }else{
                nums1[k--] = nums2[j--];
            }
        }
        while(j >= 0){
            nums1[k--] = nums2[j--];
        }
    }
};

你可能感兴趣的:(LeetCode算法简单难度)