栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Leetcode55

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Leetcode55

55. 跳跃游戏
class Solution {
public:
    bool canJump(vector& nums) {
        int k=0;//0能到达
        int n=nums.size();
        for(int i=0; i
            if(i>k) return false;
            k=max(k, nums[i]+i);
        }
        return true;
    }
};
45. 跳跃游戏II
class Solution {
public:
    int jump(vector& nums) {
        int end=0;//当前这组最右边位置
        int maxPos=0;//当前可达最远位置
        int n=nums.size();
        int k=0;//跳跃次数
        for(int i=0; i//n-1是因为最后位置前面的一组已经计算过走一次要的步数了
            maxPos=max(maxPos, i+nums[i]);
            if(i==end){
                k++;
                end=maxPos;
            }
        }
        return k;
    }
};
53. 最大子数组和
class Solution {
public:
    int maxSubArray(vector& nums) {
        int n=nums.size();
        int ans=INT_MIN;
        vector 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& nums) {
        int n=nums.size();
        int maxnum=nums[0], minnum=nums[0], ans=nums[0];
        for(int i=1; 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& nums) {
        int n=nums.size();
        if(n==1) return nums[0];
        vector dp1(n,0);
        vector dp2(n,0);
        int ans=0;
        dp1[0]=nums[0];
        for(int i=1; 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& pushed, vector& popped) {
        stack st;
        int n=pushed.size();
        int pu=0, po=0;
        while(pu
            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 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 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;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/917039.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号