lintcode-->翻转字符串
给定一个字符串,逐个翻转字符串中的每个单词。
- 单词的构成:无空格字母构成一个单词
- 输入字符串是否包括前导或者尾随空格?可以包括,但是反转后的字符不能包括
- 如何处理两个单词间的多个空格?在反转字符串中间空格减少到只含一个
方法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-->翻转字符串的更多相关文章
- LintCode翻转字符串问题 - python实现
题目描述:试实现一个函数reverseWords,该函数传入参数是一个字符串,返回值是单词间做逆序调整后的字符串(只做单词顺序的调整即可). 例如:传入参数为"the sky is blue ...
- lintcode :Reverse Words in a String 翻转字符串
题目: 翻转字符串 给定一个字符串,逐个翻转字符串中的每个单词. 样例 给出s = "the sky is blue",返回"blue is sky the" ...
- [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 ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- [CareerCup] 1.2 Reverse String 翻转字符串
1.2 Implement a function void reverse(char *str) in C or C++ which reverses a null-terminated string ...
- [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 ...
- [LeetCode] Reverse String II 翻转字符串之二
Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...
- [Swift]LeetCode151. 翻转字符串里的单词 | Reverse Words in a String
Given an input string, reverse the string word by word. Example: Input: "the sky is blue", ...
- C#版(击败100.00%的提交) - Leetcode 151. 翻转字符串里的单词 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- LeetCode 151 翻转字符串里的单词
题目: 给定一个字符串,逐个翻转字符串中的每个单词. 示例 1: 输入: "the sky is blue" 输出: "blue is sky the" 示例 ...
随机推荐
- [翻译]NUnit---TestCase Attributes(二十一)
TestCaseAttribute (NUnit 2.5) TestCase特性有两个效果,包括标记一个方法使用参数并且在调用的时候提供内置数据.示例如下,本示例会使用不同数据集执行3次: [Test ...
- MySql数据库远程连接失败问题解决
例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话. GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY ...
- vs web项目远程发布到IIS
一.下载安装 IIS安装管理服务,这里不赘述,安装完后显示如下(装完刷新一下或者重新打开iis) 下载webploy,安装的时候要选中远程功能,或者选择完全安装,否则会因为没有远程模块导致连接失败(注 ...
- Linq to SQL 练习
public class HomeController : Controller { // // GET: /Home/ empentity entity = new empentity(); pub ...
- RabbitMQ广播模式
广播模式:1对多,produce发送一则消息多个consumer同时收到.注意:广播是实时的,produce只负责发出去,不会管对端是否收到,若发送的时刻没有对端接收,那消息就没了,因此在广播模式下设 ...
- [TJOI2013]攻击装置(网络流,最小割)
前言 网络流被hbx吊起来打 Solution 考虑一下这个走法是不是和象棋中马的走法一模一样(废话) 那么显然我每一次移动是走三次,如果将棋盘二分图染色一下,不就是每一次只能走到另一个颜色的吗? 然 ...
- day 69 ORM 多表增删改查操作
http://www.cnblogs.com/liwenzhou/p/8660826.html 下面的代码是在 python console中配置的. 关闭pycharm会消失. from app01 ...
- python学习笔记6-集合
# 集合是无序且不可重复的元素的集合 a = set([1,3,1,3,3,2,2,5]) a # {1, 2, 3, 5} b = set(range(2,5)) b # {2, 3, 4} # 1 ...
- webpack快速入门——Json配置文件使用
在实际工作中,我们的项目都会配置一个Json的文件或者说API文件,作为项目的配置文件. 有时候你也会从后台读取到一个json的文件,这节课就学习如何在webpack环境中使用Json. 如果你会we ...
- [JavaScript] 获取昨日前天的日期
var day = new Date(); day.setDate(day.getDate()-1); console(day.pattern('yyyy-MM-dd'));//昨天的日期 day.s ...