剑指offer-面试题58_2-左旋转字符串-字符串
2024-09-06 19:27:30
/*
题目:
将字符串的前sep个字符转移到字符串尾部。
*/
/*
思路:
更好的方法:
先翻转前sep个字符,再翻转后面的字符,最后全体翻转。
*/
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map> using namespace std; void verse(char* pBegin,char* pEnd){
while(pEnd > pBegin){ char temp = *pBegin;
*pBegin = *pEnd;
*pEnd = temp;
pBegin++;
pEnd--;
}
} char* ReverseSentence(char* pData,int sep){
char* pBegin = pData;
char* pEnd = pData;
int length = 0;
while(*pEnd != '\0'){
pEnd++;
length++;
}
if(sep > length) return nullptr;
pEnd--;
verse(pBegin,pBegin+sep-1);
verse(pBegin+sep,pEnd);
verse(pBegin,pEnd); return pData; } int main(){
char pData[] = "abcdefg";
char* res = ReverseSentence(pData,2);
while(*res != '\0'){
cout<<*res;
res++;
}
}
最新文章
- Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分
- Torque2D MIT 学习笔记(27) ---- ImageFont的使用以及字体ImageAsset的工具生成
- UI基础UIButton
- 读书笔记 effective c++ Item 50 了解何时替换new和delete 是有意义的
- 微信退款流程,以及在过程中遇见的错误和解决方式(php 语言)
- ES6学习笔记(字符串和数值)
- 从零开始的ESP8266探索(1)-使用Server功能搭建Web Server
- 斯坦福大学公开课机器学习:machine learning system design | error metrics for skewed classes(偏斜类问题的定义以及针对偏斜类问题的评估度量值:查准率(precision)和召回率(recall))
- CSS基础以及属性
- javascript宏任务和微任务
- 【UVA 11404】Palindromic Subsequence
- Docker容器学习梳理 - 容器时间跟宿主机时间同步
- 『TensorFlow』迁移学习
- Spring Boot + Spring Cloud 实现权限管理系统 后端篇(十一):集成 Shiro 框架
- Linux 安装操作系统标准
- Spark中cache和persist的区别
- Devexpress ChartControl 柱状图简单例子
- MySql的InnoDB存储引擎--索引
- tensorflow运行出现错误 : ImportError: Could not find &#39;cudart64_90.dll&#39;.
- Educational Codeforces Round 45 (Rated for Div. 2) F - Flow Control