LeetCode OJ - 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".

解题思路:

  1、先对字符串进行一次总的翻转

  2、以空格为单位,划分单词,然后对每个单词再进行一次翻转

代码:

  


class Solution {
public:
void swap(char& a, char& b){
int tmp = a;
a = b;
b = tmp;
}
void reverse(string &s, int begin, int end){
if (s[begin] == ‘\0‘ || s[end] == ‘\0‘){
return;
}
while (begin < end){
swap(s[begin], s[end]);
begin++;
end--;
}
}
void reverseWords(string &s) {
if (s[0] == ‘\0‘ || s.empty()){
return;
}
reverse(s, 0, s.length() - 1);
//*/
int start = 0, end = 0, len = 0;
while (start < s.length()){
while (start != s.length() && s[start] == ‘ ‘) start++; //跳过对于的空格
for (end = start; end != s.length() && s[end] != ‘ ‘; end++);
if (len != 0 && end > start) s[len++] = ‘ ‘;
if (end > start){
reverse(s, start, end - 1);
}
while (start < end) s[len++] = s[start++];

}
s.resize(len);
//*/
}
};

LeetCode OJ - Reverse Words in a String

时间: 2024-10-12 13:34:51

LeetCode OJ - Reverse Words in a String的相关文章

【leetcode】Reverse Words in a String

问题:给定一个字符串,字符串中包含若干单词,每个单词间由空格分隔,将单词逆置,即第一个单词成为最后一个单词,一次类推. 说明:字符串本身可能包含前导空格或后导空格,单词间可能包含多个空格,要求结果中去掉前导和后导空格,单词间空格只保留一个. 与rotate函数类似,先逆置每个单词,再将所有字符串逆置. void reverseWords(string &s) { if(s.size() == 0) return; char blank = ' '; size_t len = s.size();

【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 OJ - Reverse Linked List II

题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ l

LeetCode OJ1: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". 解题思路: 先利用split()方法将句子按空格分为几个单词的列表, 然后reverse()函数使列表逆序, 最后join函数以空格为间隔拼成字符串返回结果. Python代码: class Solution:  

[LeetCode] 151. 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". Update (2015-02-12):For C programmers: Try to solve it in-place in O(1) space. Clarification: What constitutes a

[LeetCode]151.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". Update (2015-02-12): For C programmers: Try to solve it in-place in O(1) space. click to show clarification.

Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL tsetnoc" 注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格. 思路 分割字符串,再逆序,拼接到字符串 代码实现 package String; /** * 557. Reverse Words in a St

leetCode 151. Reverse Words in a String 字符串反转 | Medium

151. 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". 题目大意: 输入一个字符串,将单词序列反转. 思路1: 采用一个vector,来存放中间结果 将vector的结果倒序放入原字符串中. 思路2: 在字符串分割的时候

leetcode - [1]Reverse Words in a String

Question: 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 con