当前位置:网站首页>牛客网 字符串变形
牛客网 字符串变形
2022-06-24 07:54:00 【SZU治愈系BUG】
目录
题目描述
对于一个长度为 n 字符串,我们需要对它做一些变形。
首先这个字符串中包含着一些空格,就像"Hello World"一样,然后我们要做的是把这个字符串中由空格隔开的单词反序,同时反转每个字符的大小写。
比如"Hello World"变形后就变成了"wORLD hELLO"。
数据范围: 1\le n \le 10^61≤n≤106 , 字符串中包括大写英文字母、小写英文字母、空格。
进阶:空间复杂度 O(n)O(n) , 时间复杂度 O(n)O(n)
输入描述:
给定一个字符串s以及它的长度n(1 ≤ n ≤ 10^6)
返回值描述:
请返回变形后的字符串。题目保证给定的字符串均由大小写字母和空格构成。
思路分析
要看清楚题目说的是将单词的顺序颠倒。
我的思路比较大众化,为将单词的顺序颠倒,首先将整个字符串的字符顺序颠倒,然后,用空格作为字符串划分单词的判断点,将每一个由空格隔开的部分(即单词)再次颠倒。
为了方便,我尽可能地调用库函数。
AC代码
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
string s;
int n,i,j;
getline(cin,s);
cin>>n;
for(auto& element:s){
if(isupper(element))
element+=32;
else if(islower(element))
element-=32;
}
reverse(s.begin(),s.end());
for(i=0;i<n;i++){
for(j=i;j<n;j++)
if(s[j]==' ')
break;
reverse(s.begin()+i,s.begin()+j);
i=j;
}
cout<<s;
}边栏推荐
- Unable to change the virtual machine power status and report an error solution
- 2022-06-23: given a nonnegative array, select any number to make the maximum cumulative sum a multiple of 7, and return the maximum cumulative sum. N is larger, to the 5th power of 10. From meituan. 3
- Idea another line shortcut
- Threejs glow channel 01 (unrealbroompass & layers)
- Huawei Router: GRE Technology
- Framework tool class obtained by chance for self use
- [Niuke] convert string to integer
- A tip to read on Medium for free
- 开源之夏中选名单已公示,基础软件领域成为今年的热门申请
- 【LeetCode】541. 反转字符串 II
猜你喜欢

Opencv daily function structure analysis and shape descriptor (7) finding polygon (contour) / rotating rectangle intersection

Spark - LeftOuterJoin 结果条数与左表条数不一致

听说你还在花钱从网上买 PPT 模板?

开源之夏中选名单已公示,基础软件领域成为今年的热门申请

浮点数表示法(总结自CS61C和CMU CSAPP)

Data middle office: detailed explanation of technical architecture of data middle office
![[Niuke] convert string to integer](/img/56/3e491b3d0eea0d89a04d0b871501d7.png)
[Niuke] convert string to integer

4274. suffix expression

学习太极创客 — ESP8226 (十三)OTA

The native applet uses canvas to make posters, which are scaled to the same scale. It is similar to the uniapp, but the writing method is a little different
随机推荐
Implementation process of tcpdump packet capturing
Pytorch读入据集(典型数据集及自定义数据集两种模式)
Project deployment related
Opencv daily function structure analysis and shape descriptor (7) finding polygon (contour) / rotating rectangle intersection
【Redis实现秒杀业务①】秒杀流程概述|基本业务实现
When to use RDD and dataframe/dataset
Data middle office: detailed explanation of technical architecture of data middle office
jupyter入门常见的几个坑:
Array opposite pointer series
4275. Dijkstra序列
[quantitative investment] discrete Fourier transform to calculate array period
Numpy numpy中的np.c_和np.r_详解
Epidemic situation, unemployment, 2022, we shouted to lie down!
Pytoch read data set (two modes: typical data set and user-defined data set)
学习太极创客 — ESP8226 (十三)OTA
The native applet uses canvas to make posters, which are scaled to the same scale. It is similar to the uniapp, but the writing method is a little different
What is graph neural network? Figure what is the use of neural networks?
216. combined summation III enumeration method
从华为WeAutomate数字机器人论坛,看政企领域的“政务新智理”
The list of open source summer winners has been publicized, and the field of basic software has become a hot application this year