Easy-50
来源:互联网 发布:js ajax进行post请求 编辑:程序博客网 时间:2024/05/01 11:37
leetcode 541. Reverse String II
Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.
Example:
Input: s = "abcdefg", k = 2Output: "bacdfeg"Restrictions:
- The string consists of lower English letters only.
- Length of the given string and k will in the range [1, 10000]
AC:
void res(char* s,int start,int end)
{
int len=end+start;
len=len/2;
for(int i=start;i<=len;i++)
{
int c=s[i];
s[i]=s[end-i+start];
s[end-i+start]=c;
}
}
{
int len=end+start;
len=len/2;
for(int i=start;i<=len;i++)
{
int c=s[i];
s[i]=s[end-i+start];
s[end-i+start]=c;
}
}
char* reverseStr(char* s, int k) {
int len=strlen(s);
int count=len/(2*k);
for(int i=1;i<=count;i++)
{
res(s,2*k*i-2*k,2*k*i-k-1);
}
int re=len%(2*k);
if(re!=0)
{
if(re>=k)
res(s,count*(2*k),(k-1)+count*(2*k));
else
res(s,count*(2*k),(re-1)+count*(2*k));
}
return s;
}
int len=strlen(s);
int count=len/(2*k);
for(int i=1;i<=count;i++)
{
res(s,2*k*i-2*k,2*k*i-k-1);
}
int re=len%(2*k);
if(re!=0)
{
if(re>=k)
res(s,count*(2*k),(k-1)+count*(2*k));
else
res(s,count*(2*k),(re-1)+count*(2*k));
}
return s;
}
tips: 注意下标与长度之间的转换!!!
阅读全文
0 0
- Easy-50
- ISCC-BASIC-50-easy?
- easy
- Easy
- Easy-题目50:38. Count and Say
- Easy Plugs
- easy problem
- So Easy!
- so easy
- easy explorer
- Easy Explore
- Easy Task
- Easy Task
- Easy CHM
- HDU2699+Easy
- HDU4648+Easy
- Easy billiards
- Easy STL
- 搜狗手机助手联合腾讯御安全 共建APP安全生态环境
- QA实习第5周总结
- Spring Boot Jpa之构建动态SQL查询语句
- JAVA虚拟机体系结构
- VB.NET小区物业管理系统
- Easy-50
- ubuntu+cuda8.0+opencv3 Caffe GPU环境配置
- PostgreSQL9.6:新增加“idle in transaction”超时空闲事务自动查杀功能
- osg和ogre的异同
- C++中的“不完全类型”
- 还原文件打开方式为未知应用程序
- javascript中值传递,地址传递,引用传递的问题(使用js创建list对象时会用到)
- 福尔摩斯的约会
- GAN的统一架构与WGAN