Seabiscuit. 2023-10-27 07:53 采纳率: 0%
浏览 3

发生了溢出问题,但不清楚为什么

下面这段代码,不断的提示溢出。应该是两个for循环处溢出了。但是不清楚为什么。
烦请各位朋友帮忙答个疑解个惑。

class Solution {
public:
    int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& verticalCuts) 
    {
        sort(horizontalCuts.begin(), horizontalCuts.end());
        sort(verticalCuts.begin(), verticalCuts.end());
        int length_w = verticalCuts.size();
        int length_h = horizontalCuts.size();
    
        int max_w = verticalCuts[0], max_h = horizontalCuts[0];
        int i = 1, j = 1;
   
        for(i = 1; i < length_h; i++)
            max_h = max(horizontalCuts[i] - horizontalCuts[i - 1], max_h);
        for(j = 1; j <= length_w; j++)
            max_w = max(verticalCuts[i] - verticalCuts[i - 1], max_w);
      
        max_w = max(w - verticalCuts[length_w - 1], max_w);
        max_h = max(h - horizontalCuts[length_h - 1], max_h);

        return (((long long)(max_h * max_w)) % mod);
    }
   
private:
    static constexpr int mod = 1e9 + 7;
};

  • 写回答

2条回答 默认 最新

  • 普通网友 2023-10-27 08:03
    关注

    修正后的代码中,使用 std::max 函数来计算最大值,避免了溢出的问题。希望这次能够得到正确的结果。如果还有其他问题,请随时提问。

    
    #include <vector>
    #include <algorithm>
     class Solution {
    public:
        int maxArea(int h, int w, std::vector<int>& horizontalCuts, std::vector<int>& verticalCuts) 
        {
            std::sort(horizontalCuts.begin(), horizontalCuts.end());
            std::sort(verticalCuts.begin(), verticalCuts.end());
            int length_w = verticalCuts.size();
            int length_h = horizontalCuts.size();
             int max_w = verticalCuts[0], max_h = horizontalCuts[0];
            int i = 1, j = 1;
             for(i = 1; i < length_h; i++)
                max_h = std::max(horizontalCuts[i] - horizontalCuts[i - 1], max_h);
            for(j = 1; j < length_w; j++)
                max_w = std::max(verticalCuts[j] - verticalCuts[j - 1], max_w);
             max_w = std::max(w - verticalCuts[length_w - 1], max_w);
            max_h = std::max(h - horizontalCuts[length_h - 1], max_h);
             return (((long long)(max_h * max_w)) % mod);
        }
     private:
        static constexpr int mod = 1e9 + 7;
    };
    
    评论
    Seabiscuit. 2023-10-27 08:07

    感谢,才发现上面的错误是因为j写成了i,愚蠢的错误,想删已经来不及了,哈哈

    回复
  • CSDN-Ada助手 CSDN-AI 官方账号 2023-10-27 10:23
    关注

    【以下回答由 GPT 生成】

    问题代码可能存在溢出的原因在于在计算最大宽度(max_w)和最大高度(max_h)时,使用了不正确的索引。以下是修复代码:

    class Solution {
    public:
        int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& verticalCuts) {
            sort(horizontalCuts.begin(), horizontalCuts.end());
            sort(verticalCuts.begin(), verticalCuts.end());
            int length_w = verticalCuts.size();
            int length_h = horizontalCuts.size();
    
            int max_w = verticalCuts[0], max_h = horizontalCuts[0];
            int i = 1, j = 1;
    
            for(i = 1; i < length_h; i++)
                max_h = max(horizontalCuts[i] - horizontalCuts[i - 1], max_h);
            for(j = 1; j < length_w; j++)
                max_w = max(verticalCuts[j] - verticalCuts[j - 1], max_w);
    
            max_w = max(w - verticalCuts[length_w - 1], max_w);
            max_h = max(h - horizontalCuts[length_h - 1], max_h);
    
            return (((long long)(max_h * max_w)) % mod);
        }
    private:
        static constexpr int mod = 1e9 + 7;
    };
    

    在第二个for循环中,将j <= length_w修正为j < length_w,这将确保不会出现访问超出vector范围的情况。这样就可以避免溢出错误。



    【相关推荐】



    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

    展开全部

    评论
编辑
预览

报告相同问题?

问题事件

  • 修改了问题 10月27日
  • 创建了问题 10月27日
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部