LeetCode 力扣 刷题记录 热题 HOT 100(155,160,169,198,200)题目+算法分析+Cpp解答

GitHub链接:https://github.com/WilliamWuLH/LeetCode

如果你觉得不错可以 ⭐Star 和 Fork ❤

155.Min Stack

存储最小值 + 不断更新最小值:

class MinStack {
private:
    stack> minstack;
public:
    /** initialize your data structure here. */
    MinStack() {
    }
    
    void push(int x) {
        if(minstack.empty()){
            pair temp(x,x);
            minstack.push(temp);
        }
        else{
            int cur = min(minstack.top().first, x);
            pair temp(cur,x);
            minstack.push(temp);
        }
    }
    
    void pop() {
        minstack.pop();
    }
    
    int top() {
        return minstack.top().second;
    }
    
    int getMin() {
        return minstack.top().first;
    }
};

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack* obj = new MinStack();
 * obj->push(x);
 * obj->pop();
 * int param_3 = obj->top();
 * int param_4 = obj->getMin();
 */

使用辅助栈存储最小值:

class MinStack {
private:
    stack minstack;
    stack numstack;
public:
    /** initialize your data structure here. */
    MinStack() {
    }
    
    void push(int x) {
        numstack.push(x);
        if(minstack.empty())
            minstack.push(x);
        else{
            int temp = min(minstack.top(), x);
            minstack.push(temp);
        }
    }
    
    void pop() {
        numstack.pop();
        minstack.pop();
    }
    
    int top() {
        return numstack.top();
    }
    
    int getMin() {
        return minstack.top();
    }
};

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack* obj = new MinStack();
 * obj->push(x);
 * obj->pop();
 * int param_3 = obj->top();
 * int param_4 = obj->getMin();
 */

160.Intersection of Two Linked Lists

双指针 + 数学推导:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode * posA = headA;
        ListNode * posB = headB;
        while(posA != posB){
            posA = posA == NULL ? headA : posA->next;
            posB = posB == NULL ? headB : posB->next;
        }
        return posA;
    }
};

169.Majority Element

哈希计数:

class Solution {
public:
    int majorityElement(vector& nums) {
        int len = nums.size();
        map m;
        for(int i=0; i len/2)
                return nums[i];
        }
        return -1;
    }
};

排序取中间:

class Solution {
public:
    int majorityElement(vector& nums) {
        sort(nums.begin(), nums.end());
        return nums[ nums.size()/2 ];
    }
};

Moore 摩尔投票法:

class Solution {
public:
    int majorityElement(vector& nums) {
        int count = 0, ans;
        for(auto n : nums){
            if(count == 0)
                ans = n;
            if(ans == n)
                count++;
            else
                count--;
        }
        return ans;
    }
};

198.House Robber

动态规划:

class Solution {
public:
    int rob(vector& nums) {
        int len = nums.size();
        if(len == 0)
            return 0;
        if(len == 1)
            return nums[0];
        int dp[len];
        dp[0] = nums[0];
        dp[1] = max(nums[0], nums[1]);
        for(int i=2; i

200.Number of Islands

深度优先搜索 DFS:

class Solution {
public:
    int dic[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
    int numIslands(vector>& grid) {
        int ans = 0;
        int v_len = grid.size();
        if(v_len == 0)
            return 0;
        int h_len = grid[0].size();
        for(int i=0; i>& grid){
        for(int i=0; i<4; i++){
            int now[2] = {pos[0]+dic[i][0], pos[1]+dic[i][1]};
            if(now[0] >= 0 && now[0] < grid.size() && 
                now[1] >= 0 && now[1] < grid[0].size()){
                if(grid[ now[0] ][ now[1] ] == '1'){
                    grid[ now[0] ][ now[1] ] = 'v';
                    dfs(now, grid);
                }
            }
        }
        return;
    }
};

你可能感兴趣的:(算法,LeetCode,C++)