去除字符串中的空格,并返回修改后的字符串。
要求时间复杂度O(N),空间复杂度O(1)。
#include "stdafx.h"
#include <process.h>
#include <iostream>
using namespace std;
void DeleteSpace(char* pStr)
{
int j = 0;
for (int i=0; pStr[i]!='\0'; i++)
{
if (pStr[i] != ' ')
{
if (i != j)
{
pStr[j] = pStr[i];
}
j++;
}
}
pStr[j++] = '\0';
}
int _tmain(int argc, _TCHAR* argv[])
{
char str[] = "Hello w o rl d !";
cout << "Before: " << str << endl;
DeleteSpace(str);
cout << "After: " << str << endl;
system("pause");
return 0;
}