翻转字符串

给定一个字符串,逐个翻转字符串中的每个单词。

说明

  • 单词的构成:无空格字母构成一个单词
  • 输入字符串是否包括前导或者尾随空格?可以包括,但是反转后的字符不能包括
  • 如何处理两个单词间的多个空格?在反转字符串中间空格减少到只含一个

标签

字符串处理

code

class Solution {
public:
/**
* @param s : A string
* @return : A string
*/
string reverseWords(string s) {
// write your code here
string word, line;
int size=s.size();
int i=size-1, j, begin=size-1, end=size-1; if(s.empty())
return string(); while(i >= 0) {
while(s[i]==' ' && i>=0) {
begin--;
end--;
i--;
}
while(s[i]!=' ' && i>=0) {
begin--;
i--;
}
word.resize(0);
for(j=begin+1; j<=end; j++)
word.append(1, s[j]);
}
if(begin == 0)
line = line + word;
else
line = line + word + " ";
end = begin;
}
return line;
}
};

LintCode-53.翻转字符串的更多相关文章

  1. lintcode :Reverse Words in a String 翻转字符串

    题目: 翻转字符串 给定一个字符串,逐个翻转字符串中的每个单词. 样例 给出s = "the sky is blue",返回"blue is sky the" ...

  2. [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

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

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

  4. [CareerCup] 1.2 Reverse String 翻转字符串

    1.2 Implement a function void reverse(char *str) in C or C++ which reverses a null-terminated string ...

  5. [LeetCode] Reverse Words in a String III 翻转字符串中的单词之三

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

  6. [LeetCode] Reverse String II 翻转字符串之二

    Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...

  7. [Swift]LeetCode151. 翻转字符串里的单词 | Reverse Words in a String

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

  8. C#版(击败100.00%的提交) - Leetcode 151. 翻转字符串里的单词 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  9. LeetCode 151 翻转字符串里的单词

    题目: 给定一个字符串,逐个翻转字符串中的每个单词. 示例 1: 输入: "the sky is blue" 输出: "blue is sky the" 示例 ...

随机推荐

  1. vue中将html字符串转换成html后的一些问题

    今天整理之前做vue项目时遇到的一些问题,发现了当时遇到的一个很小但是又会引出很多问题的一个问题(总之就是很有意思,听我慢慢给你到来),这个问题就是当时处理后端数据时,如何将后端返回来的字符串转换成h ...

  2. 随机返回经典语句接口API

    api接口:https://www.liutianyou.com/api/?type=js&charset=utf-8 可以单独将上面链接,在浏览器中查看效果 ​ 这是get请求,参数:typ ...

  3. 【Spark】Spark2.x版的新特性

    一.API 1. 出现新的上下文接口:SparkSession,统一了SQLContext和HiveContext,并且为SparkSession开发了新的流式调用的configuration API ...

  4. win10安装kali组双系统

    一.镜像下载: 根据需求下载自己需要的版本 从官网下载kali 2018.2 的安装包:https://www.kali.org/downloads/ 二.烧录: 这里推荐用 win32 disk i ...

  5. 通过burpsuite替换cookie登录后台

    通过burpsuite可以比较方便的替换http头部的cookie.useragent等字段,在获取到用户的cookie后实现登录.具体使用方法如下: 如替换cookie,可以写正则表达式^Cooki ...

  6. 使用boost.asio实现网络通讯

    #include <boost/asio.hpp> #define USING_SSL //是否加密 #ifdef USING_SSL #include <boost/asio/ss ...

  7. Canvas在移动端设备上模糊出现锯齿边

    在绘制的过程中画布内容的实际大小是根据 canvas 的 width 与 height 属性设置的,而 style 或者CSS设置的width 与 height 只是简单的对画布进行缩放. canva ...

  8. 怎样才能使用ChipScope 加入被优化掉的信号

    在调试过程中常常遇到的一个问题就是,xilinx工具在逻辑综合的过程中,将自己RTL代码中的很多变量都优化掉了,使得调试的抓信号的过程很纠结.以下是解决方法: 1.右键synthesis,在综合选项里 ...

  9. Strange RadioButton group behavior with ToolBar

    原文地址:https://social.msdn.microsoft.com/Forums/vstudio/zh-CN/83352293-ca52-4e22-8092-8e23c453bc75/str ...

  10. 初识主席树_Prefix XOR

    主席树刚接触觉得超强,根本看不懂,看了几位dalao的代码后终于理解了主席树. 先看一道例题:传送门 题目大意: 假设我们预处理出了每个数满足条件的最右边界. 先考虑暴力做法,直接对x~y区间暴枚,求 ...