数据结构之字符串、链表、队列、栈、树

一、字符串

public class HuiWenChuan {
    public static void main(String[] args) {
        String str = "A man, a plan, a canal: Panama";
        System.out.println("判断回文串----->>>>>" + isPalindrome(str));
        List<List<String>> lists = partition("aab");
        System.out.println("分割回文串:" + lists);
        int a = StrToInt("-1112");
        System.out.println("把字符串转换成整数----->>>>>" + a);
        boolean flag = wordBreak("leetcode", Arrays.asList("leet","code"));
        System.out.println("单词拆分----->>>>>leetcode返回:" + flag);
        boolean flag1 = wordBreak("applepenapple", Arrays.asList("apple","pen"));
        System.out.println("单词拆分----->>>>>applepenapple返回:" + flag1);
        boolean flag2 = wordBreak("catsandog", Arrays.asList("cats", "dog", "sand", "and", "cat"));
        System.out.println("单词拆分----->>>>>catsandog返回:" + flag2);
    }

    /**
     *  1.验证回文串
     * @param s
     * @return
     */
    public static boolean isPalindrome(String s){
        if (s.length() == 0) {
            return true;
        }
        int l = 0;
        int r = s.length() - 1;
        while (l < r) {
            if (!Character.isLetterOrDigit(s.charAt(l))) {
                l++;
            } else if (!Character.isLetterOrDigit(s.charAt(r))) {
                r--;
            }else {
                if (Character.toLowerCase(s.charAt(l)) != Character.toLowerCase(s.charAt(r))) {
                    return false;
                }
                l++;
                r--;
            }
        }
        return true;
    }

    //回文串,只包含字符串
    public static boolean isPalindroom(String s,int left,int right){
        while (left < right && s.charAt(left) == s.charAt(right)) {
            left++;
            right--;
        }
        return left>=right;
    }

    /**
     * 2.分割回文串
     * https://blog.csdn.net/sunday0904/article/details/70153510
     * @param s
     * @return
     */
    public static List<List<String>> partition(String s) {
        // write your code here
        List<List<String>> result = new ArrayList<List<String>>();
        if(s == null){
            return result;
        }
        List<String> temp = new ArrayList<String>();
        if(s.length() == 0){
            result.add(temp);
            return result;
        }
        search(s , result , temp ,0);
        return result;

    }
    private static void search(String s , List<List<String>> result , List<String> temp , int start){
        if(start == s.length()){
            List<String> p = new ArrayList<String>(temp);
            result.add(p);
            return;
        }
        for(int i = start;i<s.length();i++){
            if(isPartition(s.substring(start , i+1))){
                temp.add(s.substring(start , i+1));
                search(s , result , temp , i+1);
                temp.remove(temp.size() - 1);
            }
        }
    }
    //回文串
    private static boolean isPartition(String temp){
        int i = 0;
        int j = temp.length() - 1;
        while(i<j){
            if(temp.charAt(i) != temp.charAt(j)){
                return false;
            }
            i++;
            j--;
        }
        return true;
    }

    /**
     * 3.把字符串转换成整数
     * @param str
     * @return
     */
    public static int StrToInt(String str) {
        if (str == null || str.length() == 0)
            return 0;
        boolean isNegative = str.charAt(0) == '-';
        int ret = 0;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (i == 0 && (c == '+' || c == '-'))  /* 符号判定 */
                continue;
            if (c < '0' || c > '9')                /* 非法输入 */
                return 0;
            ret = ret * 10 + (c - '0');
        }
        return isNegative ? -ret : ret;
    }

    /**
     * 4.单词拆分
     * @param s 非空字符串 s
     * @param wordDict  一个包含非空单词列表的字典 wordDict
     * @return s 是否可以被空格拆分为一个或多个在字典中出现的单词。
     * https://blog.csdn.net/microopithecus/article/details/88291095
     */
    public static boolean wordBreak(String s, List<String> wordDict) {
        int n=s.length();
        boolean[] dp=new boolean[n+1];
        dp[0]=true;
        for (int i=1;i<=n;i++){
            for (int j = 0; j <i ; j++) {
                if (dp[j]&&wordDict.contains(s.substring(j,i))){
                    dp[i]=true;
                    break;
                }
            }
        }
        return dp[n];

    }

}

二、链表

链表结构:

public class DataNode {

    public int data;
    public DataNode next;
    public DataNode(int data) {
        this.data = data;
    }
    public int getData() {
        return data;
    }
    public void setData(int data) {
        this.data = data;
    }
    public DataNode getNext() {
        return next;
    }
    public void setNext(DataNode next) {
        this.next = next;
    }

}

链表的初始化:

public class DataChain {
    private DataNode head;

    public DataChain(int size) {
        DataNode head = new DataNode(0);
        DataNode cur = head;
        for(int i=1;i<size;i++) {
            DataNode temp = new DataNode(i);
            cur.setNext(temp);
            cur = temp;
        }
        this.head = head;
    }


    public DataNode getHead() {
        return head;
    }

    public void setHead(DataNode head) {
        this.head = head;
    }

    public static void printChain(DataNode head) {
        StringBuilder sb = new StringBuilder();
        DataNode cur = head;
        sb.append(cur.getData());
        while (null != cur.getNext()) {
            sb.append(" -> ");
            sb.append(cur.getNext().getData());
            cur = cur.getNext();
        }
        System.out.println(sb.toString());
    }

    public static void main(String... strings) {
        DataChain chain = new DataChain(10);
        printChain(chain.getHead());
    }

}

链表反转

public class DataNodeTest {
    public static void main(String[] args) {
        DataChain chain = new DataChain(10);
        DataNode node = reverse1(chain.getHead());
        DataChain.printChain(node);
    }

    /**
     * 通过遍历实现
     * @param node
     * @return
     */
    public static DataNode reverse(DataNode node){
        //如何链表没有元素或者链表只有一个元素,不必要反转,返回链表本身就行。
        if(node == null ||node.next == null){
            return node;
        }

        //当链表超过两个及以上就需要反转
        DataNode pre = null;//用于保存当前节点的前一个节点
        DataNode cur = node;//cur保存当前节点

        while(cur != null){
            DataNode next = cur.next;//获取当前节点的下一个元素
            cur.next = pre;//把当前节点的next指向前一个元素
            pre = cur;//把当前节点改为前一个节点(其实就是前一个元素后移一位)。
            cur = next;//把当前节点的下一个节点改为当前节点(其实就是前一个元素后移一位)。
        }

        //因为反转后pre是第一个节点,所以返回pre.
        return pre;
    }

    /**
     * 通过递归实现
     * @param head
     * @return
     */
    public static DataNode reverse1(DataNode head) {
        if (null == head || null == head.getNext())
            return head;
        DataNode revHead = reverse1(head.getNext());
        head.getNext().setNext(head);
        head.setNext(null);
        return revHead;
    }
}

三、二叉树

树结构:

public class BinaryTree {
    int data;//根节点数据
    BinaryTree left;//左子树
    BinaryTree right;//左子树

    public BinaryTree(int data) {
        this.data = data;
        left = null;
        right = null;
    }

    public void insert(BinaryTree root, int data) {
        if (data > root.data) { // 右子节点
            if (root.right == null) {
                root.right = new BinaryTree(data);
            }else {
                insert(root.right, data);
            }
        }else {
            if (root.left == null) {
                root.left = new BinaryTree(data);
            }else {
                insert(root.left, data);
            }
        }
    }
}

测试代码:

public class BinaryTreeTest {
    public static void main(String[] args) {
        int[] array={12,11,4,7,34,23,56,43,22,11,55,6};
        BinaryTree root = new BinaryTree(array[0]);
        for(int i=1;i<array.length;i++) {
            root.insert(root, array[i]);
        }
        System.out.println("----------前序遍历");
        preOrder(root);
        System.out.println("----------中序遍历");
        inOrder(root);
        System.out.println("----------后序遍历");
        postOrder(root);
    }

    //前序遍历
    private static void preOrder(BinaryTree root) {
        if (root != null) {
            System.out.println("data: " + root.data);
            preOrder(root.left);
            preOrder(root.right);

        }
    }

    //中序遍历
    private static void inOrder(BinaryTree root) {
        if (root != null) {
            inOrder(root.left);
            System.out.println("data: " + root.data);
            inOrder(root.right);
        }
    }

    //后序遍历
    private static void postOrder(BinaryTree root) {
        if (root != null) {
            postOrder(root.left);
            postOrder(root.right);
            System.out.println("data: " + root.data);
        }
    }
}

四、数组实现栈

栈的实现:

public class ArrayStack<T> {
    private static final int DEFAULT_SIZE = 12;
    private T[] mArray;
    private int count;

    public ArrayStack(Class<T> type) {
        this(type, DEFAULT_SIZE);
    }

    public ArrayStack(Class<T> type, int size) {
//        Object array = Array.newInstance(type, 10);//创建一个长度为10的字符串数组,在Java中数组也可以作为Object对象
        mArray = (T[]) Array.newInstance(type, size);//创建数组
        count = 0;
    }

    //入栈
    public void push(T val) {
        mArray[count++] = val;
    }
    //出栈
    public T pop (){
        T ret = mArray[count - 1];
        count--;
        return ret;
    }
    //栈顶元素
    public T peek(){
        return mArray[count - 1];
    }
    //栈大小
    public int size(){
        return count;
    }
    //栈是否为空
    public boolean isEmpty(){
        return count == 0;
    }
    //打印栈元素
    public void print(){
        if (isEmpty()) {
            System.out.printf("stack is Empty\n");
        }

        int i = size() - 1;
        while (i >= 0) {
            System.out.println(mArray[i]);
            i--;
        }
    }

}

测试代码:

public class ArrayStackTest {
    public static void main(String[] args) {
        ArrayStack<String> arrayStack = new ArrayStack<>(String.class);
        arrayStack.push("!");
        arrayStack.push("程序猿");
        arrayStack.push("是");
        arrayStack.push("我");

        arrayStack.print();

        System.out.println("出栈元素:" + arrayStack.pop());

        System.out.println("返回栈顶元素:" + arrayStack.peek());


    }
}

五、数组实现队列

猜你喜欢

转载自www.cnblogs.com/heqiyoujing/p/11210780.html