剑指Offer-68道面试题(51-60题)-Java实现

51. 构建乘积数组 – 发散思维能力

题目描述
给定一个数组A[0,1,…,n-1],请构建一个数组B[0,1,…,n-1],其中B中的元素B[i]=A[0]A[1]…*A[i-1]A[i+1]…*A[n-1]。不能使用除法。(注意:规定B[0] = A[1] * A[2] * … * A[n-1],B[n-1] = A[0] * A[1] * … * A[n-2];)

import java.util.ArrayList;
public class Solution {
    public int[] multiply(int[] A) {
        int len = A.length;
        int[] B = new int[len];
        B[0] = 1;
        for (int i = 1; i < len; i++) {
            // B[i] = A[0]*A[1]*...*A[i-1]
            B[i] = B[i - 1] * A[i - 1];
        }
        int temp = 1;
        for (int i = len - 1; i >= 0; i--) {
            B[i] = B[i] * temp;
            // temp = A[n-1]*A[n-2]*...*A[i+1]
            temp *= A[i];
        }
        return B;
    }
}
52. 正则表达式匹配 – 代码的完整性

题目描述
请实现一个函数用来匹配包括’.‘和’‘的正则表达式。模式中的字符’.‘表示任意一个字符,而’'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"abaca"匹配,但是与"aa.a"和"ab*a"均不匹配

public class Solution {
    public static boolean match(char[] str, char[] pattern) {
        if (str == null || pattern == null) {
            return false;
        }
        return matchCore(str, 0, pattern, 0);
    }
    
    public static boolean matchCore(char[] str, int sIndex, char[] pattern,
            int pIndex) {
        int sLen = str.length;
        int pLen = pattern.length;
        if (sIndex == sLen && pIndex == pLen) {
            return true;
        }
        if (sIndex != sLen && pIndex == pLen) {
            return false;
        }
        if (pIndex + 1 < pLen && pattern[pIndex + 1] == '*') {
            if ((sIndex < sLen && str[sIndex] == pattern[pIndex])
                    || (pattern[pIndex] == '.' && sIndex < sLen)) {
                return matchCore(str, sIndex + 1, pattern, pIndex + 2)// *为1
                        || matchCore(str, sIndex + 1, pattern, pIndex)// *为n
                        || matchCore(str, sIndex, pattern, pIndex + 2);// *为0
            } else {
                return matchCore(str, sIndex, pattern, pIndex + 2);// *为0
            }
        }
        if (sIndex < sLen && str[sIndex] == pattern[pIndex]
                || (pattern[pIndex] == '.' && sIndex < sLen)) {
            return matchCore(str, sIndex + 1, pattern, pIndex + 1);
        }
        return false;
    }
}
53. 表示数值的字符串 – 代码的完整性

题目描述
请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100",“5e2”,"-123",“3.1416"和”-1E-16"都表示数值。 但是"12e",“1a3.14”,“1.2.3”,"±5"和"12e+4.3"都不是。

public class Solution {
    public boolean isNumeric(char[] str) {
        String sStr = String.valueOf(str);
        return sStr.matches("[+|-]?\\d*(\\.\\d+)?([e|E][+|-]?\\d+)?");
    }
}
54. 字符流中第一个不重复的字符 – 时间效率与空间效率的平衡

题目描述
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。
输出描述:
如果当前字符流没有存在出现一次的字符,返回#字符。

public class Solution {
    String str = new String();
    int[] hash = new int[256];
    
    // Insert one char from stringstream
    public void Insert(char ch) {
        str += ch;
        hash[ch]++;
    }
    
    // return the first appearence once char in current stringstream
    public char FirstAppearingOnce() {
        for (int i = 0; i < str.length(); i++) {
            if (hash[str.charAt(i)] == 1) {
                return str.charAt(i);
            }
        }
        return '#';
    }
}
55. 链表中环的入口结点 – 代码的鲁棒性

题目描述
给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。

/*
 public class ListNode {
    int val;
    ListNode next = null;
    ListNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    public ListNode EntryNodeOfLoop(ListNode pHead) {
        if (pHead == null) {
            return null;
        }
        ListNode a = pHead;
        ListNode b = pHead;
        boolean flag = false;
        while (a.next != null && a.next.next != null) {
            a = a.next.next;
            b = b.next;
            if (a == b) {
                flag = true;
                break;
            }
        }
        if (!flag) {
            return null;
        }
        ListNode temp = a.next;
        int count = 1;
        while (temp != a) {
            temp = temp.next;
            count++;
        }
        a = pHead;
        b = pHead;
        while (count > 0) {
            a = a.next;
            count--;
        }
        while (b != a) {
            b = b.next;
            a = a.next;
        }
        return a;
    }
}
56. 删除链表中重复的结点 – 数据结构

题目描述
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。
例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

/*
 public class ListNode {
    int val;
    ListNode next = null;
    ListNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    public ListNode deleteDuplication(ListNode pHead) {
        ListNode Head = new ListNode(0);
        Head.next = pHead;
        ListNode pre = Head;
        ListNode last = Head.next;
        while (last != null) {
            if (last.next != null && last.next.val == last.val) {
                while (last.next != null && last.next.val == last.val) {
                    last = last.next;
                }
                pre.next = last.next;
                last = last.next;
            } else {
                pre = pre.next;
                last = last.next;
            }
        }
        return Head.next;
    }
}
57. 二叉树的下一个结点 – 数据结构

题目描述
给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。

/*
public class TreeLinkNode {
    int val;
    TreeLinkNode left = null;
    TreeLinkNode right = null;
    TreeLinkNode next = null;
    
    TreeLinkNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    public TreeLinkNode GetNext(TreeLinkNode pNode) {
        TreeLinkNode result = null;
        if (pNode == null) {
            return result;
        }
        if (pNode.right != null) {
            TreeLinkNode pRight = pNode.right;
            while (pRight.left != null) {
                pRight = pRight.left;
            }
            result = pRight;
        } else if (pNode.next != null) {
            TreeLinkNode pCurrent = pNode;
            TreeLinkNode pParent = pNode.next;
            while (pParent != null && pCurrent == pParent.right) {
                pCurrent = pParent;
                pParent = pParent.next;
            }
            result = pParent;
        }
        return result;
    }
}
58. 对称的二叉树 – 抽象问题形象化

题目描述
请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。

/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    boolean isSymmetrical(TreeNode pRoot) {
        // 前序遍历,和根右左遍历
        return isSymmetricalCore(pRoot, pRoot);
    }
    
    public boolean isSymmetricalCore(TreeNode pRoot, TreeNode pRoot2) {
        if (pRoot == null && pRoot2 == null) {
            return true;
        }
        if (pRoot == null || pRoot2 == null) {
            return false;
        }
        if (pRoot.val != pRoot2.val) {
            return false;
        }
        return isSymmetricalCore(pRoot.left, pRoot2.right)
                && isSymmetricalCore(pRoot.right, pRoot2.left);
    }
}
59. 按之字形顺序打印二叉树 – 抽象问题具体化

题目描述
请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。

import java.util.ArrayList;
import java.util.LinkedList;
/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    public ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        if (pRoot == null) {
            return result;
        }
        ArrayList<Integer> arrayList = new ArrayList<Integer>();
        LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(pRoot);
        int start = 0;
        int end = 1;
        boolean flag = false;
        while (!queue.isEmpty()) {
            TreeNode temp = queue.poll();
            arrayList.add(temp.val);
            start++;
            if (temp.left != null) {
                queue.offer(temp.left);
            }
            if (temp.right != null) {
                queue.offer(temp.right);
            }
            if (start == end) {
                end = queue.size();
                start = 0;
                flag = !flag;
                if (flag) {
                    result.add(arrayList);
                    arrayList = new ArrayList<Integer>();
                } else {
                    ArrayList<Integer> tempList = new ArrayList<Integer>();
                    while (!arrayList.isEmpty()) {
                        tempList.add(arrayList.remove(arrayList.size() - 1));
                    }
                    result.add(tempList);
                }
            }
        }
        return result;
    }
}
public ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        if (pRoot == null) {
            return result;
        }
        Stack<TreeNode> stack1 = new Stack<TreeNode>();
        Stack<TreeNode> stack2 = new Stack<TreeNode>();
        stack1.push(pRoot);
        int layer = 1;
        while (!stack1.isEmpty() || !stack2.isEmpty()) {
            ArrayList<Integer> temp = new ArrayList<Integer>();
            if (layer % 2 != 0) {
                while (!stack1.isEmpty()) {
                    TreeNode tempnode = stack1.pop();
                    temp.add(tempnode.val);
                    if (tempnode.left != null) {
                        stack2.push(tempnode.left);
                    }
                    if (tempnode.right != null) {
                        stack2.push(tempnode.right);
                    }
                }
            } else {
                while (!stack2.isEmpty()) {
                    TreeNode tempnode = stack2.pop();
                    temp.add(tempnode.val);
                    if (tempnode.right != null) {
                        stack1.push(tempnode.right);
                    }
                    if (tempnode.left != null) {
                        stack1.push(tempnode.left);
                    }
                }
            }
            if (!temp.isEmpty()) {
                result.add(temp);
                layer++;
            }
        }
        return result;
    }
60. 把二叉树打印成多行 – 抽象问题具体化

题目描述
从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。

import java.util.ArrayList;
import java.util.Queue;
import java.util.LinkedList;
/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        if (pRoot == null) {
            return result;
        }
        ArrayList<Integer> arrayList = new ArrayList<Integer>();
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(pRoot);
        int start = 0;
        int end = 1;
        while (!queue.isEmpty()) {
            TreeNode temp = queue.poll();
            arrayList.add(temp.val);
            start++;
            if (temp.left != null) {
                queue.offer(temp.left);
            }
            if (temp.right != null) {
                queue.offer(temp.right);
            }
            if (start == end) {
                end = queue.size();
                start = 0;
                result.add(arrayList);
                arrayList = new ArrayList<Integer>();
            }
        }
        return result;
    }
}

其他题目

→→点击跳转

猜你喜欢

转载自blog.csdn.net/weixin_43845524/article/details/106171179