InterviewBit:提交时出现 运行 时间错误,但自定义测试用例的输出正确

InterviewBit: Giving run time error on submit but correct output on custom test case

在 interviewBit 上解决了一个问题,这里是问题的 URL: https://www.interviewbit.com/problems/first-missing-integer/

下面是我对上述问题的解答。它通过了测试用例,但在提交时给出了 运行时错误。我使用测试用例(导致运行时错误)作为自定义输入,它给了我正确的答案,但在提交时对相同的输入给出了错误。

int Solution::firstMissingPositive(vector<int> &A) 
{
    int n = A.size();
    int temp;
    for(int i=0; i<n; i++)
    {
        while(A[i]!=0 && A[i]!=(i+1))
        {
            if(A[i]<0 || A[i]>(n+1))
            {
                A[i] = 0;
                break;
            }
            temp = A[A[i]-1];
            if(temp == A[i])
            {
                A[i] = 0;
                break;
            }
            A[A[i]-1] = A[i];
            A[i] = temp;
        }
    }

    for(int i=0; i<n; i++)
        if(A[i] == 0)
            return i+1;

    return n+1;
}

这是给出错误的输入:

A : [ 885, 49, 624, 223, 604, 290, 814, 978, 507, 801, 19, 60, 952, 858, 434, 202, 417, 222, 431, 939, 460, 563, 121, 11, 178, 498, 187, 69, 997, 693, 2, 807, 428, 146, 232, 226, 595, 888, 323, 446, 995, 572, 193, 941, 958, 110, 415, 85, 613, 672, 76, 492, 404, 512, 961, 79, 551, 830, 550, 215, 938, 495, 47, 650, 137, 684, 412, 608, 573, 688, 583, 558, 922, 977, 852, 350, 590, 540, 91, 979, 441, 651, 191, 863, 177, 519, 494, 116, 705, 93, 571, 960, 399, 733, 210, 349, 150, 715, 103, 501, 867, 985, 55, 273, 821, 837, 165, 487, 881, 70, 266, 557, 40, 175, 873, 905, 260, 470, 287, 132, 744, 379, 197, 876, 765, 764, 190, 477, 388, 546, 423, 599, 944, 0, 853, 974, 825, 12, 288, 250, 95, 815, 88, 471, 251, 575, 656, 303, 252, 33, 701, 751, 302, 908, 928, 516, 225, 56, 27, 972, 199, 696, 481, 112, 524, 510, 3, 51, 459, 576, 490, 690, 817, 936, 683, 24, 111, 98, 36, 409, 694, 544, 981, 126, 552, 10, 262, 520, 92, 826, 341, 169, 778, 168, 109, 474, 812, 537, 398, 569, 965, 525, 308, 882, 970, 97, 937, 834, 192, 703, 44, 389, 753, 188, 526, 649, 464, 681, 654, -4, 666, 851, 312, 754, 759, 829, 809, 739, 547, 108, 154, 353, 402, 602, 89, 860, 257, 380, 983, 361, 741, 297, 475, 374, 281, 641, 768, 644, 171, 596, 400, 306, 279, 879, 271, 331, 275, 345, 315, 820, 625, 700, 915, 285, 849, 186, 774, 296, 248, 736, 597, 623, 236, 72, 131, 82, 500, 966, 793, 220, 276, 143, 369, 430, 722, 685, 906, 871, 545, 914, 41, 289, 413, 757, 721, 234, 891, 847, 230, 671, 376, 844, 322, 496, 338, 54, -5, 246, 450, 463, 141, 589, 710, 270, 184, 397, 969, 621, 337, 421, 219, 676, 601, 195, 410, 235, 896, 280, 828, 709, 670, 564, 503, 788, 677, 574, 253, 585, 277, 205, 682, 484, 930, 775, 461, 691, 607, 78, 62, 362, 218, 870, 875, 925, 180, 424, 582, 803, 456, 872, 992, 122, 124, 1, 796, 580, 255, 314, 805, 493, 390, 45, 505, 486, 63, 194, 105, 777, 723, 762, 67, 927, 626, 360, 984, 617, 895, 144, 26, 810, 57, 365, 278, 300, 702, 125, 170, 868, 94, 662, 395, 344, 29, 87, 923, 577, 508, 269, 325, 902, 443, 286, 556, 200, 561, 746, 658, 562, 452, 549, 433, 203, 201, 592, 749, 634, 167, 1000, 130, 559, 536, 811, 384, 99, 675, 818, 394, 106, 310, 789, 104, 7, 994, 172, 955, 832, 209, 655, 164, 5, 633, 71, 734, 692, 378, 638, 720, 769, 976, 22, 28, 845, 932, 877, 884, 823, 163, 462, 299, 964, 699, 737, 35, 567, 328, 258, 565, 115, 779, 320, 657, 647, 594, 96, 153, 515, 282, 640, 698, 30, 856, 313, 637, 735, 136, 591, 643, 504, 748, 373, 429, 578, 618, 242, 151, 432, 848, 836, 305, 155, 513, 207, 783, 46, 921, 227, 954, 802, -2, 13, 8, 738, 465, 541, 4, 53, 90, 973, 158, 560, 497, 732, 534, 444, 58, 950, 107, 975, 133, 447, 39, 342, 364, 422, 996, 893, 479, 615, 761, 457, 396, 689, 272, 864, 368, 233, 511, 138, 478, 383, 747, 183, 947, 445, 473, 946, 483, 797, 74, 543, 943, 239, 448, 135, 321, 48, 800, 907, 245, 329, 528, 588, 850, 953, 386, 819, 653, 147, 185, 790, 743, 869, 630, 962, 440, 142, 667, 480, 145, 646, 437, 926, 318, 64, 23, 499, 911, 489, 140, 238, 241, 16, 659, 419, 901, 990, 890, 224, 42, 718, 264, 502, 77, 216, 458, 86, 123, 910, 405, 455, 317, 614, 406, 229, 772, 660, 728, 942, 841, 468, 442, 166, 372, 382, 340, 758, 714, 249, 887, 956, 668, 740, 332, 327, 17, 917, 522, 918, 127, 31, 785, 83, 326, 795, 593, 712, 292, 274, 566, 206, 548, 934, 991, 295, 542, 117, 371, 695, 50, 968, 284, 824, 449, 359, 780, 298, 533, 59, 988, 346, 356, 100, 467, 357, 265, 488, 669, 792, 605, 21, 18, 358, 897, 861, 408, 716, 173, 579, 427, 217, 530, 951, 933, 855, 181, 854, 179, 159, 606, 213, 554, 986, 827, 664, 352, 945, 244, 418, 304, 335, 711, 268, 636, 128, 102, 212, 293, 261, 411, 334, 661, 330, 900, 139, 301, 34, 706, 336, 892, 333, 663, 846, 482, 61, 598, 929, 784, 451, 231, 517, 134, 294, 899, 679, 176, 931, 527, 152, 439, 948, 766, 348, 767, 189, 20, 909, 963, 998, 538, 387, 898, 267, 628, 414, 609, 32, 904, 466, 916, 838, 756, 214, 529, 889, 454, 835, 773, 208, 570, 426, 755, 687, 407, 770, 354, 161, 425, 43, 113, 673, 752, 648, 782, 859, 523, 874, 211, 196, 612, 704, 713, 993, 535, 760, 256, 619, 81, 472, 611, 120, 865, 15, 80, 610, 539, 999, 729, 982, 989, 959, 750, 967, 149, 629, 678, 377, 118, 532, 420, 616, 833, 148, 857, 745, 75, 639, 957, 156, 391, 237, 521, 469, 518, 385, 553, 243, 416, 600, 725, 584, 631, 319, 831, 924, 940, 727, 392, 808, 581, 52, 717, 393, 886, 674, 101, 38, 6, 568, 401, 587, 367, 324, 506, 375, 436, 798, 254, 913, 307, 355, 84, 843, 9, 794, 403, 980, 791, 182, 665, 119, 311, 25, 620, 816, 381, 240, 157, 804, 263, 920, 309, 622, 160, 283, 228, 822, 726, 627, 935, 73, 878, 880, 866, 453, 291, 652, 555, 343, -3, 66, 438, 949, 485 ]

以上输入的预期输出为:

14

和错误:

Runtime Error. Your submission stopped because of a runtime error. ex: division by zero, array index out of bounds, uncaught exception You can try testing your code with custom input and try putting debug statements in your code.

solution: malloc.c:2374: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof> (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed.
Aborted

请帮忙。

这里:

        if(A[i]<0 || A[i]>(n+1)){
            A[i] = 0;
            break;
        }
        temp = A[A[i]-1];

您正在检查边界,但 A[i]==0A[i]==n+1 之间的任何内容都将通过。

现在考虑 A[i]==n+1 那么您正在访问 A[A[i]-1] == A[n],这太过分了。

我对您的代码了解太少,无法提出修复建议,但通常使用 at() 而不是 operator[] 可以帮助发现此类错误。

这不是您问题的答案。但是想向您展示更简单的方法。

看来你对这个简单的算法想得太多了。 从 1n 使用 std::binary_search() 二进制搜索给定排序向量中缺失的整数。如果没有找到,那就是你的答案。尽管这具有 O(nlongn) 的时间复杂度,但已通过所有 TC。

试试这个:

int solution(std::vector<int>& A)
{
    std::sort(A.begin(), A.end());
    int ans = 1;
    int n = A[A.size()-1]+1;
    for(int i=1; i<=n; ++i )
    {
       if ( !(std::binary_search (A.begin(), A.end(), i)) )
        {
           ans = i;
           break;
        }
    }
    return ans;
}