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

您在真实的面试中是否遇到过这个题?

Yes
说明

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

方法1:从后往前依次遍历源字符串src,每次遍历完一个单词后,直接将该单词整个拷贝到另一个字符串dst中,依次遍历原字符串,分别将每个独立的单词拷贝到dst中。

在拷贝字符串是先判断该字符串是否全是空格如果是则不拷贝。

实现:


class Solution {
public:
/*
* @param s: A string
* @return: A string
*/ void *reverse(char *src, char *dst)
{
char *p1, *p2;
if(src == NULL || dst == NULL)
{
return NULL;
}
//从src的最后一个字符开始遍历
p1 = src + strlen(src) - ;
p2 = p1;
while (p1 != src)
{
if (*p1 == ' ')
{
int len = p2 - p1;//单词的长度 memcpy(dst, p1 + , len);
//每个单词的末尾加上一个空格
dst += len;
if(len>)
*dst++ = ' '; p1--;
p2 = p1;
}
else
{
//不断将p1向前移动
p1--;
}
}
//最后一次拷贝单词
int len = p2 - p1 + ;
memcpy(dst, p1, len);
dst += len;
*dst++ ='\0'; } string reverseWords(string &s) {
int i;
int j;
string cstr;
char *str=(char*)calloc(,s.length());
char *dst=(char*)calloc(,s.length());
memcpy(str,s.c_str(),s.length()); if(s.length()==)
return cstr.append(""); reverse(str,dst); return cstr.append(dst);
}
};


方法2:先将每个单词分成独立的几部分,然后分别对它们进行翻转,返回将整个字符串进行翻转

 实现:

 class Solution {
public:
/*
* @param s: A string
* @return: A string
*/ void reverse(char *str, int low, int hight) {
while (low < hight) {
int temp;
temp = str[hight];
str[hight] = str[low];
str[low] = temp;
low++;
hight--;
}
} void space(char *str)
{
int i=;
int j=;
char *s=str; //去掉字符串头部的空格
while(str[i]!='\0')
{
if(str[i]!=' ')
break;
i++;
} while(str[i]!='\0')
{
if(str[i]==' ')
{
s[j]=str[i];
while(str[i]==' ')
i++;
j++;
}else
{
s[j]=str[i];
i++;
j++;
}
}
s[j]='\0';
} string reverseWords(string &s) {
int i;
int j;
string cstr;
char *str=(char*)calloc(,s.length());
memcpy(str,s.c_str(),s.length());
int len = strlen(str);
int num = ; for (int i = ; i <= len; i++) {
if (*(str + i) == ' ' || *(str + i) == '\0') {
reverse(str, i - num, i - );
num = ;
} else {
num++;
}
}
reverse(str, , len - );
space(str); return cstr.append(str);
}
};

lintcode-->翻转字符串的更多相关文章

  1. LintCode翻转字符串问题 - python实现

    题目描述:试实现一个函数reverseWords,该函数传入参数是一个字符串,返回值是单词间做逆序调整后的字符串(只做单词顺序的调整即可). 例如:传入参数为"the sky is blue ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. windows下C++实现遍历本地文件

    1.假设本地 d:/ 下存放着0.txt,1.txt两个文件 2.开发工具VS,开发语言C++,怎么遍历得到两个文件呢? 废话不多,具体代码请看下面: /** * 入参:文件存放文件夹路径,例如D:\ ...

  2. Scala偏函数与部分函数

    函数 1.部分函数 部分应用函数(Partial Applied Function)是缺少部分参数的函数,是一个逻辑上概念. def sum(x: Int, y: Int, z: Int) = x + ...

  3. 转:getContextPath、getServletPath、getRequestURI的区别

    假定你的web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下结果 ...

  4. Spine输出资源一键入Unity3D工具代码

    之前预研过2D骨骼动画编辑工具SPINE,感觉其比cocosStudio及Unity3D自带的骨骼动画编辑器(原生Sprite Tree或Uni2D)要更适合有3DSMax习惯的美术,即Spine更容 ...

  5. 开源WebGIS实施方案(六):空间数据(PostGIS)与GeoServer服务迁移

    研发环境的变更,或者研发完成进行项目现场实施.运维的时候,经常就会面临数据及服务的迁移,这其中就包含空间数据以及GeoServer服务的迁移工作. 这里需要提醒的是:如果采用的是类似的开源WebGIS ...

  6. Java泛型与Restlet客户端

    写一个与restlet服务器通信的客户端类,用于测试通信是否成功,并且进行交互.为了方便其他人使用,于是,写一个通用的方法封装起来,可是中途却放生了一些问题. 按照正常写法,顺序走下来是这样的: pu ...

  7. Java反射reflection与注解annotation的应用(自动测试机)

    一.关于自动测试机 1.什么是自动测试机? 对类中的指定方法进行批量测试的工具 2.自动测试机有什么用? a.避免了冗长的测试代码 当类中的成员方法很多时,对应的测试代码可能会很长,使用测试能够让测试 ...

  8. C#全局键盘监听(Hook)

    一.为什么需要全局键盘监听? 在某些情况下应用程序需要实现快捷键执行特定功能,例如大家熟知的QQ截图功能Ctrl+Alt+A快捷键,只要QQ程序在运行(无论是拥有焦点还是处于后台运行状态),都可以按下 ...

  9. 构建NetCore应用框架之实战篇(六):BitAdminCore框架架构小结

    本篇承接上篇内容,如果你不小心点击进来,建议从第一篇开始完整阅读,文章内容继承性连贯性. 构建NetCore应用框架之实战篇系列 一.小结 1.前面已经完成框架的第一个功能,本篇做个小结. 2.直接上 ...

  10. C# TinyMapper

    https://github.com/TinyMapper/TinyMapper 据说性能蛮高的,我没对比测试过. NuGet安装下. 新增三个实体. public class A { public ...