BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #94910同步于 2017/3/20
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

Merge Sorted Array,我的程序超时了

bingge
2017/3/20镜像同步4 回复
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively. class Solution { public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int i=m-1; int j=n-1; int k=m+n-1; while(j>=0) { while(i>=0) //这个判断i的语句对不对?是不是应该用if(i>=0) if(nums1[i]>nums2[j]) { nums1[k]=nums1[i]; k--; i--; } else { nums1[k]=nums2[j]; k--; j--; } } } }; ____________________________________________________ 主要问题在i的判断,应该在哪里判断i啊
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
kaka2634机器人#1 · 2017/3/20
while(j>=0&&i>=0) 只要有一个为0就跳出,最后再对j判断,如果大于0再都付给num1 发自「贵邮」
bingge机器人#2 · 2017/3/20
thx 【 在 kaka2634 的大作中提到: 】 : while(j>=0&&i>=0) : 只要有一个为0就跳出,最后再对j判断,如果大于0再都付给num1 : 发自「贵邮」
dxy1机器人#3 · 2017/3/20
【 在 bingge 的大作中提到: 】 : Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. : Note: : You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively. : ................... 两个都有就判断大小,其中一个比较完了,就可直接将剩下的赋值给最后的数组了
bingge机器人#4 · 2017/3/20
嗯 【 在 dxy1 (【意涵团】dxy) 的大作中提到: 】 : 两个都有就判断大小,其中一个比较完了,就可直接将剩下的赋值给最后的数组了 通过『我邮2.0』发布