翻转字符串

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

说明

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

标签

字符串处理

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. 可以提高php编程效率的20个要点

    整理了可以提高php编程效率的20个要点,发博客记录一下,需要的朋友可以参考.    1.如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍. 2.$row['id' ...

  2. 对象转换成JSON字符串

    定义一个Student类: 1 class Student { 2 public $name; 3 public $age; 4 function __construct($name, $age) { ...

  3. Java软件开发者,如何学习大数据?

    正常来讲学习大数据之前都要做到以下几点 1.学习基础的编程语言(java,python) 2.掌握入门编程基础(linux操作,数据库操作.git操作) 3.学习大数据里面的各种框架(hadoop.h ...

  4. 2.3 摄像头驱动_vivi驱动程序分析

    学习目标:熟悉vivi的调用过程,分析vivi程序源码的ioctl函数: 一.vivi虚拟视频驱动测试方法 当我们接上usb摄像头设备时,系统会自动给我们安装对应的usb设备驱动程序.如果下次直接测试 ...

  5. C语言跳水比赛预测结果

    5位运动员参加了10米台跳水比赛,有人让他们预测比赛结果A选手说:B第二,我第三:B选手说:我第二,E第四:C选手说:我第一,D第二:D选手说:C最后,我第三:E选手说:我第四,A第一:比赛结束后,每 ...

  6. HyperLedger Fabric 1.4 Solo模式简介(10.1)

    Solo模式指单节点通信模式,该环境中只有一个排序(orderer)服务,从节点(peer)发送来的消息由一个orderer进行排序和产生区块:由于排序(orderer)服务只有一个orderer为所 ...

  7. Java设计模式(15)——行为模式之策略模式(Strategy)

    一.概述 概念 UML简图 角色 二.实践 我们先将上述的UML图的抽象情况下的代码写出,然后再给出一个具体的例子 策略接口——当然如果有一些公共的行为,应当使用抽象类! /** * 策略接口 * * ...

  8. 优步UBER司机全国各地奖励政策汇总 (2月22日-2月28日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  9. 上海Uber优步司机奖励政策(12月20日到12月27日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  10. VINS(二)Feature Detection and Tracking

    系统入口是feature_tracker_node.cpp文件中的main函数 1. 首先创建feature_tracker节点,从配置文件中读取信息(parameters.cpp),包括: ROS中 ...