LintCode 第53题 翻转字符串
  7wIPaP9ltHUV 2023年11月02日 114 0


题目描述:

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


说明:


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


其实这道题的说明很笼统,我们就暂且按照大众网友的理解来做这道题。


样例:

给出s = “ the sky is blue”,返回“blue is sky the”


解题思路:

1.第一种使用Objective-C来写算法是非常简单的,所以不过多介绍,大家看代打都可以看懂。

2.第二种使用C++来写,因为C++语法都忘光了,所以是边写边查字符串的语法,写起来真的累,可能过程也很繁琐,但终究写出来了,需要优化的还有很多。所以立马下载了本Essential C++,打算多熟悉下C++语法。

实现代码:

第一种Objective-C:

NSString * string = @"the sky is blue";
NSMutableArray * stringArray = [[string componentsSeparatedByString:@" "] mutableCopy];
NSMutableArray * tempArray = [NSMutableArray array];
for (int i = 0; i<stringArray.count; i++) {
if (![[stringArray objectAtIndex:i] isEqualToString:@""]) {
[tempArray addObject:[stringArray objectAtIndex:i]];
}
}
[stringArray removeAllObjects];
for (NSInteger i = tempArray.count-1; i>=0; i--) {
[stringArray addObject:[tempArray objectAtIndex:i]];
}


第二种C++:


#include <iostream>
#include <string.h>
using namespace std;

typedef struct {
size_t loc;
size_t length;
}Range;

string stringRotate(const char* str)
{
const size_t length = strlen(str); //拿到str字符串长度
string stdString(str); //拷贝字符串stdString 接下来的操作都根据stdString来做 因为char*类型的字符串不好操作 所以转化为string类型
string endString(str); //拷贝字符串endString 作为最终结果返回的string 因为char*类型的字符串不好操作 所以转化为string类型
Range range; //自定义结构体记录每个单词的开头位置和长度,截取字符串用到
int index = 0;
 for (int i = length-1; i>=0; i--)//从后往前遍历字符串
{
char c = stdString[i];
if (c != ' '&& i>0 && stdString[i-1]==' ') {//如果当前字符是某个单词的开头 则记录下标
range.loc = i;
}
if (c != ' ') { //如果当前字符不是' '则字母长度+1
range.length++;
}
if ((c != ' ' && i>0 && stdString[i-1] == ' ') || (i==0)) { //这里判断是否是每个单词的开头位置
string tempString;
if (i==0 && c == ' ') { //截取字符串
tempString = stdString.substr(1,range.length);
} else {
tempString = stdString.substr(range.loc,range.length);
}

for (int j = 0; j<tempString.length(); j++) { //遍历tempString替换endString内容
endString[j+index] = tempString[j];
}
range.loc = 0;//range清空 需要重新记录
range.length = 0;//range清空 需要重新记录
endString[index+tempString.length()] = ' ';//置endString各个字母间为' '分割
index = index+tempString.length()+1; //记录当前字母要插入endString的位置
}
}
return endString;
}

int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
char * originalStr = "the sky is blue";
string endString = stringRotate(originalStr);
return 0;
}



【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  4WdcduV19eWs   2023年11月02日   65   0   0 ios#includeios#include
  4WdcduV19eWs   2023年11月02日   62   0   0 ios#includeios#include
  4WdcduV19eWs   2023年11月02日   67   0   0 ios#includeios#include
  4WdcduV19eWs   2023年11月02日   54   0   0 ios#includeios#include
  4WdcduV19eWs   2023年11月02日   44   0   0 ios#includeios#include
  ZydNzX6XOBO2   2023年11月02日   113   0   0 出队C++ci#include
  4WdcduV19eWs   2023年11月02日   67   0   0 ios#includeios#include
  4WdcduV19eWs   2023年11月02日   90   0   0 ios#includeios#include
  ZydNzX6XOBO2   2023年11月02日   52   0   0 i++C语言#include
  4WdcduV19eWs   2023年11月02日   51   0   0 ios#includeios#include
  4WdcduV19eWs   2023年11月02日   80   0   0 #includelinuxlinux#include
7wIPaP9ltHUV
作者其他文章 更多