EricHo
本帖最后由

本帖最后由 EricHo 于 2012-3-29 16:24 编辑

这道题来源于真实项目代码,实现起来非常简短但考点丰富。别看它不是很难,这么多年可以称得上是灭人无数!

P.S:不考虑正则表达式
//请实现下面的函数,输入参数baseStr是一个(可以更改的)字符串,请将其中所有连续出现的多个空格都替换成一个空格,单一空格需保留。

//请直接使用baseStr的空间,如需开辟新的存储空间,不能超过o(N)(注意是小o,N是字符串的长度)。返回值是替换后的字符串的长度。

//样例代码为C#,但可以使用任何语言。如需使用任何库函数,必须同时给出库函数的实现。

class Program

{

public static int RemoveMultipleSpaces(char[] baseStr)

{

// TODO

}

}

//样例测试程序(请自行补充更多的测试案例)

[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));

}

}

discovery
也没看明白楼主的意思
展开Biu

也没看明白楼主的意思,是要把包含有连续多个空格的字符串改为只含一个空格的字符串吗,然后还存储在原来的字符串空间里并压缩吗?

[查看全文]
不锈钢老鼠
膜拜楼猪
展开Biu

膜拜楼猪,膜拜小米,膜拜楼猪,膜拜小米。。

[查看全文]
gwl3323405
不太明白
展开Biu

不太明白LZ限定的方法,猜测着写了一下不知对不对:

Java代码:

[mw_shl_code=java,true] public static int RemoveMultipleSpaces(String str){

char[] ch=str.toCharArray();//把目标字符串转成字符数组

String res="";//存放结果

boolean isAdd=false;//保证一个空格只追加一次

for(int i=0;i<ch.length;i++){//遍历每个字符

if(!(ch+"").contains("\u0020")){

res+=ch;isAdd=false;//如果不是空格则追加,并重置isAdd变量为false

}else if(!isAdd){//如果没有添加过空格则追加一个空格,并设置isAdd变量为true,即已添加过

res+=ch;isAdd=true;

}

}

return res.length();

}[/mw_shl_code]

[查看全文]
Flagalac
看到的有点迟
展开Biu

看到的有点迟=-=

话说由于少某个组件上述代码没法直接用我就随便写了个不知道合不合题意=-=

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication7

{

class Program

{

static void Main(string[] args)

{

int n;

Console.WriteLine("请输入测试数据");

string str=Console.ReadLine();

while(str.IndexOf(" ")>=0)

{

n = str.IndexOf(" ");

str=str.Replace(" ", " ");

Console.WriteLine(n.ToString());

}

Console.WriteLine("转换后数据为:"+str);

Console.ReadKey();

}

}

}

[查看全文]
风音洛洛
你该感谢我么
展开Biu

EricHo 发表于 2012-5-13 17:13

然后捏?你该感谢我么?

嘛。。。我后悔当时没仔细看背下来,结果还要现构思。。。

不过幸好前天听老师说我做对了。。。下次考试前一定会把大E的发帖子都背一遍的【握拳

[查看全文]
EricHo
风音洛洛
展开Biu

风音洛洛 发表于 2012-5-13 14:46

话说这个啊,其实我刚好上星期的竞赛碰到了这道题的答案~

然后捏?你该感谢我么?

[查看全文]
风音洛洛
话说这个啊
展开Biu

话说这个啊,其实我刚好上星期的竞赛碰到了这道题的答案~

[查看全文]
johnnyworm
本帖最后由
展开Biu

本帖最后由 johnnyworm 于 2012-5-13 03:46 编辑

[mw_shl_code=java,true] public static int RemoveMultipleSpaces(char[] baseStr) {

int length = baseStr.length;

int result = length;

boolean space = false;

for (int idx = 0; idx < length; idx++) {

if (space) {

if (baseStr[idx] == ' ') {

result--;

} else {

space = false;

}

} else if (baseStr[idx] == ' ') {

space = true;

}

}

return result;

}[/mw_shl_code]

[查看全文]