题目来源:leetcode 题库 344.反转字符串
使用双指针
题目要求原地修改,直接修改传入指针即可
left = 0;
right = sSize - 1;
不满足left < right
void reverseString(char* s, int sSize){
int left = 0;
int right = sSize - 1;
char temp;
while(left < right)
{
temp = s[left];
s[left] = s[right];
s[right] = temp;
left++;
right--;
}
}
题目来源:541. 反转字符串II
基本思路
略
越界了,回头再看
==20==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000035 at pc 0x55c57483083f bp 0x7ffe22d48480 sp 0x7ffe22d48470 READ of size 1 at 0x602000000035 thread T0 #2 0x7f9f699e1082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082) 0x602000000035 is located 0 bytes to the right of 5-byte region [0x602000000030,0x602000000035)
static void MyReverseString(char* s, int start, int end){
char temp;
while(start < end)
{
temp = s[start];
s[start] = s[end];
s[end] = temp;
start++;
end--;
}
}
char * reverseStr(char * s, int k){
char* Ret = s;
int start = 0;
int end = 0;
int len = 0;
while(s[end] != '\0')
{
len++;
end++;
if(len == (2 * k))
{
MyReverseString(s, start, start + k -1);
start = end;
len = 0;
}
}
if((len > 1) && (len < k))
{
MyReverseString(s, start, end - 1);
}
else if(len < (2 *k))
{
MyReverseString(s, start, start + k -1);
}
return Ret;
}
题目来源:leetcode LCR 122.路径加密
字符串以'\0'为结束符,遍历一遍,将'.'的字符替换成' '即可
char* pathEncryption(char* path) {
char* head = path;
while((*path) != '\0')
{
if((*path) == '.')
{
*path = ' ';
}
path++;
}
return head;
}
原题被删了,原题:
题目:剑指Offer 05.替换空格
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
示例 1: 输入:s = "We are happy."
输出:"We%20are%20happy."
遍历一遍,判断有几个空格,得到最后的产犊,malloc创建数组空间;
从字符串尾到字符串首 遍历,赋字符
题目来源:leetcode 题库 151.翻转字符串里的单词
先去除首尾及中间多余的空格
若直接翻转字符串
所以去除多余空格,再翻转字符串,再识别反转后字符串里的单词将其翻转
使用双指针,fast指针从字符串头到尾遍历(s[fast] = '\0'即为队尾)
若s[fast]不为空格,将fast索引的值赋给slow: s[slow++] = s[fast++]
若s[fast]为空格,
若slow为0,在队首,不赋值
若s[slow - 1]也为空格,中间多余空格,不赋值
遍历完成后,剩余字符串长度为slow
遍历完成后,队尾的空格还没处理,检查队尾若为空格,slow--;
/* 去除多余空格 */
while(s[fast] != '\0')
{
if((s[fast] == ' ') && ((slow == 0) || (s[slow - 1] == ' ')))
{
fast++;
}
else
{
s[slow++] = s[fast++];
}
}
/* 还有结尾的空格需要去除,此时slow为字符串长度 */
while(s[slow -1] == ' ')
{
slow--;
}
len = slow;
static void MyReverseString(char* s, int start, int end){
char temp;
while(start < end)
{
temp = s[start];
s[start] = s[end];
s[end] = temp;
start++;
end--;
}
}
/* 调用MyReverseString翻转字符串 */
MyReverseString(s, 0, slow-1);
由于已删除多余的空格,fast从字符串头遍历到尾,若s[fast]为空格,找到一个单词
slow记录单词的开始
遍历结束后,最后一个单词是以'\0'结尾的,还没翻转,将之翻转
/* 找出反转后的字符串里的单词,翻转之 */
slow = 0;
for(fast = 0; fast < len; fast++)
{
if(s[fast] == ' ')
{
MyReverseString(s, slow, fast-1);
slow = fast+1;
}
}
MyReverseString(s, slow, fast-1);
由于函数返回是个指针,也没有返回长度,调用者会以字符串对返回的指针进行处理,由于我们删除了多余的空格,字符串的长度已经改变,我们要在s[字符串长度]处赋'\0'说明字符串到此结束
static void MyReverseString(char* s, int start, int end){
char temp;
while(start < end)
{
temp = s[start];
s[start] = s[end];
s[end] = temp;
start++;
end--;
}
}
char * reverseWords(char * s){
int fast = 0;
int slow = 0;
int len;
char* start = s;
/* 去除多余空格 */
while(s[fast] != '\0')
{
if((s[fast] == ' ') && ((slow == 0) || (s[slow - 1] == ' ')))
{
fast++;
}
else
{
s[slow++] = s[fast++];
}
}
/* 还有结尾的空格需要去除,此时slow为字符串长度 */
while(s[slow -1] == ' ')
{
slow--;
}
len = slow;
/* 翻转字符串 */
MyReverseString(s, 0, slow-1);
/* 找出反转后的字符串里的单词,翻转之 */
slow = 0;
for(fast = 0; fast < len; fast++)
{
if(s[fast] == ' ')
{
MyReverseString(s, slow, fast-1);
slow = fast+1;
}
}
MyReverseString(s, slow, fast-1);
s[fast] = '\0';
return s;
}
leetcode题已删除:leetcode LCR 182.动态口令
解析:代码随想录
字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。
示例 1:
输入: s = "abcdefg", k = 2
输出: "cdefgab"
示例 2:
输入: s = "lrloseumgh", k = 6
输出: "umghlrlose"
限制:
1 <= k < s.length <= 10000
某公司门禁密码使用动态口令技术。初始密码为字符串 password
,密码更新均遵循以下步骤:
target
password
前 target
个字符按原顺序移动至字符串末尾请返回更新后的密码字符串。
创建target个字符的空间存储前target个字符,将字符串整体左移target, 将后target个字符赋值为初始的前target个字符
非暴力想不出来
看题解思路吧
将一个字符串翻转后,前 n个字符就变成了翻转后的后n个字符(虽然顺序翻转了),把这部分再翻转过来,就是前n个字符整体移动到了最后n个字符。另一部分也要翻转才能保持原来顺序不变
因此步骤为
static void MyReverseString(char* s, int start, int end){
char temp;
while(start < end)
{
temp = s[start];
s[start] = s[end];
s[end] = temp;
start++;
end--;
}
}
char* dynamicPassword(char* password, int target) {
int len = strlen(password);
MyReverseString(password, 0, target-1);
MyReverseString(password, target, len-1);
MyReverseString(password, 0, len-1);
return password;
}