Java中回文数的判断


文章目录

  • Java中回文数的判断
  • 1.字符串回文数判断
  • 2.链表回文数判断
  • 3.双指针(Two Pointers)
  • 3.1 同向指针
  • 3.2 反向指针


1.字符串回文数判断

package com.wyw;

import java.util.Stack;

/**
 * 判断是否为回文字符串
 * @author Administrator
 */
public class HuiWen {
    public static void main(String[] args) {
        System.out.println(f1("12321"));
        System.out.println(f2("abcba"));
        System.out.println(f3("Aa           bcb aA "));
    }

    /**
     * 通过数组的方式进行判断
     * 1.创建两个大小为字符串长度大小的数组
     * 2.对字符串转变为数组
     * 3.把久数组的值放入新数组中
     * 4.进行两个数组的判断
     * 注意:字符串有length()方法,而数组有length属性。
     * @param target
     * @return
     */
    private static boolean f1(String target) {
        char[] str = new char[target.length()];
        char[] newStr = new char[target.length()];
        target.getChars(0,target.length(),str,0);
        for (int i = 0; i < str.length; i++) {
            newStr[i] = str[str.length - 1 -i];
        }
        for (int i = 0; i < str.length; i++) {
            if (str[i] != newStr[i]) {
                return false;
            }
        }
        return true;
    }

    /**
     * 通过栈的方式进行回文数判断
     * 1.首先创建一个栈
     * 2.对栈进行入栈,然后出栈
     * 3.进行判断
     * @param target
     * @return
     */
    public static boolean f2(String target) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < target.length(); i++) {
            stack.push(target.charAt(i));
            //System.out.println(target.charAt(i));
        }
        for (int i = 0; i < target.length(); i++) {
            Character character = stack.pop();
            if (character != target.charAt(i)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 通过指针的方式进行回文数判断
     * 1.首先去除输入字符串的空格,以及使所有的字符变成小写
     * 2.创建两个指针,一个头指针,一个尾指针
     * 3.进行判断
     * @param target
     * @return
     */

    public static boolean f3(String target) {
        String newTarget = target.replaceAll(" ","").toLowerCase();
        System.out.println(newTarget);
        int preIndex = -1;
        int lastIndex = newTarget.length();
        for (int i = 0; i < newTarget.length()/2; i++) {
            preIndex++;
            lastIndex--;
            if (newTarget.charAt(preIndex) != newTarget.charAt(lastIndex)) {
                return false;
            }
        }
        return true;
    }
}

2.链表回文数判断

package com.wyw;

import javax.print.attribute.standard.NumberUp;
import java.util.Stack;

/**
 * 判断是否为回文
 * @author Administrator
 */
public class LinkedHuiWen {
    public static void main(String[] args) {
       Node head = new Node('a');
       Node b1 = new Node('b');
       Node b2 = new Node('a');
       Node b3 = new Node('b');
       Node b4 = new Node('a');

       head.next = b1;
       b1.next = b2;
       b2.next = b3;
       b3.next = b4;

       System.out.println(f2(head));
    }

    /**
     * 1.首先使用栈进行入栈
     * 2.进行出栈,并做比较
     * @param head
     * @return
     */
    static boolean f1(Node head) {
        Node ref = head;
        Stack<Character> stack = new Stack<>();
        while (ref != null){
            stack.push(ref.data);
            ref = ref.next;
        }

        ref = head;
        while (ref != null) {
            Character pop = stack.pop();
            if (pop != ref.data) {
                return false;
            }
            ref = ref.next;
        }
        return true;
    }

    /**
     * 使用快慢指针来解决这个回文
     * @param head
     * @return
     */
    public static boolean f2(Node head) {
        // 快指针一次走两步
        Node q = head;
        // 慢指针一次走一步
        Node s = head;
        while (q != null && q.next !=null) {
            q = q.next.next;
            s = s.next;
        }
        q = s;
        
        // 链表的原地反转
        Node temp = new Node(s.data);
        temp.next = s.next;

        while (temp.next != null) {
            Node pre = s;
            s = temp.next;
            temp.data = s.data;
            temp.next = s.next;
            s.next = pre;
        }

        q.next = null;
        q = head;
        while (q.next != null) {
            if (q.data != s.data) {
                return false;
            }
            q = q.next;
            s = s.next;
        }
        return true;
    }

    static class Node {
        public char data;
        public Node next;

        public Node(char data) {
            this.data = data;
        }
    }
}

3.双指针(Two Pointers)

Java回文数的判断_回文数

3.1 同向指针

Java回文数的判断_System_02


Java回文数的判断_数组_03


Java回文数的判断_System_04

Java回文数的判断_回文数_05

public static int removeDuplicates(int[] nums) {
    // 1 2 2 2 3 4 5
    int i = 0, j = 0;
    while (j < nums.length) {
        if (i == 0 || nums[j] != nums[i - 1]) {
            nums[i] = nums[j];
            i++;
            j++;
        } else {
            j++;
        }
    }
    return i;
}

3.2 反向指针

Java回文数的判断_System_06

编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。
不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。 你可以假设数组中的所有字符都是 ASCII
码表中的可打印字符。

示例 1:

输入:["h","e","l","l","o"]
输出:["o","l","l","e","h"]

示例 2:

输入:["H","a","n","n","a","h"]
输出:["h","a","n","n","a","H"]

Java回文数的判断_数组_07


Java回文数的判断_回文数_08

public  static void reverseString(char[] s) {
        int i = 0 , j = s.length - 1;
        while (i < j) {
            char temp = s[i];
            s[i] = s[j];
            s[j] = temp;
            i++;
            j--;
        }
        System.out.println(Arrays.toString(s));
    }