面试问题:修剪字符串中的多个连续空格

这是一个面试问题寻找最佳的最佳解决方案来修剪字符串中的多个空格。 此操作应该是就地操作。

input = "I Like StackOverflow a lot" output = "I Like StackOverflow a lot" 

不允许使用字符串函数,因为这是一个面试问题。 寻找问题的算法解决方案。

保留两个索引:下一个可用的字母(例如, i ),以及您正在检查的当前索引(例如, j )。

只需用j循环遍历所有字符,每当看到一个字母时,将其复制到索引i ,然后递增i 。 如果您看到空格前面没有空格,也请复制空格。

认为这可以就地工作……

使用是否有资格作为“算法解决方案”?

 #include  #include  #include  #include  struct BothAre { char c; BothAre(char r) : c(r) {} bool operator()(char l, char r) const { return r == c && l == c; } }; int main() { std::string str = "I Like StackOverflow a lot"; std::string::iterator i = unique(str.begin(), str.end(), BothAre(' ')); std::copy(str.begin(), i, std::ostream_iterator(std::cout, "")); std::cout << '\n'; } 

测试运行: https : //ideone.com/ITqxB

一个c ++ 0x – 使用lambda而不是常规函数对象的解决方案。 与Cubbi的解决方案相比。

 #include  #include  int main() { std::string str = "I Like StackOverflow a lot"; str.erase(std::unique(str.begin(), str.end(), [](char a, char b) { return a == ' ' && b == ' '; } ), str.end() ); } 

我跟着这个:

 int main(int argc, char* argv[]) { char *f, *b, arr[] = " This is a test. "; f = b = arr; if (f) do { while(*f == ' ' && *(f+1) == ' ') f++; } while (*b++ = *f++); printf("%s", arr); return 0; } 

我建议一个小状态机(只是一个简单的switch语句)。 因为如果面试官和我一样,他们要求你做的第一个改进是完全修剪任何前导或尾随空格,这样:

 " leading and trailing " 

变成了:

 "leading and trailing" 

代替:

 " leading and trailing " 

这是对状态机设计的一个非常简单的修改,对我而言,通常在“直接”编码循环中理解状态机逻辑似乎更容易,即使它需要更多的代码行而不是直接循环。

如果你认为对直接循环的修改不会太糟糕(可以合理地论证),那么我(作为采访者)会投入,我也希望修剪数字的前导零。

另一方面,很多采访者可能实际上不喜欢状态机解决方案是“非最优”的。 我想这取决于你想要优化的内容。

这里只使用stdio:

 #include  int main(void){ char str[] = "I Like StackOverflow a lot"; int i, j = 0, lastSpace = 0; for(i = 0;str[i]; i++){ if(!lastSpace || str[i] != ' '){ str[j] = str[i]; j++; } lastSpace = (str[i] == ' '); } str[j] = 0; puts(str); return 0; } 

修剪多个空格也意味着空格应始终后跟非空格字符。

 int pack = 0; char str[] = "I Like StackOverflow a lot"; for (int iter = 1; iter < strlen(str); iter++) { if (str[pack] == ' ' && str[iter] == ' ') continue; str[++pack] = str[iter]; } str[++pack] = NULL; 
 int j = 0; int k=0; char str[] = "I Like StackOverflow a lot"; int length = strlen(str); char str2[38]; for (int i = 0; i < length; i++) { if (str[i] == ' ' && str[i+1] == ' ') continue; str2[j] = str[i]; j++; } str2[j] =NULL; cout< 
 void trimspaces(char * str){ int i = 0; while(str[i]!='\0'){ if(str[i]==' '){ for(int j = i + 1; j 

Haskell中的function变体:

 import Data.List (intercalate) trimSpaces :: String -> String trimSpaces = intercalate " " . words 

下一个算法:

  1. 将一个字符串分成一个单词列表,这些单词由空格分隔
  2. 连接列表在列表中的每个元素之间插入一个空格

这是一个非常简单的删除额外空格的实现。

 #include  std::string trimExtraWhiteSpaces(std::string &str); int main(){ std::string str = " Apple is a fruit and I like it . "; str = trimExtraWhiteSpaces(str); std::cout<