鱼C论坛

 找回密码
 立即注册
查看: 3280|回复: 3

leetcode 88题

[复制链接]
发表于 2019-10-5 15:45:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <iostream>
  2. #include <vector>

  3. using namespace std;

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

  9.         // i为当前查看的元素
  10.         for(int i=m+n-1; i>=0; i--){
  11.             if(nums1[p1] > nums2[p2] or p2<0){
  12.                 nums1[i] = nums1[p1--];
  13.             }
  14.             else{
  15.                 nums1[i] = nums2[p2--];
  16.             }
  17.         }
  18.     }
  19. };



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

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

  27.     for (int i = 0; i < vec1.size(); i++)
  28.     {
  29.         cout<<vec1[i]<<" ";
  30.     }
  31. }
复制代码

将两个数组合并, 在我的机器上跑没问题, 在leetcode上死活Runtime Error

题目大概是这样:
Example:

Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6],       n = 3

Output: [1,2,2,3,5,6]

https://leetcode.com/problems/merge-sorted-array/
求大佬指教
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-10-6 07:29:37 | 显示全部楼层
runtime error 也会有提示的,是哪个case 没有通过
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-6 10:10:39 | 显示全部楼层
Seawolf 发表于 2019-10-6 07:29
runtime error 也会有提示的,是哪个case 没有通过

我那个example就通过不了
报的好像是内存的错误
  1. Finished in N/A
  2. =================================================================
  3. ==29==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6020000000ac at pc 0x00000040f85b bp 0x7ffffa5dcd10 sp 0x7ffffa5dcd08
  4. READ of size 4 at 0x6020000000ac thread T0
  5.     #1 0x7f94d173d2e0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202e0)
  6. 0x6020000000ac is located 4 bytes to the left of 16-byte region [0x6020000000b0,0x6020000000c0)
  7. allocated by thread T0 here:
  8.     #0 0x7f94d3162ce0 in operator new(unsigned long) (/usr/local/lib64/libasan.so.5+0xe9ce0)
  9.     #4 0x7f94d173d2e0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202e0)
  10. Shadow bytes around the buggy address:
  11.   0x0c047fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  12.   0x0c047fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  13.   0x0c047fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  14.   0x0c047fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  15.   0x0c047fff8000: fa fa fd fa fa fa fd fa fa fa fd fd fa fa fd fa
  16. =>0x0c047fff8010: fa fa fd fa fa[fa]00 00 fa fa fa fa fa fa fa fa
  17.   0x0c047fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  18.   0x0c047fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  19.   0x0c047fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  20.   0x0c047fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  21.   0x0c047fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  22. Shadow byte legend (one shadow byte represents 8 application bytes):
  23.   Addressable:           00
  24.   Partially addressable: 01 02 03 04 05 06 07
  25.   Heap left redzone:       fa
  26.   Freed heap region:       fd
  27.   Stack left redzone:      f1
  28.   Stack mid redzone:       f2
  29.   Stack right redzone:     f3
  30.   Stack after return:      f5
  31.   Stack use after scope:   f8
  32.   Global redzone:          f9
  33.   Global init order:       f6
  34.   Poisoned by user:        f7
  35.   Container overflow:      fc
  36.   Array cookie:            ac
  37.   Intra object redzone:    bb
  38.   ASan internal:           fe
  39.   Left alloca redzone:     ca
  40.   Right alloca redzone:    cb
  41. ==29==ABORTING
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-6 13:07:01 | 显示全部楼层
本帖最后由 bin554385863 于 2019-10-6 13:52 编辑

这个题是考察对容器操作的掌握
如果能熟练操作容器相关函数以及排序算法,这个问题一点都不难!
另外对容器增加元素时最好使用push_back()函数,而不是使用下标.
  1. #include <iostream>
  2. #include<algorithm>
  3. #include <vector>
  4. void merge(std::vector<int> &nums1, int m, std::vector<int> &nums2, int n)
  5. {
  6.     if( (m > nums1.size())||(n > nums2.size()))
  7.     {
  8.         exit(0);
  9.     }
  10.     nums1.erase(nums1.begin() + m, nums1.end());//擦除元素erase(begin, end)/erase(pos)函数
  11.     nums2.erase(nums2.begin() + n, nums2.end());//擦除元素
  12.     for(int i:nums2)
  13.     {
  14.         nums1.push_back(i);//合并容器
  15.     }
  16.     sort(nums1.begin(), nums1.end());//排序sort(begin, end, less<datetype>(降序)/greater<datetype>(升序))
  17. }
  18. int main(int argc, char const *argv[])
  19. {
  20.     std::vector<int> i = {0,1,2,3,9,10,11,0,0,0,0};
  21.     std::vector<int> j = {4,5,6,7,23,54,0,0,0,0};
  22.     merge(i,3,j,2);
  23.     for(int k:i)
  24.     {
  25.         std::cout<<k;
  26.     }
  27.     return 0;
  28. }
复制代码

---------------------------------------------------------------------------------------
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%
的用户

容器最基本的操作增察删改
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-5 09:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表