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" 示例 ...
随机推荐
- n&&m and n||m 的区别
今天写一道题老是WA最后才发现问题出在了这个地方, 题目说的是当输入的n和m 都为0的时候,结束输入. 于是乎,条件我就写成了while(n&&m),其实这句话的意思是:只有m和n都不 ...
- 四则运算 Java (于泽浩,袁浩越)
GitHub 地址 一. 项目要求 题目 实现一个自动生成小学四则运算题目的命令行程序. 需求(全部完成) 使用 -n 参数控制生成题目的个数 Myapp.exe -n 10 使用 -r 参数控制题目 ...
- WindowsPhone 8.1 语音命令资料
快速入门:语音命令(XAML) http://msdn.microsoft.com/library/windows/apps/xaml/dn630430.aspx/ 语音命令元素和属性参考(XAML) ...
- 万恶的KPI、新兴的OKR及让人纠结的程序员考核
最近两天在研究研发部门如何进行绩效管理(其实一直都在思考,关注,实践,总感觉无从下手,也想求助咨询公司,无奈囊中羞涩).查了两天的资料,主要的方向是KPI,OKR,谷歌等互联网公司的考核方法.这里做个 ...
- JaveScript之CSS变量
大概是CSS3吧,出了一个叫CSS变量的东西,也叫自定义属性,还是比较有用的东东,可以用JavaScript灵活控制,变量作用 我们来实现一个div跟随鼠标滚动的小东西用来说明如何自定义变量 :roo ...
- ! [rejected] master -> master (non-fast-forward)
当向GitHub远程仓库提交请求时,常会出现 ! [rejected] master -> master (non-fast-forward) 错误. 出现这种错误通常是由于远程仓库的文件版 ...
- The service definition selected is invalid
吐槽下 最近在学Java 听闻Java生态很好 社区很多 但实际操作起来确实另一番风景 不多说了 说正事 添加WebService服务Client时有密码认证得服务 Eclipse抛出 The ser ...
- Trie-648. Replace Words
In English, we have a concept called root, which can be followed by some other words to form another ...
- 读懂Netty的高性能架构之道
Netty是一个高性能.异步事件驱动的NIO框架,它提供了对TCP.UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作都是异步非阻塞的,通过Future-Listener机制,用 ...
- 更换SSL证书
1.申请证书,需要提供完整域名(例如:xxx.aaa.com),会和证书完全匹配. 2.将证书上传到web服务器,例如我的nginx,在server中指定证书路径. 3.重启web服务器.(这个证书和 ...