当前位置:网站首页>1592. 重新排列单词间的空格
1592. 重新排列单词间的空格
2022-07-30 00:10:00 【Mr Gao】
1592. 重新排列单词间的空格
给你一个字符串 text ,该字符串由若干被空格包围的单词组成。每个单词由一个或者多个小写英文字母组成,并且两个单词之间至少存在一个空格。题目测试用例保证 text 至少包含一个单词 。
请你重新排列空格,使每对相邻单词之间的空格数目都 相等 ,并尽可能 最大化 该数目。如果不能重新平均分配所有空格,请 将多余的空格放置在字符串末尾 ,这也意味着返回的字符串应当与原 text 字符串的长度相等。
返回 重新排列空格后的字符串 。
示例 1:
输入:text = " this is a sentence "
输出:“this is a sentence”
解释:总共有 9 个空格和 4 个单词。可以将 9 个空格平均分配到相邻单词之间,相邻单词间空格数为:9 / (4-1) = 3 个。
示例 2:
输入:text = " practice makes perfect"
输出:"practice makes perfect "
解释:总共有 7 个空格和 3 个单词。7 / (3-1) = 3 个空格加上 1 个多余的空格。多余的空格需要放在字符串的末尾。
示例 3:
输入:text = “hello world”
输出:“hello world”
示例 4:
输入:text = " walks udp package into bar a"
输出:"walks udp package into bar a "
示例 5:
输入:text = “a”
输出:“a”
这题思路上可能没什么难的地方,但是编程的时候,我们需要考虑很多细节问题,还是值得学习的,解题代码如下:
char * reorderSpaces(char * text){
int spacenum=0;
int i;
int wordnum=0;
char * re=(char *)malloc(sizeof(char )*(strlen(text)+5));
for(i=0;text[i]!='\0';i++){
if(text[i]!=' '&&text[i+1]==' '){
wordnum++;
}
if(text[i+1]=='\0'&&text[i]!=' '){
wordnum++;
}
if(text[i]==' '){
spacenum++;
}
if(text[i+1]=='\0'){
break;
}
}
printf("%d %d ",wordnum,spacenum);
if(wordnum==1){
int size=0;
for(i=0;text[i]!='\0';i++){
if(text[i]!=' '){
text[size++]=text[i];
}
}
int j=0;
while(j<spacenum){
text[size++]=' ';
j++;
}
return text;
}
int num=spacenum/(wordnum-1);
int numt=spacenum%(wordnum-1);
int size=0;
printf("- %d %d",numt,num);
for(i=0;text[i]!='\0';i++){
if(text[i]!=' '){
re[size++]=text[i];
}
if(text[i]!=' '&&text[i+1]==' '&&wordnum!=1){
int j=0;
wordnum--;
while(j<num){
re[size++]=' ';
j++;
}
}
if(text[i+1]=='\0'){
break;
}
}
int j=0;
while(j<numt){
re[size++]=' ';
j++;
}
re[size]='\0';
return re;
}
边栏推荐
- CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
- How do we-media people create explosive articles?These 3 types of articles are most likely to explode
- Introduction to Worthington Elastase & Hyaluronidase
- 低压差线性稳压器MPQ2013A-AEC1品牌MPS国产替代
- Since the media how to write a short video title?Three hot style title, let your video gain more traffic
- Mysql internal and external connections
- Worthington解离酶:中性蛋白酶(分散酶)详情解析
- 基于TNEWS‘ 今日头条中文新闻(短文本)分类
- 【经验】经验总结-经验教训
- Worthington Dissociation Enzymes: Trypsin and Frequently Asked Questions
猜你喜欢
随机推荐
Types and check set (set), study T treasure code
新媒体运营必备的4个热点查询网
【集训DAY18】有趣的交换【模拟】【数学】
二维数组的查找
低压差线性稳压器MPQ2013A-AEC1品牌MPS国产替代
Go language serialization and deserialization and how to change the uppercase of the json key value to lowercase if the json after serialization is empty
call、apply 以及 bind 的区别和用法
2022年ps应该选择哪个版本
【集训DAY16】KC‘s Can 【动态规划】
The strongest JVM in the whole network is coming!(Extreme Collector's Edition)
BEVDetNet:Bird‘s Eye View LiDAR Point Cloud based Real-time 3D Object Detection for Autonomous Drivi
NumPy(二)
【集训DAY18】Welcome J and Z 【动态规划】
Some personal understandings about MySQL indexes (partially refer to MySQL45 lectures)
Paper Intensive Reading - YOLOv3: An Incremental Improvement
【集训DAY16】KC ‘ s Stars【dfs】
Comprehensive Practice - Three-Mison Chess Mini Game
“灯塔工厂”的中国路径:智造从点到面铺开
全网最强 JVM 来袭!(至尊典藏版)
Worthington酶促细胞收获&细胞粘附和收获