当前位置:网站首页>牛客网 字符串变形
牛客网 字符串变形
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;
}边栏推荐
- 520. detect capital letters
- 深入了解 border
- "Unusual proxy initial value setting is not supported", causes and Solutions
- pm2 部署 nuxt3.js 项目
- I heard that you are still spending money to buy ppt templates from the Internet?
- Scheme of alcohol concentration tester based on single chip microcomputer
- 普通人没有学历,自学编程可以月入过万吗?
- 目标检测系列——Fast R-CNN
- [e325: attention] VIM editing error
- Applet wx show
猜你喜欢

When programmers are asked if they can repair computers... | daily anecdotes

jupyter入门常见的几个坑:

Data midrange: detailed explanation of the technical stack of data acquisition and extraction

Huawei Router: GRE Technology

On the routing tree of gin
![[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)](/img/b3/b156d75c7b4f03580c449f8499cd74.png)
[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)
![[noi Simulation Competition] send (tree DP)](/img/5b/3beb9f5fdad00b6d5dc789e88c6e98.png)
[noi Simulation Competition] send (tree DP)

【牛客】HJ1 字符串最后一个单词的长度

Spark - the number of leftouterjoin results is inconsistent with that of the left table

Spark - LeftOuterJoin 结果条数与左表条数不一致
随机推荐
uniapp 开发多端项目如何配置环境变量以及区分环境打包
Transplantation of xuantie e906 -- fanwai 0: Construction of xuantie c906 simulation environment
Lu Qi: I am most optimistic about these four major technology trends
[pytorch basic tutorial 30] code analysis of DSSM twin tower model
Get post: do you really know the difference between requests??????
Data middle office: detailed explanation of technical architecture of data middle office
浮点数表示法(总结自CS61C和CMU CSAPP)
[redis realize Secondary killing Business ①] Overview of Secondary killing Process | Basic Business Realization
荐书丨《好奇心的秘密》:一个针尖上可以站多少跳舞的小天使?
【gdb调试工具】| 如何在多线程、多进程以及正在运行的程序下调试
Framework tool class obtained by chance for self use
用VNC Viewer的方式远程连接无需显示屏的树莓派
Essay - Reflection
Linux MySQL installation
Depens:*** but it is not going to be installed
解决:jmeter5.5在win11下界面上的字特别小
GradScaler MaxClipGradScaler
Double pointer analog
Fast and slow pointer series
When to use RDD and dataframe/dataset