Leetcode: Reverse Words in a String

你的名字 2022-08-07 10:44 155阅读 0赞

题目:
Given an input string, reverse the string word by word.

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

思路一:
先休整下给定的字符串,去掉其中的多于一个的空格,就是使所有单词之间的空格都变成一个,然后去掉最后面的空格,给最前面加一个空格。这样每次遇到空格的时候,前面的子串就是一个完整的单词。
C++代码实现(用时9ms):

  1. class Solution
  2. {
  3. public:
  4. void reverseWords(string &s)
  5. {
  6. //去掉所有多于一个的空格
  7. for (size_t i = 1; i < s.length(); i++)
  8. {
  9. if (s[i - 1] == ' ' && s[i] == ' ')
  10. {
  11. s.erase(s.begin() + i);
  12. --i;
  13. }
  14. }
  15. //如果前面没有空格,给前面加一个空格
  16. if (s.front() != ' ') s.insert(0, " ");
  17. //如果后面有空格,去掉后面空格
  18. if (s.back() == ' ') s.pop_back();
  19. if (s.length() <= 1) return;
  20. vector<string> words;
  21. int start = int(s.length() - 1);
  22. //从后往前遍历
  23. for (int i = start; i >= 0; --i)
  24. {
  25. if (s[i] == ' ')
  26. {
  27. words.push_back(s.substr(i + 1, start - i));
  28. if (i != 0) start = i - 1;
  29. }
  30. }
  31. s.clear();
  32. size_t size = words.size();
  33. for (size_t i = 0; i < size; i++)
  34. {
  35. s += words[i];
  36. s.push_back(' ');
  37. }
  38. //去掉最后一个多余的空格
  39. s.pop_back();
  40. }
  41. };

思路二:
维护两个栈,当是不是空格的时候压入单词栈,当是空格的时候压入句子栈。这个的好处就是不用预先处理给定的的字符串。
C++实现代码(用时24ms,可能是栈的开销比较大吧):

  1. class Solution
  2. {
  3. public:
  4. void reverseWords(string &s)
  5. {
  6. s += ' ';//因为当s[i]是空格的时候前面的字符串是一个单词,为了好判断给字符的后面加一个空格
  7. size_t length = s.length();
  8. stack<char> words;
  9. stack<char> sentence;
  10. for (size_t i = 0; i < length; i++)
  11. {
  12. //如果不为空格,压入words栈中
  13. if (s[i] != ' ') words.push(s[i]);
  14. //如果是空格且words不为空的话,将words中的字符压入sentence栈中且在sentence后面添加空额
  15. //如果是空格但是words为空则什么都不做
  16. else
  17. {
  18. bool flag = false;
  19. while (!words.empty())
  20. {
  21. flag = true;
  22. sentence.push(words.top());
  23. words.pop();
  24. }
  25. if (flag) sentence.push(' ');
  26. }
  27. }
  28. s.clear();
  29. //如果sentence不为空,后面肯定有一个多余的空格,去掉最后的空格
  30. if (!sentence.empty()) sentence.pop();
  31. while (!sentence.empty())
  32. {
  33. s += sentence.top();
  34. sentence.pop();
  35. }
  36. }
  37. };

C#代码(思路一解法):

  1. public class Solution
  2. {
  3. public string ReverseWords(string s)
  4. {
  5. if (s.Length == 0) return string.Empty;
  6. StringBuilder result = new StringBuilder(s);
  7. //去掉多于一个的空格
  8. for (int i = 1; i < result.Length; i++)
  9. {
  10. if (result[i - 1] == ' ' && result[i] == ' ')
  11. {
  12. result.Remove(i, 1);
  13. --i;
  14. }
  15. }
  16. if (result[0] != ' ') result.Insert(0, ' ');
  17. if (result[result.Length - 1] == ' ') result.Remove(result.Length - 1, 1);
  18. if (result.Length <= 1) return result.ToString();
  19. s = result.ToString();
  20. List<string> words = new List<string>();
  21. int start = result.Length - 1;
  22. for (int i = start; i >= 0; --i)
  23. {
  24. if (result[i] == ' ')
  25. {
  26. words.Add(s.Substring(i + 1, start - i));
  27. start = i - 1;
  28. }
  29. }
  30. result.Clear();
  31. foreach (string item in words)
  32. {
  33. result.Append(item);
  34. result.Append(' ');
  35. }
  36. result.Remove(result.Length - 1, 1);
  37. return result.ToString();
  38. }
  39. }

Python代码:

  1. class Solution:
  2. # @param s, a string
  3. # @return a string
  4. def reverseWords(self, s):
  5. if len(s) == 0:
  6. return ''
  7. words = s.split(' ')
  8. i = 0
  9. while i < len(words):
  10. if words[i] == '':
  11. del words[i]
  12. else:
  13. i = i + 1
  14. if len(words) == 0:
  15. return ''
  16. words.reverse()
  17. result = ''
  18. for item in words:
  19. result = result + item + ' '
  20. return result[:len(result) - 1]

显然,Python代码要简洁地多!

发表评论

表情:
评论列表 (有 0 条评论,155人围观)

还没有评论,来说两句吧...

相关阅读