也谈 尉迟方 遇到的面试题! 参阅"装配脑袋" 实现"就地正法 in place"! 考察的是"栈"的思想!...

原贴:
in place:

None.gif public   class  Class1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
static string s = "  boy     love  girls     "
InBlock.gif
InBlock.gif    
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.Console.WriteLine(
"[" + s + "]");
InBlock.gif        
char[] chars = s.ToCharArray();
InBlock.gif        
for (int i = 0 ; i < chars.Length / 2 ; i ++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Swap(
ref chars[i],ref chars[chars.Length - i - 1]);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
int j = 0;
InBlock.gif        
for (int i = 0; i <= chars.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (i == chars.Length || chars[i] == ' ')
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//这里参阅了 "装配脑袋" 自己没想出来
InBlock.gif
                for (int k = j; k < (j + i - 1/ 2f; k ++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Swap(
ref chars[k], ref chars[i - 1 - k + j]);
ExpandedSubBlockEnd.gif                }

InBlock.gif                j 
= i + 1;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
string S = new string(chars);
InBlock.gif        System.Console.WriteLine(
"[" + S + "]"); 
InBlock.gif        System.Console.ReadLine();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
static void Swap(ref char x,ref char y)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        x 
= (char) ((int) x + (int) y);
InBlock.gif        y 
= (char) ((int) x - (int) y);
InBlock.gif        x 
= (char) ((int) x - (int) y);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif




有关尉迟方兄遇到的面试题。
http://www.cnblogs.com/aowind/archive/2005/04/14/137522.html


其实这道题考查的应该是数据结构中栈的思想,用两个栈实现起来非常简单!
我想出题人根本不是在考察你类库的函数熟不熟!
C 语言仅需 字符数组 + Stack 思想 即可实现
先把原字符串从右到左全部入"大栈": STACK 然后从栈顶一个个出栈 弹出的元素 再压入另一个"小栈": stack
当"大栈STACK" 弹出的元素为空格 先暂停 再将 "小栈stack" 里的所有元素弹出 (怡子章 => 章子怡)... 循环 ...

 

None.gif public   class  Class1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
static string s = "   我   爱    张柏芝  和  章子怡     ";
InBlock.gif
InBlock.gif    
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.Console.WriteLine(
"[" + s + "]");
InBlock.gif        
//Stack 思想
InBlock.gif
        string S = ""//相当于一个栈 STACK
InBlock.gif
        string stack = ""//相当于一个栈 stack
InBlock.gif        
//我从后向前遍历 相当于全部入栈,为然后出栈做准备
InBlock.gif        
// 倒着访问就相当于全部入栈后, 后进先出 really?
InBlock.gif
        for (int i = s.Length-1 ; i >= 0 ; i--)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (s.Substring(i,1!= " ")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                stack 
= stack + s.Substring(i,1); //push
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (stack.Length > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
for (int j = stack.Length -1 ; j >=0 ; j-- )
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        S 
= S + stack.Substring(j,1);//pop
ExpandedSubBlockEnd.gif
                    }

InBlock.gif                    stack 
= "";
ExpandedSubBlockEnd.gif                }

InBlock.gif                S 
= S + s.Substring(i,1);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (i == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for (int j = stack.Length -1 ; j >=0 ; j-- )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    S 
= S + stack.Substring(j,1);//Pop
ExpandedSubBlockEnd.gif
                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        System.Console.WriteLine(
"[" + S + "]");
InBlock.gif        
//非 Stack 思想
InBlock.gif
        NoStack();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
static void NoStack()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string x = "";
InBlock.gif        
string S = "";
InBlock.gif        
for (int i = 0 ; i < s.Length ; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (s.Substring(i,1!= " ")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                x 
= x + s.Substring(i,1);
InBlock.gif                
if (i == s.Length - 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    S 
= x + S;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (x.Length > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    S 
= x + S;
InBlock.gif                    x 
= "";
ExpandedSubBlockEnd.gif                }

InBlock.gif                S 
= s.Substring(i,1+ S; //空格
ExpandedSubBlockEnd.gif
            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        System.Console.WriteLine(
"[" + S + "]");
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif



 

转载于:https://www.cnblogs.com/Microshaoft/archive/2005/04/15/138217.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值