翻转字符串

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

说明

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

标签

字符串处理

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. ruby 正则表达式Regexp

    ruby正则表达式在线编辑器:rubular 一般规则: /a/匹配字符a.      /\?/匹配特殊字符?.特殊字符包括^, $, ? , ., /, \, [, ], {, }, (, ), + ...

  2. Python学习笔记六:集合

    集合 Set,去重,关系测试:交.并.差等:无序 list_1=set(list_1), type(list_1) list_2=set([xxxxx]) 交集:list_1.intersectin( ...

  3. xargs命令的使用过程中一个小领悟:管道与xargs的差别

    对xargs的使用总是比较模糊,大概的理解为:通道中,上一个命令的标准输出逐行作为下一个命令的参数 例如 find /var/temp* | xargs rm -r 功效:找出/var/中所有temp ...

  4. linux之sed基础命令详解

    sed (Stream  EDitor)是一个强大的字符流编辑器,输入一般是来自文件,默认情况下不编辑原文件,仅对模式空间中的数据作处理;而后,将模式空间打印到屏幕显示 sed基础用法 sed [op ...

  5. 如何在同一个Excel里,对两个很相似的工作簿比对出不同之处

    如何在同一个Excel里,对两个很相似的工作簿比对出不同之处

  6. CSS 兼容iPhone X、iPhone XS及iPhone XR

    @media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ra ...

  7. LeetCode:35. Search Insert Position(Easy)

    1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...

  8. SpringBoot入门(一)——开箱即用

    本文来自网易云社区 Spring Boot是什么 从根本上来讲Spring Boot就是一些库的集合,是一个基于"约定优于配置"的原则,快速搭建应用的框架.本质上依然Spring, ...

  9. Mate20兼容性如何?WeTest带你抢先测!

    自从九月份 iPhone XS 系列发布后,WeTest团队迅速入库了iPhone XS和iPhone XR设备,十月份国内巨头华为也重磅推出了一款“Mate 20”设备,让下半年的国内手机市场又热闹 ...

  10. python3 BeautifulSoup模块使用

    BeautifulSoup就是Python的一个HTML或XML的解析库,可以用它来方便地从网页中提取数据.官方解释如下: Beautiful Soup提供一些简单的.Python式的函数来处理导航. ...