680. 验证回文字符串 Ⅱ

  1. class Solution {
  2. public boolean validPalindrome(String s) {
  3. int left = 0;
  4. int right = s.length() - 1;
  5. while (left <= right) {
  6. if (s.charAt(left) == s.charAt(right)) {
  7. ++left;
  8. --right;
  9. } else {
  10. return judge(s, left, right - 1) || judge(s, left + 1, right);
  11. }
  12. }
  13. return true;
  14. }
  15. private boolean judge(String s, int left, int right) {
  16. while (left <= right) {
  17. if (s.charAt(left) == s.charAt(right)) {
  18. ++left;
  19. --right;
  20. } else {
  21. return false;
  22. }
  23. }
  24. return true;
  25. }
  26. }