1、题目
给定一个字符串 S
,返回 “反转后的” 字符串,其中不是字母的字符都保留在原地,而所有字母的位置发生反转。
示例 1:
输入:"ab-cd"
输出:"dc-ba"
示例 2:
输入:"a-bC-dEf-ghIj"
输出:"j-Ih-gfE-dCba"
示例 3:
输入:"Test1ng-Leet=code-Q!"
输出:"Qedo1ct-eeLg=ntse-T!"
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reverse-only-letters
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2、分析
3、代码实现
# define true 1
# define false 0
int isZm(char a) {
if (a <= 'z' && a >= 'a')
return true;
if (a <= 'Z' && a >= 'A')
return true;
return false;
}
char * reverseOnlyLetters(char * s){
// char *p = "li bo";
// char s[] = "li bo"; \0 结尾
if (s == NULL)
return NULL;
int i = 0, length = 0;
char tmp;
while (*(s + length) != '\0')
{
length++;
}
printf("s length is %d\n", length);
--length;
char *start = s + 0;
char *end = s + length;
while (start <= end)
{
if (!isZm(*start))
{
++start;
continue;
}
if (!isZm(*end))
{
--end;
continue;
}
tmp = *start;
*start = *end;
*end = tmp;
++start;
--end;
}
return s;
}