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

24 篇文章 0 订阅
17 篇文章 0 订阅
GitHub链接:https://github.com/WilliamWuLH/LeetCode
如果你觉得不错可以 ⭐Star 和 Fork ❤

155.Min Stack

存储最小值 + 不断更新最小值:
class MinStack {
private:
    stack<pair<int,int>> minstack;
public:
    /** initialize your data structure here. */
    MinStack() {
    }
    
    void push(int x) {
        if(minstack.empty()){
            pair<int,int> temp(x,x);
            minstack.push(temp);
        }
        else{
            int cur = min(minstack.top().first, x);
            pair<int,int> 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<int> minstack;
    stack<int> 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<int>& nums) {
        int len = nums.size();
        map<int, int> m;
        for(int i=0; i<len; i++){
            m[ nums[i] ]++;
            if(m[ nums[i] ] > len/2)
                return nums[i];
        }
        return -1;
    }
};
排序取中间:
class Solution {
public:
    int majorityElement(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        return nums[ nums.size()/2 ];
    }
};
Moore 摩尔投票法:
class Solution {
public:
    int majorityElement(vector<int>& 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<int>& 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<len; i++){
            dp[i] = max(dp[i-1], dp[i-2]+nums[i]);
        }
        return dp[len-1];
    }
};

200.Number of Islands

深度优先搜索 DFS:
class Solution {
public:
    int dic[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
    int numIslands(vector<vector<char>>& 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<v_len; i++){
            for(int j=0; j<h_len; j++){
                if(grid[i][j] == '1'){
                    ans++;
                    grid[i][j] = 'v';
                    int pos[2] = {i, j};
                    dfs(pos, grid);
                }
            }
        }
        return ans;
    }
    void dfs(int pos[], vector<vector<char>>& 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;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值