Bootstrap

LeetCode打卡--Google面试题一

Google面试题一

LeetCode 14. Longest Common Prefix

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

        //找到一个最小的字符串
        for(int i = 1; i < strs.size(); i++)
        {
            minlenth = min(minlenth, int(strs[i].size()));
        }

        //然后一个一个进行比较
        for(int j = 1; j <= minlenth; j++)
        {
            char s = strs[0][j-1];
            for(int k = 1; k < strs.size(); k++)
            {
                if(s != strs[k][j-1])
                    return strs[0].substr(0, j-1);
            }
        }

        return strs[0].substr(0, minlenth);
    }
};

LeetCode 20. Valid Parentheses

class Solution {
public:
    bool isValid(string s) {
        stack<char> stk;
        for(auto c : s)
        {
            if(c == ')')
            {
                if(stk.empty() || stk.top() != '(')return false;
                stk.pop();
            }
            else if(c ==']')
            {
                if(stk.empty() || stk.top() != '[')return false;
                stk.pop();
            }
            else if(c == '}')
            {
                if(stk.empty() || stk.top() != '{')return false;
                stk.pop();
            }
            else stk.push(c);
        }
        return stk.empty();
    }
};

LeetCode 43. Multiply Strings

class Solution {
public:
    string multiply(string num1, string num2) {
        vector<int> product(num1.size() + num2.size(), 0);
        //这里是把数值先按位存储起来
        for(int i = 0; i < num1.size(); i++)
            for(int j = 0; j < num2.size(); j++)
                product[num1.size() - i + num2.size() - j - 2] += (num1[i] - '0')*(num2[j] - '0');

        //这个是处理进位
        int t = 0;
        for(int i = 0; i < product.size(); i++)
        {
            int &x = product[i];
            t += x;
            x = t % 10;
            t /= 10;
        }

        string res;
        //去除前导零
        int k = product.size() - 1;
        while(!product[k] && k > 0)k--;
        for(int i = k; i >= 0; i--)res += to_string(product[i]);
        return res;
    }
};

LeetCode 48. Rotate Image

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {

        //45度进行翻转
        for(int i = 0; i < matrix.size(); i++)
            for(int j = 0; j < i; j++)
                swap(matrix[i][j], matrix[j][i]);

        //水平翻转
        for(int i = 0; i < matrix.size(); i++)
            for(int j = 0, k = matrix[i].size() - 1; j < k; j++, k--)
                swap(matrix[i][j], matrix[i][k]);
    }
};

LeetCode 31. Next Permutation

class Solution {
public:
    void nextPermutation(vector<int>& nums) {
        //遍历所有的数字
        for(int i = nums.size() - 1; i > 0; i--)
            //如果有逆序
            if(nums[i-1] < nums[i])
            {
                //从逆序中找到一个比自己大的数字
                int j = i;
                while(j + 1 < nums.size() && nums[j + 1] > nums[i-1])j++;
                //交换两个数字
                swap(nums[i-1], nums[j]);
                //再重新排下序
                reverse(nums.begin() + i, nums.end());
                return;
            }
        //如果没有,说明就是最大的了
        reverse(nums.begin(), nums.end());
    }
};

LeetCode 23. Merge k Sorted Lists

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        //用一个优先队列来存储
        priority_queue<pair<int, ListNode*>> heap;
        for(auto list : lists)
        {
            //因为是最大堆,所以加了一个负号,就变成最小堆了
            if(list)heap.push({-list->val, list});
        }
        ListNode * dummy = new ListNode(-1);
        auto cur = dummy;
        while(heap.size())
        {
            //取出最小值,然后该链表还有没有下一个
            auto t = heap.top();
            heap.pop();
            if(t.second->next)heap.push({-t.second->next->val, t.second->next});
            cur->next = t.second;
            cur = cur->next;
        }
        cur->next = NULL;
        return dummy->next;
    }
};

LeetCode 33. Search in Rotated Sorted Array

class Solution {
public:
    int search(vector<int>& nums, int target) {
        //数据比较小,那就直接查找就好了
        if(nums.size() < 5)
        {
            for(int i = 0; i < nums.size(); i++)
                if(nums[i] == target)
                    return i;
            return -1;
        }

        //第一次二分,查找到边界
        int left = 0, right = nums.size() - 1;
        if(nums[0] > nums.back())
        {
            int l = 0, r = nums.size() - 1;
            while(l < r)
            {
                int mid = l + r + 1 >> 1;
                if(nums[mid] >= nums[0])l = mid;
                else r = mid - 1;
            }
            if(target >= nums[0])left = 0, right = l;
            else left = l + 1, right = nums.size() - 1;
        }

        //因为二分是针对有序数据的,所以第二次二分
        while(left < right)
        {
            int mid = left + right >> 1;
            if(nums[mid] < target)left = mid + 1;
            else right = mid;
        }

        if(nums[left] == target)return left;
        return -1;
    }
};

LeetCode 11. Container With Most Water

class Solution {
public:
    int maxArea(vector<int>& height) {
        int res = 0;
        for(int l = 0, r = height.size() - 1; l < r;)
        {
            int left = height[l], right = height[r];
            res = max(res, (r-l)*min(left, right));
            if(left < right)l++;
            else r--;
        }
        return res;
    }
};

LeetCode 41. First Missing Positive

class Solution {
public:
    int firstMissingPositive(vector<int>& nums) {
        for(int i = 0; i < nums.size(); i++)
            while(nums[i] >= 1 && nums[i] <= nums.size() && nums[nums[i]-1] != nums[i])
                swap(nums[i], nums[nums[i] - 1]);

        for(int i = 0; i < nums.size(); i++)
            if(nums[i] != i + 1)
                return i + 1;

        return nums.size() + 1; 
    }
};

LeetCode 25. Reverse Nodes in k-Group

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        //使用虚拟节点
        ListNode * dummy = new ListNode(-1);
        dummy->next = head;
        auto cur = dummy;
        //链表题一般需要画图,需要注意的细节比较多
        while(cur)
        {
            int s = 0;
            for(auto i = cur->next; i;i = i->next)s++;
            if(s < k)break;

            s = 0;
            auto a = cur->next, b = a->next;
            while(s < k - 1)
            {
                s++;
                auto c = b->next;
                b->next = a;
                a = b, b = c;
            }

            auto p = cur->next;
            cur->next->next = b;
            cur->next = a;
            cur = p;
        }
        return dummy->next;
    }
};
;