LeetCode | Reverse Words in a String(C#)

题目:

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;
        }
时间: 2024-10-17 05:05:18

LeetCode | Reverse Words in a String(C#)的相关文章

【leetcode】Reverse Words in a String (python)

陆陆续续几个月下来,终于把题刷完了,过程中遇到的python的题解很少,这里重新用python实现下,所以题解可能都是总结性的,或者是新的心得,不会仅针对题目本身说的太详细. def reverseWords(self, s): s = ' '.join(s.split()[::-1]) return s [ : :  -1 ] 是将元素进行翻转 [leetcode]Reverse Words in a String (python),布布扣,bubuko.com

leetcode——Reverse Integer 反转整数数字(AC)

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 这个题比较简单,考虑特殊情况如12000,注意检查反转后数字是否会越界溢出.代码如下: class Solution { public: int reverse(int x) { bool minus = false; short int splitNum[10]; int i = 0, j = 0; unsign

345. Reverse Vowels of a String(C++)

345. Reverse Vowels of a String Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Given s = "hello", return "holle". Example 2: Given s = "leetcode", return "leotcede"

leetcode345——Reverse Vowels of a String(C++)

Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede". 个人博客:http://www.cnblogs.com/wdfw

Reverse Words in a String (4)

Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". Clarification: What constitutes a word?A sequence of non-space characters constitutes a word.//单词中间没有空格 Could t

LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2&q

[LeetCode] Interleaving String(dp)

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false. 分析:非常好的DP的训练题,

[leetcode]Reverse Words in a String @ Python

原题地址:https://oj.leetcode.com/problems/reverse-words-in-a-string/ 题意: Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". 解题思路:这题很能体现python处理字符串的强大功能. 代码: class Solut

LeetCode——Reverse Words in a String

Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". Clarification: What constitutes a word? A sequence of non-space characters constitutes a word. Could the input