Leetcode#:557. Reverse Words in a String III

xiaoxiao2021-02-28  107

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Example 1:

Input: "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"

Note: In the string, each word is separated by single space and there will not be any extra space in the string.

分割成每一个单词,翻转;

我的代码:

class Solution { public: string reverseWords(string s) { string ss,temp; int k=0; for(int i=0;i<=s.size();i++) { if(s[i]==' '||i==s.size()) { temp=""; for(int j=i-1;j>=k;j--) temp+=s[j]; if(s[i]==' ') ss+=temp+" "; else ss+=temp; k=i+1; } } return ss; } };

转载请注明原文地址: https://www.6miu.com/read-40258.html

最新回复(0)