简体   繁体   English

为什么我在 leetcode 上收到 AddressSanitizer: heap-buffer-overflow on address 0x602000000058 错误?

[英]Why am I getting AddressSanitizer: heap-buffer-overflow on address 0x602000000058 error on leetcode?

I have read few other answers and checked a blog on codeforces.我已经阅读了一些其他答案并查看了关于 codeforces 的博客。 All suggest that it must be some potential overflow.所有人都认为它一定是一些潜在的溢出。 I have tested it for all the testcases from n = 1 to n = 45. I don't see that overflow.我已经对从 n = 1 到 n = 45 的所有测试用例进行了测试。我没有看到溢出。

class Solution {
public:
    int checkSteps(int n, vector<int>&cache){
       if(n <= 0)
         return cache[0];
       else if(n == 1){
           return cache[1];
       }
       else if(n == 2){
           return cache[2];
       }
       if(cache[n] > 0) return cache[n];
       cache[n] = checkSteps(n-1, cache) + checkSteps(n-2, cache);
        return cache[n];
    }

   int climbStairs(int n){
           vector<int> cache(n+1, 0);
           cache[0] = 0;
           cache[1] = 1;
           cache[2] = 2;
          int result = checkSteps(n, cache);
           return result;

   }

You can also use the Fib Number formula ( Golden Ratio ) for this problem, will get accepted:您也可以使用 Fib 数公式(黄金比例)来解决这个问题,将被接受:

struct Solution {
    static const inline int climbStairs(const int n) {
        double ways = 1 / pow(5, 0.5) * (pow((1 + pow(5, 0.5)) / 2, -~n) - pow((1 - pow(5, 0.5)) / 2, -~n));
        return (int) ways;
    }
};
  • -~n is simply ( n + 1 ), just a bit shorter -~n很简单( n + 1 ),只是短一点

or based on your approach, we would just iterate:或根据您的方法,我们将迭代:

struct Solution {
    static const inline int climbStairs(const int n) {
        int first = 1;
        int second = 0;
        int ways = 0;

        for (int iter = 0; iter < n; iter++) {
            ways = first + second;
            second = first;
            first = ways;
        }

        return ways;
    }
};

References参考

  • For additional details, you can see the Discussion Board .有关其他详细信息,您可以查看讨论板 There are plenty of accepted solutions with a variety of languages and explanations, efficient algorithms, as well as asymptotic time / space complexity analysis 1 , 2 in there.有许多公认的解决方案,其中包含各种语言和解释、高效算法以及渐近时间/空间复杂度分析12

If you are preparing for interviews :如果你正在准备面试

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LeetCode为什么会给出错误:AddressSanitizer:堆缓冲区溢出? - Why does LeetCode give the ERROR : AddressSanitizer: heap-buffer-overflow? AddressSanitizer:地址上的堆缓冲区溢出 - AddressSanitizer: heap-buffer-overflow on address 错误:AddressSanitizer:地址 X 上的堆缓冲区溢出 pc Y bp Z sp W - ERROR: AddressSanitizer: heap-buffer-overflow on address X at pc Y bp Z sp W 地址上的堆缓冲区溢出 - heap-buffer-overflow on address AddressSanitizer:堆缓冲区溢出在地址 0x6020000000b4 在 pc 0x0000003a86fc bp 0x7ffeebd5f9d0 sp 0x7ffeebd5f9c8 - AddressSanitizer: heap-buffer-overflow on address 0x6020000000b4 at pc 0x0000003a86fc bp 0x7ffeebd5f9d0 sp 0x7ffeebd5f9c8 错误:AddressSanitizer:地址 0x6020000001d4 在 pc 0x0000003a3e3e bp 0x7fffdafb66c0 sp 0x7fffdafb66b8 上的堆缓冲区溢出 - ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6020000001d4 at pc 0x0000003a3e3e bp 0x7fffdafb66c0 sp 0x7fffdafb66b8 C ++在我的计算机上工作正常但在leetcode上获取地址清理程序堆缓冲区溢出错误 - C++ works fine at my computer but gets address sanitizer heap-buffer-overflow errors on leetcode C++ 中的 LeetCode 417 解决方案。 我收到堆缓冲区溢出错误 - LeetCode 417 solution in C++. I'm getting a heap buffer overflow error 为什么我收到堆缓冲区溢出运行时错误 - Why am I receiving a heap buffer overflow runtime error 应该如何阅读堆缓冲区溢出错误消息? - How should the heap-buffer-overflow error message be read?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM