Leetcode55_45_53_152_198_JZ31_2_6

本文探讨了五道经典算法题目:跳跃游戏系列、最大子数组和、乘积最大子数组、动态规划应用(打家劫舍)及栈操作(验证序列)。通过实例解析,展示了如何使用C++实现并优化解决方案,涉及数据结构如数组、栈和链表,以及常见问题如动态规划和递归技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

55. 跳跃游戏

class Solution {
public:
    bool canJump(vector<int>& nums) {
        int k=0;//0能到达
        int n=nums.size();
        for(int i=0; i<n; ++i){
            if(i>k) return false;
            k=max(k, nums[i]+i);
        }
        return true;
    }
};

45. 跳跃游戏II

class Solution {
public:
    int jump(vector<int>& nums) {
        int end=0;//当前这组最右边位置
        int maxPos=0;//当前可达最远位置
        int n=nums.size();
        int k=0;//跳跃次数
        for(int i=0; i<n-1; ++i){//n-1是因为最后位置前面的一组已经计算过走一次要的步数了
            maxPos=max(maxPos, i+nums[i]);
            if(i==end){
                k++;
                end=maxPos;
            }
        }
        return k;
    }
};

53. 最大子数组和

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int n=nums.size();
        int ans=INT_MIN;
        vector<int> dp(n+1,0);
        for(int i=1; i<=n; ++i){
            dp[i]=max(dp[i-1]+nums[i-1], nums[i-1]);
            ans=max(ans, dp[i]);
        }
        return ans;
    }
};

152. 乘积最大子数组

class Solution {
public:
    int maxProduct(vector<int>& nums) {
        int n=nums.size();
        int maxnum=nums[0], minnum=nums[0], ans=nums[0];
        for(int i=1; i<n; ++i){
            int mx=maxnum, mn=minnum;//如果直接用maxnum,minnum,下面算minnum的时候代的是更新后的maxnum
            maxnum=max(max(mx*nums[i], mn*nums[i]),nums[i]);
            minnum=min(min(mx*nums[i], mn*nums[i]),nums[i]);
            ans=max(maxnum, ans);
        }
        return ans;
        //return ans;
    }
};

198. 打家劫舍

dp1[i]是当前i要偷得到的最大金额
dp2[i]是当前i不偷得到的最大金额

class Solution {
public:
    int rob(vector<int>& nums) {
        int n=nums.size();
        if(n==1) return nums[0];
        vector<int> dp1(n,0);
        vector<int> dp2(n,0);
        int ans=0;
        dp1[0]=nums[0];
        for(int i=1; i<n; ++i){
            dp1[i]=dp2[i-1]+nums[i];
            dp2[i]=max(dp1[i-1], dp2[i-1]);
            ans=max(max(dp1[i], dp2[i]), ans);
        }
        return ans;
    }
};

剑指 Offer 31. 栈的压入、弹出序列

class Solution {
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
        stack<int> st;
        int n=pushed.size();
        int pu=0, po=0;
        while(pu<n){
            st.push(pushed[pu]);
            while(!st.empty() && st.top()==popped[po]){
                st.pop();
                po++;
            }
            pu++;
        }
        if(st.empty()) return true;
        else return false;
    }
};

2. 两数相加

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int ans=0;
        ListNode* dummyhead=new ListNode(0, nullptr);
        ListNode* head=dummyhead;
        ListNode* cur1=l1, *cur2=l2;
        int flag=0;
        while(cur1 && cur2){
            int he=cur1->val+cur2->val;
            he+=flag;
            if(he<10){
                head->next=new ListNode(he, nullptr);
                head=head->next;
                flag=0;
            }
            else{
                head->next=new ListNode(he-10, nullptr);
                head=head->next;
                flag=1;
            }
            cur1=cur1->next;
            cur2=cur2->next;
        }
        while(cur1){
            int he=cur1->val;
            he+=flag;
            if(he<10){
                head->next=new ListNode(he, nullptr);
                head=head->next;
                flag=0;
            }
            else{
                head->next=new ListNode(he-10, nullptr);
                head=head->next;
                flag=1;
            }
            cur1=cur1->next;
        }
        while(cur2){
            int he=cur2->val;
            he+=flag;
            if(he<10){
                head->next=new ListNode(he, nullptr);
                head=head->next;
                flag=0;
            }
            else{
                head->next=new ListNode(he-10, nullptr);
                head=head->next;
                flag=1;
            }
            cur2=cur2->next;
        }
        if(flag==1){
            head->next=new ListNode(1, nullptr);
        }
        return dummyhead->next;
    }
};

减少冗余

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* dummyhead=new ListNode(0, nullptr);
        auto head=dummyhead;
        int flag=0;
        while(l1 || l2){
            int n1=l1?l1->val:0;
            int n2=l2?l2->val:0;
            int he=n1+n2+flag;
            if(he>=10) flag=1;
            else flag=0;
            he=he%10;
            head->next=new ListNode(he, nullptr);
            head=head->next;
            if(l1) l1=l1->next;
            if(l2) l2=l2->next;
        }
        if(flag==1) head->next=new ListNode(1, nullptr);
        return dummyhead->next;
    }
};

6. Z 字形变换

class Solution {
public:
    string convert(string s, int numRows) {
        if(numRows<2) return s;
        int flag=-1;
        vector<string> v(numRows);
        int i=0;
        for(auto ch:s){
            if(i==0 || i==numRows-1){
                flag=-flag;
            }
            v[i]+=ch;
            i+=flag;
        }
        string ans;
        for(auto str:v){
            ans+=str;
        }
        return ans;
    }
};

宏定义面试题

unordered_map<char, string> mp = {
    {'A', "Bcd"},{'B', "bc"},{'D',"efg"}
};

int main() {
    string s = "ABD";
    string s1=s;
    int n = s.size();
    int k = 0;
    while (k < n) {
        if (s1[k] >= 'A' && s1[k] <= 'Z') {
            s1 = s1.substr(0, k) + mp[s1[k]] + s1.substr(k + 1);
            //cout << s1 << endl;
            n = s1.size();
        }
        else k++;
    }
    cout << s1;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值