登录
  • #刷题
  • #leetcode

Leetcode -- Merge Sorted Array (很诡异的问题)

fangl086
3458
6
#include <iostream>

using namespace std;

class Solution {

public:

void merge(int A[], int m, int B[], int n) {

int i = m-1;

int j = n-1;

int k = m+n-1;

for (; i>=0 && j>=0; k--) {

if (B[j] > A) {

A[k] = B[j];

j--;

}

else {

A[k] = A;

i--;

}

}

while (j >= 0) {

A[k] = B[j];

k--;

j--;

}

//cout << m << ", " << n << endl;

//for (int z=0; z<m+n; z++) cout << A[z] << endl;

}

};

int main()

{

int A[] = {1,1,2,2,3,3};[br]
int B[] = {1,1,2,2,3};[br]
int m = 6;

int n = 5;

Solution s;

s.merge(A, m, B, n);

cout << m << " " << n << endl;

for (int z=0; z<m+n; z++) cout << A[z] << endl;

return 0;

}

===============输入结果=============================

3 3

1

1

1

1

2

2

===============END===============================

问题是程序里面没有任何地方修改m, n的值,系统修改了m,n的值

将代码中的

int m = 6;

int n = 5;

改为

const int m = 6;

const int n = 5;

===============输入结果=============================

6 5

1

1

1

1

2

2

2

2

3

3

3

===============END===============================

结果是正确的了,但是代码没有修改m,n的值,所以我觉得没必要设置const.

烦请各位大神帮忙看下。
6条回复
热度排序

发表回复