码迷,mamicode.com
首页 > Windows程序 > 详细

LeetCode | Reverse Words in a String(C#)

时间:2017-06-01 18:41:16      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:leetcode   count   span   ++   div   i++   color   item   tostring   

题目:

Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

 

代码:

        public static string reverseWords(string str)
        {
            string reverStr = "";
            int count = 0;
            Stack stack1 = new Stack();
            Stack stack2 = new Stack();
            foreach (var item in str)
            {
                if(item== )
                {
                    stack1.Push(item);
                    count = stack1.Count;
                    for (int i = 0; i < count; i++)
                    {
                        stack2.Push(stack1.Pop());
                    }
                }
                else
                {
                    stack1.Push(item);
                }
            }
            stack2.Push( );
            count = stack1.Count;
            for (int i = 0; i < count; i++)
            {
                stack2.Push(stack1.Pop());
            }
            count = stack2.Count - 1;
            for (int i = 0; i < count; i++)
            {
                reverStr = reverStr + stack2.Pop().ToString();
            }
            return reverStr;
        }

 

LeetCode | Reverse Words in a String(C#)

标签:leetcode   count   span   ++   div   i++   color   item   tostring   

原文地址:http://www.cnblogs.com/dayang12525/p/6930089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!