557. Reverse Words in a String III【easy】

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Example 1:

Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"

Note: In the string, each word is separated by single space and there will not be any extra space in the string.

解法一:

 class Solution {
public:
void reverseSelf(string & s, int start, int end)
{
while (start <= end) {
char c = s[start];
s[start] = s[end];
s[end] = c;
++start;
--end;
}
} string reverseWords(string s) {
int i = ; while (i < s.length()) {
int j = i;
while (j < s.length() && s[j] != ' ') {
++j;
} reverseSelf(s, i, j - ); i = j + ;
} return s;
}
};

无他,注意下标边界尔。

解法二:

 public String reverseWords(String s)
{
char[] s1 = s.toCharArray();
int i = ;
for(int j = ; j < s1.length; j++)
{
if(s1[j] == ' ')
{
reverse(s1, i, j - );
i = j + ;
}
}
reverse(s1, i, s1.length - );
return new String(s1);
} public void reverse(char[] s, int l, int r)
{
while(l < r)
{
char temp = s[l];
s[l] = s[r];
s[r] = temp;
l++; r--;
}
}

参考@sooryaprasanna 的代码

Step 1. Convert the string to char[] array
Step 2. Whenever I encounter a space ' ' , I call the reverse function ( just to keep the code clean )
Step 3. Repeat till the end!

解法三:

 class Solution {
public:
string reverseWords(string s) {
for (int i = ; i < s.length(); i++) {
if (s[i] != ' ') { // when i is a non-space
int j = i;
for (; j < s.length() && s[j] != ' '; j++) { } // move j to the next space
reverse(s.begin() + i, s.begin() + j);
i = j - ;
}
} return s;
}
};

参考@alexander 的代码。

补充一下reverse函数:

reverse函数可以反转一个容器中的内容,包含在<algorithm>库中。

1、函数原型

reverse函数等同于下面的代码:

 template <class BidirectionalIterator> void reverse (BidirectionalIterator first, BidirectionalIterator last)
{
while ((first!=last)&&(first!=--last))
{
std::iter_swap (first,last);
++first;
}
}

reverse函数使用iter_swap来交换两个元素。

2、参数:first、last

first和last是双向迭代器类型,reverse函数反转的范围是[first,last),所以包括first指向的元素,不包括last指向的元素。

3、返回值

reverse函数没有返回值。

参考自:http://blog.csdn.net/u012877472/article/details/49557077

557. Reverse Words in a String III【easy】的更多相关文章

  1. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

  2. 【leetcode_easy】557. Reverse Words in a String III

    problem 557. Reverse Words in a String III solution1:字符流处理类istringstream. class Solution { public: s ...

  3. Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

    题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...

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

  5. Week4 - 500.Keyboard Row & 557.Reverse Words in a String III

    500.Keyboard Row & 557.Reverse Words in a String III 500.Keyboard Row Given a List of words, ret ...

  6. 557. Reverse Words in a String III 翻转句子中的每一个单词

    [抄题]: Given a string, you need to reverse the order of characters in each word within a sentence whi ...

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

  8. 【LeetCode】557. Reverse Words in a String III 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  9. LeetCode 557 Reverse Words in a String III 解题报告

    题目要求 Given a string, you need to reverse the order of characters in each word within a sentence whil ...

随机推荐

  1. URAL 2062 Ambitious Experiment(分块)

    [题目链接] http://acm.timus.ru/problem.aspx?space=1&num=2062 [题目大意] 给出两个操作,操作一给出区间[l,r],对l到r中的每一个下标i ...

  2. 软件配置篇-java下载及安装

    1.进入java官网下载合适版本: 下载地址:https://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.htm ...

  3. Target runtime Apache Tomcat 7.1 is not defined 解决方法

    在工作台目录下找到 自己操作的项目的文件夹 /.settings/org.eclipse.wst.common.project.facet.core.xml 打开后,显示 <?xml versi ...

  4. lync项目总结

    概述 9月份,由于公司人事变动,摆在自己面前也有两条路可选择,一是选择lync,二是选择sharepoint,由于之前,部门老大已经让我看了大概一个月的有关lync方面的资料(文档,代码,项目实施等) ...

  5. Debian、Ubuntu 源列表说明

    转载:http://forum.ubuntu.org.cn/viewtopic.php?t=366506 概貌: 源列表主文件为 /etc/apt/sources.list,另兼取 /etc/apt/ ...

  6. servlet之request和response的使用区分

    有的时候在写servlet程序时,我总是被一个方法该用request去调用.还是用response去调用而困惑.从而造成编程时间的延长. 我在区分request和response的使用时,使用的方法是 ...

  7. linux设备驱动:中断的实现

    一.什么是中断 中断分两种: 1)中断,又叫外部中断或异步中断,它的产生是由于外设向处理器发出中断请求.其中外部中断也有两种,这是由配置寄存器设定的:普通中断请求(IRQ)和快速中断请求(FIQ).一 ...

  8. How to rebuild RPM database on a Red Hat Enterprise Linux system?

    本文是笔者最近遇到的一个故障的处理过程,解决方案是Rebuild RPM 的DB,后面内容其实是REDHAT官方的solutions,不过我遇到的现象和解决方案都与官方有点出入,故一直帖出来: 我遇到 ...

  9. Docker解析及轻量级PaaS平台演练(一)--Docker简介与安装

    Container技术: 传统的虚拟化技术: 通过对硬件层模拟,从而实现了能够在一套硬件上面运行多个操作系统,因为通过硬件虚拟化,使得操作系统认为在它之下就是硬件层 但是实际情况是这样的:虚拟机中的O ...

  10. C# 多线程控制 通讯

    一.多线程的概念  Windows是一个多任务的系统,如果你使用的是windows 2000及其以上版本,你可以通过任务管理器查看当前系统运行的程序和进程.什么是进程呢?当一个程序开始运行时,它就是一 ...