一、题目内容

image.png

二、题解

解法1:

思路

数组,可以使用双指针
如果是链表,则需要找到中间节点,然后断开,将后半部分reverse,再从头挨个比较

代码

  1. public class Solution {
  2. /**
  3. * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
  4. *
  5. * @param str string字符串 待判断的字符串
  6. * @return bool布尔型
  7. */
  8. public boolean judge (String str) {
  9. // write code here
  10. if(str == null || str.length() == 0){
  11. return true;
  12. }
  13. int left = 0,right = str.length() - 1;
  14. while(left++ <= right--){
  15. if(str.charAt(left) != str.charAt(right)){
  16. return false;
  17. }
  18. }
  19. return true;
  20. }
  21. }