苏丛 发表于 2019-10-5 15:45:40

leetcode 88题

#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
      int p1=m-1; // nums1未检查过的元素中最大的
      int p2=n-1; // nums2未检查过的元素中最大的

      // i为当前查看的元素
      for(int i=m+n-1; i>=0; i--){
            if(nums1 > nums2 or p2<0){
                nums1 = nums1;
            }
            else{
                nums1 = nums2;
            }
      }
    }
};



int main(){
    int arr1[] = {1,2,4,4,0,0,0};
    vector<int> vec1(arr1, arr1+sizeof(arr1)/sizeof(int));

    int arr2[] = {2,5,6};
    vector<int> vec2(arr2, arr2+sizeof(arr2)/sizeof(int));
   
    Solution().merge(vec1,4,vec2,3);

    for (int i = 0; i < vec1.size(); i++)
    {
      cout<<vec1<<" ";
    }
}
将两个数组合并, 在我的机器上跑没问题, 在leetcode上死活Runtime Error

题目大概是这样:
Example:

Input:
nums1 = , m = 3
nums2 = ,       n = 3

Output:

https://leetcode.com/problems/merge-sorted-array/
求大佬指教

Seawolf 发表于 2019-10-6 07:29:37

runtime error 也会有提示的,是哪个case 没有通过

苏丛 发表于 2019-10-6 10:10:39

Seawolf 发表于 2019-10-6 07:29
runtime error 也会有提示的,是哪个case 没有通过

我那个example就通过不了
报的好像是内存的错误
Finished in N/A
=================================================================
==29==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6020000000ac at pc 0x00000040f85b bp 0x7ffffa5dcd10 sp 0x7ffffa5dcd08
READ of size 4 at 0x6020000000ac thread T0
    #1 0x7f94d173d2e0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202e0)
0x6020000000ac is located 4 bytes to the left of 16-byte region [0x6020000000b0,0x6020000000c0)
allocated by thread T0 here:
    #0 0x7f94d3162ce0 in operator new(unsigned long) (/usr/local/lib64/libasan.so.5+0xe9ce0)
    #4 0x7f94d173d2e0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202e0)
Shadow bytes around the buggy address:
0x0c047fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff8000: fa fa fd fa fa fa fd fa fa fa fd fd fa fa fd fa
=>0x0c047fff8010: fa fa fd fa fa00 00 fa fa fa fa fa fa fa fa
0x0c047fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable:         00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone:       fa
Freed heap region:       fd
Stack left redzone:      f1
Stack mid redzone:       f2
Stack right redzone:   f3
Stack after return:      f5
Stack use after scope:   f8
Global redzone:          f9
Global init order:       f6
Poisoned by user:      f7
Container overflow:      fc
Array cookie:            ac
Intra object redzone:    bb
ASan internal:         fe
Left alloca redzone:   ca
Right alloca redzone:    cb
==29==ABORTING

bin554385863 发表于 2019-10-6 13:07:01

本帖最后由 bin554385863 于 2019-10-6 13:52 编辑

这个题是考察对容器操作的掌握
如果能熟练操作容器相关函数以及排序算法,这个问题一点都不难!
另外对容器增加元素时最好使用push_back()函数,而不是使用下标.
#include <iostream>
#include<algorithm>
#include <vector>
void merge(std::vector<int> &nums1, int m, std::vector<int> &nums2, int n)
{
    if( (m > nums1.size())||(n > nums2.size()))
    {
      exit(0);
    }
    nums1.erase(nums1.begin() + m, nums1.end());//擦除元素erase(begin, end)/erase(pos)函数
    nums2.erase(nums2.begin() + n, nums2.end());//擦除元素
    for(int i:nums2)
    {
      nums1.push_back(i);//合并容器
    }
    sort(nums1.begin(), nums1.end());//排序sort(begin, end, less<datetype>(降序)/greater<datetype>(升序))
}
int main(int argc, char const *argv[])
{
    std::vector<int> i = {0,1,2,3,9,10,11,0,0,0,0};
    std::vector<int> j = {4,5,6,7,23,54,0,0,0,0};
    merge(i,3,j,2);
    for(int k:i)
    {
      std::cout<<k;
    }
    return 0;
}
---------------------------------------------------------------------------------------
E:\Users\86184\Documents\Code>c:\Users\86184\.vscode\extensions\ms-vscode.cpptools-0.25.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-h1fynkur.lkj --stdout=Microsoft-MIEngine-Out-5krajmjw.k3n --stderr=Microsoft-MIEngine-Error-hbeytjdn.g0o --pid=Microsoft-MIEngine-Pid-twavuu23.nog "--dbgExe=E:\My Program\MinGW\bin\gdb.exe" --interpreter=mi
01245
E:\Users\86184\Documents\Code>
------------------------------------------------------------------------
执行结果:
通过
显示详情
执行用时 :
8 ms
, 在所有 C++ 提交中击败了
77.81%
的用户
内存消耗 :
8.6 MB
, 在所有 C++ 提交中击败了
89.69%
的用户

容器最基本的操作增察删改
页: [1]
查看完整版本: leetcode 88题