1. Reverse Words in a String

Given an input string, reverse the string word by word.

Example 1:

Input: "the sky is blue"
Output: "blue is sky the"

Example 2:

Input: "  hello world!  "
Output: "world! hello"
Explanation: Your reversed string should not contain leading or trailing spaces.

Example 3:

Input: "a good   example"
Output: "example good a"
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.

Note:

  • A word is defined as a sequence of non-space characters.
  • Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.
  • You need to reduce multiple spaces between two words to a single space in the reversed string.

解法1 分词,然后用字符串拼接

class Solution {
public:
string reverseWords(string s) {
string tmp, res;
for(int i = 0; i < s.size(); ++i){
if(s[i] == ' '){
if(tmp.size() > 0){
res = tmp + " " + res;
tmp = "";
}
}else{
tmp += s[i];
}
}
res = tmp + " " + res;
int i = 0, j = res.size() - 1;
while(i < s.size() && res[i] == ' ')i++;
while(j >= 0 && res[j] == ' ')j--;
return res.substr(i, j-i+1);
}
};

解法2 字符串翻转。先将字符串整体翻转,然后将每个单词翻转。注意需要预处理除掉字符串首尾的空格,翻转结束后需要去除字符串中间多余的空格

class Solution {
public:
string reverseWords(string s) {
while(s.size() > 0 && s[0] == ' ')s.erase(0,1);
while(s.size() > 0 && s[s.size()-1] == ' ')s.erase(s.size()-1, 1);
reverse(s, 0, s.size()-1);
int pre = 0, cur = 0;
while(cur < s.size()){
if(s[cur] == ' '){
reverse(s, pre, cur-1);
cur += 1;
pre = cur;
}else{
cur += 1;
}
}
reverse(s, pre, cur-1);
int i = 1;
while(i < s.size()){
if(s[i] == ' ' && s[i-1] == ' ')s.erase(i,1);
else i++;
}
return s;
}
void reverse(string &s, int pre, int cur){
while(pre < cur){
char tmp = s[pre];
s[pre] = s[cur];
s[cur] = tmp;
pre++;
cur--;
}
}
};

【刷题-LeetCode】151 Reverse Words in a String的更多相关文章

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

  2. 【leetcode刷题笔记】Reverse Words in a String

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  3. LeetCode 151 reverse word in a string

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  4. Java for 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 ...

  5. leetcode 151. Reverse Words in a String --------- java

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  6. Leetcode#151 Reverse Words in a String

    原题地址 将单词按空格分词,然后倒序拼接即可 代码: void reverseWords(string &s) { vector<string> words; ; ; ; i &l ...

  7. [leetcode]151. Reverse Words in a String翻转给定字符串中的单词

    Given an input string, reverse the string word by word. Example: Input: "the sky is blue", ...

  8. [LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词 II

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

  9. [LeetCode] 557. Reverse Words in a String III 翻转字符串中的单词 III

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

  10. leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String

    557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public: string reverseWords(string ...

随机推荐

  1. .NET Core基础篇之:白话管道中间件

    在.Net Core中,管道往往伴随着请求一起出现.客户端发起Http请求,服务端去响应这个请求,之间的过程都在管道内进行. 举一个生活中比较常见的例子:旅游景区. 我们都知道,有些景区大门离景区很远 ...

  2. windows生成ssh上传git代码

    打开 执行 ssh-keygen -t rsa -C "email@email.com" #换成你的git登录账号 中间肯会有提示确认的 然后在 C:\Users(用户)\你电脑用 ...

  3. flink使用命令开始、停止任务

    命令操作 进行flink的安装目录 动态上传jar包启动job ./bin/flink run -c com.test.CountMain -P 3 Test-1. 0-SNAPSHOT.jar -- ...

  4. Qt之使用qss设置Qwidget背景色无效解决

    如题 解决方案 添加头文件 #include <QStyleOption> 重写函数paintEvent 内容如下 void statistics_assistant::paintEven ...

  5. Visual Studio Code常用快捷键

    说明 以下快捷键适用于windows环境下, Mac请将ctrl替换为command按键: 部分快捷键或不一样. 查看VSCode快捷键定义: settings -> keymaps. 目前使用 ...

  6. 第一篇CSDN博客,大家好!

    大家好,我是负雪明烛! 我这昵称的来源是喜欢一句很有意蕴的古诗--苍山负雪,明烛天南. 我喜欢这句诗,很多的账号都用了这个"负雪明烛"的昵称,如果大家在其他地方看到叫这个名字的人, ...

  7. 【九度OJ】题目1109:连通图 解题报告

    [九度OJ]题目1109:连通图 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1109 题目描述: 给定一个无向图和其中的 ...

  8. 【LeetCode】358. Rearrange String k Distance Apart 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/rearrang ...

  9. 手机端h5页面 图片根据手势放大缩小

    pinchzoom.js 这个插件可以简单的实现这一功能 <div class="big_pos_img page"> <div class="pinc ...

  10. [决策树]西瓜数据graphviz可视化实现

    [决策树]西瓜数据graphviz可视化实现 一.问题描述: 使用西瓜数据集构建决策树,并将构建的决策树进行可视化操作. 二.问题简析: 首先我们简单的介绍一下什么是决策树.决策树是广泛用于分类和回归 ...