題目位址:
https://leetcode-cn.com/problems/valid-palindrome-ii/description/
判斷最多删除一個字元,能否形成回文字元串(正反念是一樣的)
給定一個非空字元串 s,最多删除一個字元。判斷是否能成為回文字元串。
示例 1:
輸入: “aba”
輸出: True
示例 2:
輸入: “abca”
輸出: True
解釋: 你可以删除c字元。
注意:
字元串隻包含從 a-z 的小寫字母。字元串的最大長度是50000。
#include<stdio.h>
#include<string.h>
#define MAX 50000
int validPalindrome(char* s);
int main()
{
char s[MAX] = "avbcvbva";
int b = validPalindrome(s);
if(b == )
printf("yes\n");
else
printf("no\n");
return ;
#if 0 //一種注釋方法,0變為1,則代碼可運作
printf("請輸入字元串:\n");
while(scanf("%c",&c))
{
}
#endif
}
int validPalindrome(char* s)
{
int i, j;
int len = strlen(s)/;
int count = ;//允許删除的次數
for(i=, j=strlen(s) - ; i< len; i++, j--)
{
if(s[i] != s[j])
{
if(count == )
return ;
else
{
if(s[i] != s[j-])
{
if(s[i+] != s[j])
return ;
else
{
count--;
i = i+;
}
}
else
{
count--;
j = j-;
}
}
}
}
if(i == len)
return ;
}
/*****************************
Author: 張女名
Time: 2018/4/20
*****************************/