Skip to content

Instantly share code, notes, and snippets.

@Ninputer
Created March 28, 2012 15:23
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 29 You must be signed in to fork a gist
  • Save Ninputer/2227226 to your computer and use it in GitHub Desktop.
Save Ninputer/2227226 to your computer and use it in GitHub Desktop.
模拟微面试之去空格
//请实现下面的函数,输入参数baseStr是一个(可以更改的)字符串,请将其中所有连续出现的多个空格都替换成一个空格,单一空格需保留。
//请直接使用baseStr的空间,如需开辟新的存储空间,不能超过o(N)(注意是小o,N是字符串的长度)。返回值是替换后的字符串的长度。
//样例代码为C#,但可以使用任何语言。如需使用任何库函数,必须同时给出库函数的实现。
class Program
{
public static int RemoveMultipleSpaces(char[] baseStr)
{
if (baseStr == null)
{
throw new ArgumentNullException("baseStr");
}
int writeCursor = 0;
bool isLastCharSpace = false;
for (int i = 0; i < baseStr.Length; i++)
{
char current = baseStr[i];
if (current == ' ')
{
if (!isLastCharSpace)
{
isLastCharSpace = true;
baseStr[writeCursor++] = current;
}
}
else
{
isLastCharSpace = false;
baseStr[writeCursor++] = current;
}
}
return writeCursor;
}
}
//样例测试程序(请自行补充更多的测试案例)
[TestFixture]
public class ScannersTest
{
[Test]
public void RemoveOneInnterSpaceBlockTest()
{
char[] input = "abc def".ToCharArray();
int resultLength = Program.RemoveMultipleSpaces(input);
Assert.AreEqual(7, resultLength);
Assert.AreEqual("abc def", new string(input, 0, resultLength));
}
[Test]
public void RemoveTwoInnterSpaceBlocksTest()
{
char[] input = "abc def ghi".ToCharArray();
int resultLength = Program.RemoveMultipleSpaces(input);
Assert.AreEqual(11, resultLength);
Assert.AreEqual("abc def ghi", new string(input, 0, resultLength));
}
[Test]
public void KeepSingleSpaceTest()
{
char[] input = " a b d e ".ToCharArray();
int resultLength = Program.RemoveMultipleSpaces(input);
Assert.AreEqual(9, resultLength);
Assert.AreEqual(" a b d e ", new string(input, 0, resultLength));
}
[Test]
public void AllSpacesTest()
{
char[] input = " ".ToCharArray();
int resultLength = Program.RemoveMultipleSpaces(input);
Assert.AreEqual(1, resultLength);
Assert.AreEqual(" ", new string(input, 0, resultLength));
}
}
@xidiandaily
Copy link

include "stdio.h"

include "stdbool.h"

int RemoveMultipleSpaces(char* baseStr)
{
char* s=baseStr;
char* d=baseStr;

if(baseStr==NULL)
    return -1;
if(*baseStr=='\0')
    return 0;

do{
    if(*d==' ')
    {
        if(s==d || *s==' ')
        {
            s++;
        }
        else
        {
            d++;
            *d=*s;
            s++;
        }
    }
    else
    {
        if(d < s)
        {
            d++;
            *d=*s;
            s++;
        }
        else
        {
            s++;
            d++;
        }
    }
}while((*d)!='\0');

return d-baseStr;

}

int main(int argc,char\* argv[])
{
    int i=0;
    int len=0;
    char test[7][30]={ 
        {"abc def"},
        {"abc    def"},
        { "abc    def       ghi"},
        { " a b   d e "},
        { " a b   d e    "},
        { "             "},
        { ""},
    };

```
for(i=0;i<7;i++)
{
    printf("test[%d]:%s|\t",i,test[i]);
    len=RemoveMultipleSpaces(test[i]);
    printf("after:len=%d,test=%s|\n",len,test[i]);
}

return 0;
```

}

===结果===
test[0]:abc def|    after:len=7,test=abc def|
test[1]:abc    def| after:len=7,test=abc def|
test[2]:abc    def       ghi|   after:len=11,test=abc def ghi|
test[3]: a b   d e |    after:len=9,test= a b d e |
test[4]: a b   d e    | after:len=9,test= a b d e |
test[5]:             |  after:len=1,test= |
test[6]:|   after:len=0,test=|

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment