原贴:
in place:
public class Class1
{
static string s = " boy love girls ";
static void Main(string[] args)
{
System.Console.WriteLine("[" + s + "]");
char[] chars = s.ToCharArray();
for (int i = 0 ; i < chars.Length / 2 ; i ++)
{
Swap(ref chars[i],ref chars[chars.Length - i - 1]);
}
int j = 0;
for (int i = 0; i <= chars.Length; i++)
{
if (i == chars.Length || chars[i] == ' ')
{
//这里参阅了 "装配脑袋" 自己没想出来
for (int k = j; k < (j + i - 1) / 2f; k ++)
{
Swap(ref chars[k], ref chars[i - 1 - k + j]);
}
j = i + 1;
}
}
string S = new string(chars);
System.Console.WriteLine("[" + S + "]");
System.Console.ReadLine();
}
static void Swap(ref char x,ref char y)
{
x = (char) ((int) x + (int) y);
y = (char) ((int) x - (int) y);
x = (char) ((int) x - (int) y);
}
}
{
static string s = " boy love girls ";
static void Main(string[] args)
{
System.Console.WriteLine("[" + s + "]");
char[] chars = s.ToCharArray();
for (int i = 0 ; i < chars.Length / 2 ; i ++)
{
Swap(ref chars[i],ref chars[chars.Length - i - 1]);
}
int j = 0;
for (int i = 0; i <= chars.Length; i++)
{
if (i == chars.Length || chars[i] == ' ')
{
//这里参阅了 "装配脑袋" 自己没想出来
for (int k = j; k < (j + i - 1) / 2f; k ++)
{
Swap(ref chars[k], ref chars[i - 1 - k + j]);
}
j = i + 1;
}
}
string S = new string(chars);
System.Console.WriteLine("[" + S + "]");
System.Console.ReadLine();
}
static void Swap(ref char x,ref char y)
{
x = (char) ((int) x + (int) y);
y = (char) ((int) x - (int) y);
x = (char) ((int) x - (int) y);
}
}
有关尉迟方兄遇到的面试题。
http://www.cnblogs.com/aowind/archive/2005/04/14/137522.html
其实这道题考查的应该是数据结构中栈的思想,用两个栈实现起来非常简单!
我想出题人根本不是在考察你类库的函数熟不熟!
C 语言仅需 字符数组 + Stack 思想 即可实现
先把原字符串从右到左全部入"大栈": STACK 然后从栈顶一个个出栈 弹出的元素 再压入另一个"小栈": stack
当"大栈STACK" 弹出的元素为空格 先暂停 再将 "小栈stack" 里的所有元素弹出 (怡子章 => 章子怡)... 循环 ...
public class Class1
{
static string s = " 我 爱 张柏芝 和 章子怡 ";
static void Main(string[] args)
{
System.Console.WriteLine("[" + s + "]");
//Stack 思想
string S = ""; //相当于一个栈 STACK
string stack = ""; //相当于一个栈 stack
//我从后向前遍历 相当于全部入栈,为然后出栈做准备
// 倒着访问就相当于全部入栈后, 后进先出 really?
for (int i = s.Length-1 ; i >= 0 ; i--)
{
if (s.Substring(i,1) != " ")
{
stack = stack + s.Substring(i,1); //push
}
else
{
if (stack.Length > 0)
{
for (int j = stack.Length -1 ; j >=0 ; j-- )
{
S = S + stack.Substring(j,1);//pop
}
stack = "";
}
S = S + s.Substring(i,1);
}
if (i == 0)
{
for (int j = stack.Length -1 ; j >=0 ; j-- )
{
S = S + stack.Substring(j,1);//Pop
}
}
}
System.Console.WriteLine("[" + S + "]");
//非 Stack 思想
NoStack();
}
static void NoStack()
{
string x = "";
string S = "";
for (int i = 0 ; i < s.Length ; i++)
{
if (s.Substring(i,1) != " ")
{
x = x + s.Substring(i,1);
if (i == s.Length - 1)
{
S = x + S;
}
}
else
{
if (x.Length > 0)
{
S = x + S;
x = "";
}
S = s.Substring(i,1) + S; //空格
}
}
System.Console.WriteLine("[" + S + "]");
}
}
{
static string s = " 我 爱 张柏芝 和 章子怡 ";
static void Main(string[] args)
{
System.Console.WriteLine("[" + s + "]");
//Stack 思想
string S = ""; //相当于一个栈 STACK
string stack = ""; //相当于一个栈 stack
//我从后向前遍历 相当于全部入栈,为然后出栈做准备
// 倒着访问就相当于全部入栈后, 后进先出 really?
for (int i = s.Length-1 ; i >= 0 ; i--)
{
if (s.Substring(i,1) != " ")
{
stack = stack + s.Substring(i,1); //push
}
else
{
if (stack.Length > 0)
{
for (int j = stack.Length -1 ; j >=0 ; j-- )
{
S = S + stack.Substring(j,1);//pop
}
stack = "";
}
S = S + s.Substring(i,1);
}
if (i == 0)
{
for (int j = stack.Length -1 ; j >=0 ; j-- )
{
S = S + stack.Substring(j,1);//Pop
}
}
}
System.Console.WriteLine("[" + S + "]");
//非 Stack 思想
NoStack();
}
static void NoStack()
{
string x = "";
string S = "";
for (int i = 0 ; i < s.Length ; i++)
{
if (s.Substring(i,1) != " ")
{
x = x + s.Substring(i,1);
if (i == s.Length - 1)
{
S = x + S;
}
}
else
{
if (x.Length > 0)
{
S = x + S;
x = "";
}
S = s.Substring(i,1) + S; //空格
}
}
System.Console.WriteLine("[" + S + "]");
}
}