private string Revert(string str) { if (str.Length == 0) { return string.Empty; } string newStr = null; int indexFirst = -1; int indexLast = str.Length - 1; for (int i = str.Length - 1; i >= 0; i--) { if (str[i] == ‘ ‘) { indexFirst = i + 1; while (indexLast - indexFirst >= 0) { newStr += str[indexFirst]; ++indexFirst; } newStr = newStr + ‘ ‘; indexLast = i - 1; indexFirst = -1; } if (i == 0) { indexFirst = i; while (indexLast - indexFirst >= 0) { newStr += str[indexFirst]; ++indexFirst; } } } return newStr; }
[算法] C# Revert 单词反转字符串[低时间复杂度],布布扣,bubuko.com
[算法] C# Revert 单词反转字符串[低时间复杂度]
原文地址:http://blog.csdn.net/wzhiu/article/details/25639447