leetcode贪心算法题总结(二)

本节目录

1.最长回文串

最长回文串

cpp 复制代码
class Solution {
public:
    int longestPalindrome(string s) {
    //计数一:用数组模拟哈希表
        int hash[127] = {0};
        for(auto x:s)
        {
            hash[x]++;
        }
		//统计结果
        int ret = 0;
        for(auto x:hash)
        {
            ret += x/2*2;
        }
        return ret<s.size()?ret+1:ret;
    }
};

2.增减字符串匹配

增减字符串匹配

cpp 复制代码
class Solution {
public:
    vector<int> diStringMatch(string s) {
        //贪心
        //遇到I,选择当前能选择的最小的数
        //遇到D,选择当前能选择的最大的数
        int left = 0,right = s.size();
        vector<int> ret;
        for(auto ch:s)
        {
            if(ch == 'I') ret.push_back(left++);
            else ret.push_back(right--);
        }
        ret.push_back(left);
        return ret;
    }
};

3.分发饼干

分发饼干

cpp 复制代码
class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        int ret = 0,m = g.size(),n = s.size();
        sort(g.begin(),g.end());
        sort(s.begin(),s.end());
        for(int i=0,j=0;i<m&&j<n;i++,j++)
        {
            while(j<n&&s[j]<g[i]) j++;
            if(j<n) ret++;
        }
        return ret;
    }
};

4.最优除法

最优除法

cpp 复制代码
class Solution {
public:
    string optimalDivision(vector<int>& nums) {
        //贪心+找规律
        int n = nums.size();
        if(n == 1) return to_string(nums[0]);
        if(n == 2) return to_string(nums[0])+'/'+to_string(nums[1]);
        string str = to_string(nums[0])+"/("+to_string(nums[1]);
        for(int i=2;i<n;i++)
        {
            str+='/'+to_string(nums[i]);
        }
        str +=')';
        return str;
    }
};

5.跳跃游戏II

跳跃游戏II

cpp 复制代码
class Solution {
public:
    int jump(vector<int>& nums) {
        //使用层序遍历的思想,一层一层往后跳
        //maxPos表示下一层最右端点的下标
        int left = 0,right = 0,maxPos = 0,ret = 0,n = nums.size();
        while(left<=right)
        {
            if(maxPos>=n-1) return ret;//判断能否跳到最后一个位置
            //遍历当前层
            for(int i=left;i<=right;i++)
            {
                maxPos = max(maxPos,nums[i]+i);
            }
            left = right+1;
            right = maxPos;
            ret++;
        }
        return -1;
    }
};

6.跳跃游戏

跳跃游戏

cpp 复制代码
class Solution {
public:
    bool canJump(vector<int>& nums) {
        //跟上一题跳跃游戏II思路一模一样
        //一层一层往后跳,层序遍历
        int left = 0,right = 0,maxPos = 0,n =nums.size();
        while(left<=right)
        {
            if(maxPos>=n-1) return true;
            for(int i=left;i<=right;i++)
            {
                maxPos = max(maxPos,nums[i]+i);
            }
            left = right+1;
            right = maxPos;
        }
        return false;
    }
};

7.加油站

加油站

cpp 复制代码
class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        //解法一:暴力枚举 O(n^2) 会超时
        int n = gas.size();
        int step = n;
        for(int i=0;i<n;i++)
        {
            int rest = 0;
            for(int step=0;step<n;step++)
            {
                int index = (i+step)%n;
                rest = rest+gas[index]-cost[index];
                if(rest<0) break;
            }
            if(rest>=0) return i;
        }
        return -1;
    }
};
cpp 复制代码
class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        //解法二:找规律+在解法一的基础上稍作改动
        int n = gas.size();
        for(int i=0;i<n;i++)
        {
            int rest = 0;
            int step = 0;
            for(;step<n;step++)
            {
                int index = (i+step)%n;
                rest = rest+gas[index]-cost[index];
                if(rest<0) break;
            }
            if(rest>=0) return i;
            i = i+step;
        }
        return -1;
    }
};

8.单调递增的数字

单调递增的数字

cpp 复制代码
class Solution {
public:
    int monotoneIncreasingDigits(int n) {
        //找规律
        string s = to_string(n);
        int i=0,m = s.size();
        //找到第一个递减的位置
        while(i+1<m && s[i]<=s[i+1]) i++;
        if(i+1 == m) return n;
        //回推
        while(i-1>=0 && s[i]==s[i-1]) i--;
        s[i]--;
        for(int j=i+1;j<m;j++)
        {
            s[j]='9';
        }
        return stoi(s);
    }
};

9.坏了的计算器

坏了的计算器

cpp 复制代码
class Solution {
public:
    int brokenCalc(int startValue, int target) {
        //正难则反+贪心
        //从end->begin *->/ -1->+1
        int ret = 0;
        while(target>startValue)
        {
            if(target%2 == 0) target/=2;
            else target += 1;
            ret++;
        }
        return ret+startValue-target;
    }
};
相关推荐
福大大架构师每日一题4 小时前
go-zero v1.9.3 版本更新:一致性哈希负载均衡、gRPC优化、链路追踪修复、ORM完善等重要提升
golang·负载均衡·哈希算法
努力学算法的蒟蒻6 小时前
day27(12.7)——leetcode面试经典150
算法·leetcode·面试
CoderYanger7 小时前
动态规划算法-子序列问题(数组中不连续的一段):28.摆动序列
java·算法·leetcode·动态规划·1024程序员节
有时间要学习8 小时前
面试150——第二周
数据结构·算法·leetcode
小白程序员成长日记11 小时前
2025.12.03 力扣每日一题
算法·leetcode·职场和发展
元亓亓亓11 小时前
LeetCode热题100--20. 有效的括号--简单
linux·算法·leetcode
熊猫_豆豆12 小时前
LeetCode 49.字母异位组合 C++解法
数据结构·算法·leetcode
繁华似锦respect13 小时前
lambda表达式中的循环引用问题详解
java·开发语言·c++·单例模式·设计模式·哈希算法·散列表
小武~13 小时前
Leetcode 每日一题C 语言版 -- 234 basic calculator
linux·c语言·leetcode
小白程序员成长日记13 小时前
2025.12.02 力扣每日一题
数据结构·算法·leetcode