代码改变世界

字符串反转的几种实现

2009-04-06 10:44  yearN  阅读(5106)  评论(9编辑  收藏  举报

今天看了一些微软面试题,其中有一个编程题是:求一个字符串的反串。我试着写了两种方法,然后再拿来与.netList<T>中提供的Reverse()方法来比较一下。

我们先生成一个测试字符串:

char[] sArr = new char[10000]; //我的测试算法中用到的

           for (int i = 0; i < 10000; i++)

            {

                if (i % 2 == 0)

                {

                    sArr[i] = 'A';

                }

                else

                {

                    sArr[i] = 'B';

                }

            }

            string s = new string(sArr);

  List<byte> list = new List<byte>(10000); //List测试中用到的

    //List赋值

            byte[] by = System.Text.Encoding.ASCII.GetBytes(sArr);

            for (int i = 0; i < by.Length; i++)

            {

                list.Add(by[i]);

            }

好了,准备工作已经做完,让我们来看第一种方法:

/// <summary>

        /// 字符串的反转(用栈实现)

        /// </summary>

        /// <param name="str">要反转的字符串</param>

        /// <returns></returns>

        public static string Reverse(string str)

        {

            byte[] byteStr = System.Text.Encoding.ASCII.GetBytes(str);

            Stack<byte> stack = new Stack<byte>();

 

            for (int i = 0; i < byteStr.Length; i++)

            {

                stack.Push(byteStr[i]);

            }

            byteStr = stack.ToArray();

            return System.Text.Encoding.ASCII.GetString(byteStr);

        }

这种算法我是用栈来实现,用时2584毫秒。再看另一个方法:

/// <summary>

        /// 字符串的反转(常规的算法)

        /// </summary>

        /// <param name="str">要反转的字符串</param>

        /// <returns></returns>

        public static string Reverse1(string str)

        {

            byte[] byteStr = System.Text.Encoding.ASCII.GetBytes(str);

            byte b;

            for (int i = 0; i < byteStr.Length/2; i++)

            {

                b = byteStr[i];

                byteStr[i] = byteStr[byteStr.Length - i - 1];

                byteStr[byteStr.Length - i - 1] = b;

            }

            return System.Text.Encoding.ASCII.GetString(byteStr);

        }

这个方法是常规的算法,记得我们高中老师当时教我们用的就是此算法,用时1262毫秒

    如果直接用List<T>Reverse()方法,用时却是120毫秒

总结:

    比较以上三种方法,.net提供的方法确实很不错。我不知道在微软面试时能不能用.net提供的方法写(开个玩笑),.netReverse()方法的实现我没有研究过,哪位大侠有更好的算法不妨跟帖提供一下。

最后我给出我的测试源程序:

class Class1

    {

        static void Main()

        {

            char[] sArr = new char[10000];//我的测试算法中用到的

           

            List<byte> list = new List<byte>(10000);//List测试中用到的

            for (int i = 0; i < 10000; i++)

            {

                if (i % 2 == 0)

                {

                    sArr[i] = 'A';

                }

                else

                {

                    sArr[i] = 'B';

                }

            }

            string s = new string(sArr);

            //List赋值

            byte[] by = System.Text.Encoding.ASCII.GetBytes(sArr);

            for (int i = 0; i < by.Length; i++)

            {

                list.Add(by[i]);

            }

 

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

            sw.Start();

            for (int i = 0; i < 10000; i++)

                Reverse(s);

            sw.Stop();

            Console.WriteLine("Reverse: " + sw.Elapsed.TotalMilliseconds + "ms");

 

            sw.Reset();

            sw.Start();

            for (int i = 0; i < 10000; i++)

                Reverse1(s);

            sw.Stop();

            Console.WriteLine("Reverse1: " + sw.Elapsed.TotalMilliseconds + "ms");

 

            sw.Reset();

            sw.Start();

            for (int i = 0; i < 10000; i++)

                list.Reverse();

            sw.Stop();

            Console.WriteLine("list.Reverse: " + sw.Elapsed.TotalMilliseconds + "ms");

 

            Console.Read();

        }

 

        /// <summary>

        /// 字符串的反转(用栈实现)

        /// </summary>

        /// <param name="str">要反转的字符串</param>

        /// <returns></returns>

        public static string Reverse(string str)

        {

            byte[] byteStr = System.Text.Encoding.ASCII.GetBytes(str);

            Stack<byte> stack = new Stack<byte>();

 

            for (int i = 0; i < byteStr.Length; i++)

            {

                stack.Push(byteStr[i]);

            }

            byteStr = stack.ToArray();

            return System.Text.Encoding.ASCII.GetString(byteStr);

        }

 

        /// <summary>

        /// 字符串的反转(常规的算法)

        /// </summary>

        /// <param name="str">要反转的字符串</param>

        /// <returns></returns>

        public static string Reverse1(string str)

        {

            byte[] byteStr = System.Text.Encoding.ASCII.GetBytes(str);

            byte b;

            for (int i = 0; i < byteStr.Length/2; i++)

            {

                b = byteStr[i];

                byteStr[i] = byteStr[byteStr.Length - i - 1];

                byteStr[byteStr.Length - i - 1] = b;

            }

            return System.Text.Encoding.ASCII.GetString(byteStr);

        }

    }