严格的来说这并不算什么算法,网上实现回文验证的很多,只不过有些花里胡哨的,顺手写了一下复杂度为O(n),O(1)的回文验证
设置两个指针(索引)从两端向中间移动,若对应位置字符不相等return false;
若左索引小于等于了右索引 return true;
public bool PalindromeVerification(string str)
{
int left = 0, right = str.Length - 1;
while (left < right)
{
if (str[left] == str[right])
{
left++;
right--;
}
else
return false;
}
return true;
}