Web Page Counter
Internet Date

判断回文

    所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的。比如"level" 、 “aaabbaaa”

对于递归的回文判断方法可以参照这篇文章:http://blog.csdn.net/cbs612537/article/details/8217425,下边代码是通过压入堆栈和排入队列的方法来循环依次比较

 1         /// <summary>
 2         /// 判断回文(比如"level"、"aaabbaaa")
 3         /// </summary>
 4         /// <param name="str"></param>
 5         /// <returns></returns>
 6         public static bool CheckPalindrome(string str)
 7         {
 8             char[] charCompare = str.ToCharArray();
 9             //栈只允许在栈顶一端进行插入和删除(栈后进先出)
10             Stack<char> s = new Stack<char>();
11             //队列只允许在表尾一端进行插入,在表头一端进行删除(队列先进先出)
12             Queue<char> q = new Queue<char>();
13             bool justfy = true;
14             //分别压入堆栈和排入队列里
15             for (int i = 0; i < str.Length;i++ )
16             {
17                 s.Push(charCompare[i]);
18                 q.Enqueue(charCompare[i]);
19             }
20             for (int i = 0; i < str.Length;i++ )
21             {
22                 //Pop将返回并去除压入堆栈里最后一个字符,Dequeue将返回并去除排入队列里的第一个字符
23                 if (s.Pop()!=q.Dequeue())
24                 {
25                     justfy = false;
26                 }
27             }
28             return justfy;
29         }
posted @ 2015-03-02 10:46  LX一木  阅读(244)  评论(0编辑  收藏  举报