HappyLeetcode1 Reverse Words in a String

HappyLeetcode1:Reverse Words in a String

题目描述:

题目思考:

  1. 建立一个数组,每个数组存储一个单词。
  2. 将该数组倒序输出,每输出一个单词后,紧跟着输出一个空格。

题目解题代码

Python

class Solution:

# @param s, a string

# @return a string

def reverseWords(self, s):

slist=[]

slist=s.split()

for i in range(len(slist)/2):

slist[i],slist[len(slist)-1-i]=slist[len(slist)-1-i],slist[i]

return ‘ ‘.join(slist)

C++

class Solution {

public:

void reverseWords(string &s) {

if(s.empty())

return;

//remove heading and trailing spaces

int i = 0;

while(i<s.size() && s[i] == ‘ ‘)

i++;

if(i == s.size())

{

s = "";

return;

}

int j = s.size() - 1;

while(j>=0 && s[j] == ‘ ‘)

j--;

if(j == -1)

{

s = "";

return;

}

s = s.substr(i,j - i + 1);

size_t pos = 0;

vector<string> strs;

size_t begin = 0;

while(begin < s.size())

{

pos = s.find_first_of(‘ ‘,begin);

if(pos == begin)

{

begin++;

continue;

}

else if(pos != -1)

strs.push_back(s.substr(begin,pos - begin));

else  //pos == -1, the end

{

strs.push_back(s.substr(begin,s.size() - 1 - begin + 1));

break;

}

begin = pos + 1;

}

string ans;

for(int i = strs.size() - 1; i > 0; i--)

{

ans += strs[i];

ans += " ";

}

ans += strs[0];

s = ans;

}

};

题后总结

  1. ‘ ‘.join(slist) 堪称列表转字符串的神表达。

官网答案

One simple approach is a two-pass solution: First pass to split the string by spaces into an array of words, then second pass to extract the words in reversed order.

We can do better in one-pass. While iterating the string in reverse order, we keep track of a word’s begin and end position. When we are at the beginning of a word, we append it.

时间: 2024-11-05 12:06:04

HappyLeetcode1 Reverse Words in a String的相关文章

Reverse Words in a String

Problem: Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". 这个题目叙述的不够精确,有些边界的情况需要考虑.题目的下方给出了Clarification澄清了几个问题,也是面试的时候需要确认的. 第一个问题是什么构成了一个词(word),回答是一串不包含空格(non

Reverse Words in a String (JAVA)

Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". 1 public class Solution { 2 public String reverseWords(String s) { 3 if(s.equals(""))return s; 4 String

[LeetCode] Reverse Words in a String II

Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.The input string does not contain leading or trailing spaces and the words are always separated by a single space.For example,Given s = "t

[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.

1. Reverse Words in a String

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". click to show clarification. Clarification: What constitutes a word?A sequence of non-s

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】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 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: 在字符串分割的时候