LeetCode解题思路:557. Reverse Words in a String III

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.

题意:把输入的字符串单词的顺序不变,单词中字母顺序逆置,测试中所给的字符串中不会有两个相连的空格。

基本思路:

1. 查单词,把从开始到空格、空格之间和空格到结束之间的单词分别保存在一个字符串数组中,当结束时,把字符串数组中的每个单词倒置,然后按顺序输出中间插入空格。

这种方法虽然条理清晰,但是用了大量的额外空间,而且相当于要遍历三次,所以不推荐使用。

2. 下面这种方式没有使用额外空间就对每个单词进行头尾交换。代码如下

 1 char* reverseWords(char* s) {
 2     char *beg = s;
 3     char *end = s;
 4     char temp = 0;
 5     while(*beg != ‘\0‘)
 6     {
 7         if(*beg == ‘ ‘){
 8             ++beg;
 9             ++end;
10         }else if(*end == ‘\0‘ || *end == ‘ ‘)
11         {
12             char *ps = beg, *pe = end-1;
13             while(ps <pe){
14                 temp = *ps;
15                 *ps = *pe;
16                 *pe = temp;
17                 ps++;
18                 pe--;
19             }
20             beg = end;
21         }else
22             end++;
23     }
24     return s;
25 }

写的简洁一点就是

 1 class Solution {
 2 public:
 3     string reverseWords(string s) {
 4         size_t front = 0;
 5         for(int i = 0; i <= s.length(); ++i)
 6             if(i == s.length() || s[i] == 0x20){
 7                 reverse(&s[front], &s[i]);
 8                 front = i + 1;
 9             }
10         return s;
11     }
12 };

使用c++的reverse函数而不自定义反转函数,关于reverse函数:http://www.cplusplus.com/reference/algorithm/reverse/

时间: 2024-10-27 01:10:25

LeetCode解题思路:557. Reverse Words in a String III的相关文章

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】557. Reverse Words in a String III

Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-in-a-string-iii/ 1)problem Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace

557. Reverse Words in a String III【easy】

557. Reverse Words in a String III[easy] 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:

LeetCode 557. Reverse Words in a String III

晚饭后写了一题.当然,我这种菜鸟是从easy开始写的...发现leetcode也是有bug的 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" O

Leetcode - 557. Reverse Words in a String III (C++) stringstream

1. 题目:https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/ 反转字符串中的所有单词. 2. 思路: 这题主要是要注意空格的影响.比方说,string首尾和单词之间可能有一或多个空格.看到有人逐个对空格判断,但是我觉得逐个判断其实稍微容易出错(当然如果非常熟悉的话就完全无所谓啦),我的一个简单想法是用stringstream. PS:不熟悉stringstream的朋友可以看看链接的文档. 3. 代码 cla

557. Reverse Words in a String III

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"

ledecode Reverse Words in a String III

557. Reverse Words in a String III 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: "

LeetCode解题思路:595. Big Countries

There is a table World +-----------------+------------+------------+--------------+---------------+ | name | continent | area | population | gdp | +-----------------+------------+------------+--------------+---------------+ | Afghanistan | Asia | 652

LeetCode解题思路:344. Reverse String

Write a function that takes a string as input and returns the string reversed. Example:Given s = "hello", return "olleh". 题意:翻转字符串,从头到尾翻转. 基本思路: 1.如果对指针操作还不熟练,可以通过另分配n个字节的内存来进行翻转.即将字符串从后向前读,然后从前向后保存在新内存中,再将指针指向新内存. 代码如下:(如果不把strlen(s)保