557. 反转字符串中的单词 III

给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。

示例 1:

输入: "Let‘s take LeetCode contest"
输出: "s‘teL ekat edoCteeL tsetnoc" 

注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格。

思路:先将给定的字符串(s)中的单词拆分出来(str),然后单个处理每个单词,拼接成一个新的字符串(ans),返回ans.

总结:

 1 static const auto __ = []()
 2 {
 3     ios::sync_with_stdio(false);
 4     cin.tie(nullptr);
 5     return nullptr;
 6 }();
 7
 8 class Solution {
 9 public:
10     string reverseWords(string s) {
11         string ans="";
12         vector<string>str;
13         string st = "";
14         for(int i = 0; i < s.length(); i++) {
15             if(s[i] != ‘ ‘)
16                 st += s[i];
17
18             if(s[i] == ‘ ‘|| i == s.length()-1) {
19                 str.push_back(st);
20                 st = "";
21
22             }
23
24         }
25         for(int i = 0; i < str.size(); i++) {
26             for(int j = str[i].length() - 1; j >= 0; j--) {
27                 ans += str[i][j];
28             }
29             if(i !=  str.size()-1) {
30                 ans += " ";
31             }
32         }
33         return ans;
34     }
35 };

使用vector容器,数组要用length( )

在处理字符串的单词的时候一开始是↓  发现最后一个单词没法分割出来,后来增加一个条件后解决问题。

 1  vector<string>str;
 2         string st = "";
 3         for(int i = 0; i < s.length(); i++) {
 4             if(s[i] != ‘ ‘)
 5                 st += s[i];
 6
 7             if(s[i] == ‘ ‘) {
 8                 str.push_back(st);
 9                 st = "";
10
11             }
12
13         }

原文地址:https://www.cnblogs.com/jj81/p/9038674.html

时间: 2024-07-29 23:25:58

557. 反转字符串中的单词 III的相关文章

Leetcode 557.反转字符串中的单词III

反转字符串中的单词III 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL tsetnoc"  注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格. 实现思路: 刚看题目心想直接用String.split会不会更容易些,不过这样就失去了这个算法的意义了.还是采用最原始的方法,先

[LeetCode 557] 反转字符串中的单词 III

题目: 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL tsetnoc" 注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格. 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/reverse-words-in-a-stri

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

反转字符串中的单词 III

给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest"输出: "s'teL ekat edoCteeL tsetnoc" 注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格. code1:使用自定义reverse函数 class Solution { public: string reverseWords(string s) { i

【LeetCode-面试算法经典-Java实现】【152-Reverse Words in a String(反转字符串中的单词)】

[152-Reverse Words in a String(反转字符串中的单词)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". 题目大意 给定一个字符串,将其反转,其的字词不转 解题思路

反转字符串中的单词

第一种 export default (str) => { // 字符串按空格进行分隔,保存数组,数组的元素的先后顺序就是单词的顺序 let arr = str.split(' ') // 对数组进行遍历,然后每个元素进行反转 let result = arr.map(item => { return item.split('').reverse().join('') }) return result.join(' ') } 第二种 export default (str) => { /

[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

统计一个字符串中的单词的个数,并打印各个单词

/*测试数据:Shen zhen is a beautiful city!*/ /*运行结果:Word:6 Shen zhen is a beautiful city!*/ #include<stdio.h> #define SIZE 1000 void wordCount(char *str) { int count = 0, flag = 0; char *p = str; while (*p != '\0'){ while (*p == 32){ if (*(p + 1) == 0){/

345. 反转字符串中元音字母的位置 Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede" 题意:反转字符串中元音字母的位置 方法1:用栈保存元音字符串,时间