剑指offer第二版题解系列(四)

Java 同时被 2 个专栏收录
7 篇文章 0 订阅
7 篇文章 0 订阅

写在开头:正值毕业生求职期间,翻一翻剑指offer的书籍,将其中的题目进行分析和代码编写,并将代码整理到csdn博客上,如果有错误,敬请指出!(此系列不提供题目原文,请参考书籍《剑指offer第二版》即可。)


Test30 - Test39系列

1. Test30.java
import java.util.Stack;
public class Test30{
    Stack s1 = new Stack();
    Stack s2 = new Stack();
    public void push(int num)
    {
        if(s1.isEmpty()&&s2.isEmpty())
        {
            s1.push(num);
            s2.push(num);
        }
        else if(!s2.isEmpty())
        {
            s1.push(num);
            if(num<s2.peek())
                s2.push(num);
            else
                s2.push(s2.peek());
        }
    }
    public int pop()
    {
        if(s1.isEmpty())
            return -1;


            int x = s1.pop();
            s2.pop();
        return x;
    }
    public int min()
    {
        if(s2.isEmpty())
            return -1;
        else
            int x = s2.pop();
        return x;
    }
    public static void main(String[] args)
    {

    }
}
2. Test31.java
import java.util.Stack;
public class Test31{
    public static boolean popPush(int[] push,int[] pop) {
        if(push==null&&pop==null)
            return true;
        if(push==null||pop==null||push.length<1||pop.length<1||push.length!=pop.length)
            return false;
        Stack<Integer> s = new Stack<Integer>();
        int popIndex = 0;
//        Stack s2 = new Stack();
        for(int i=0;i<push.length;i++)
        {
            s.push(push[i]);
            while(!s.isEmpty()&&s.peek()==pop[popIndex]) {
                s.pop();
                popIndex++;
            }
        }
        if(s.isEmpty()&&popIndex==pop.length)
            return true;
        else
            return false;

    }
    public static void main(String[] args)
    {
        int[] push = {1,2,3,4,5};
        int[] pop = {4,5,3,1,2};
        boolean flag = false;
        flag = popPush(push,pop);
        System.out.println(flag);
    }


}
3. Test32.java
import java.util.Queue;
import java.util.LinkedList;
public class Test32{
    public static class BinaryTree
    {
        int data;
        BinaryTree left;
        BinaryTree right;
        public BinaryTree(int data)
        {
            this.data = data;

        }
    }
    public static void updownPrint(BinaryTree root)
    {
        if(root==null)
            return;
        Queue<BinaryTree> queue = new LinkedList<BinaryTree>();
        queue.add(root);
        while(!queue.isEmpty()) {
            BinaryTree node = queue.poll();
            System.out.println(node.data);
            if(node.left!=null) {
                queue.add(node.left);
            }
            if(root.right!=null) {
                queue.add(node.right);
            }

        }
        return;
    }
    public static void main(String[] args)
    {
        BinaryTree t1 = new BinaryTree(8);
        BinaryTree t2 = new BinaryTree(8);
        BinaryTree t3 = new BinaryTree(7);
        BinaryTree t4 = new BinaryTree(9);
        BinaryTree t5 = new BinaryTree(2);
        BinaryTree t6 = new BinaryTree(4);
        BinaryTree t7 = new BinaryTree(7);
        t1.left = t2;
        t1.right = t3;
        t2.left = t4;
        t2.right = t5;
        t3.left = null;
        t3.right = null;
        t5.left = t6;
        t5.right = t7;
        t4.left = null;
        t4.right = null;
        t6.right = null;
        t6.left = null;
        t7.left = null;
        t7.right = null;
        updownPrint(t1);
    }
}
4. Test32_2.java
import java.util.Queue;
import java.util.LinkedList;
public class Test32_2{
    public static class BinaryTree{
        int data;
        BinaryTree left;
        BinaryTree right;

        public BinaryTree(int data)
        {
            this.data = data;
        }
    }
    public static void upDownPrint(BinaryTree root)
    {
        if(root==null)
            return;
        Queue<BinaryTree> queue  = new LinkedList<BinaryTree>();
//        int ce = 1;
//        root.ceng = 1;
        queue.offer(root);
        int nextNode = 0;
        int toBehind = 1;
        while(!queue.isEmpty())
        {
           BinaryTree node = queue.poll();
           System.out.print(node.data+"\t");
            if(node.left!=null) {
//                node.left.ceng = node.ceng + 1;
                queue.offer(node.left);
                nextNode++;
            }
            if(node.right!=null) {
//                node.right.ceng = node.ceng + 1;
                queue.offer(node.right);
                nextNode++;
            }
            toBehind--;
            if(toBehind==0) {
                System.out.println();
                toBehind = nextNode;
                nextNode = 0;

            }

        }
        return;
    }
    public static void main(String[] args){
        BinaryTree t1 = new BinaryTree(8);
        BinaryTree t2 = new BinaryTree(8);
        BinaryTree t3 = new BinaryTree(7);
        BinaryTree t4 = new BinaryTree(9);
        BinaryTree t5 = new BinaryTree(2);
        BinaryTree t6 = new BinaryTree(4);
        BinaryTree t7 = new BinaryTree(7);
        t1.left = t2;
        t1.right = t3;
        t2.left = t4;
        t2.right = t5;
        t3.left = null;
        t3.right = null;
        t5.left = t6;
        t5.right = t7;
        t4.left = null;
        t4.right = null;
        t6.right = null;
        t6.left = null;
        t7.left = null;
        t7.right = null;
        upDownPrint(t1);
    }
}
5. Test32_3.java
import java.util.Stack;
public class Test32_3{
    public static class BinaryTree{
        int data;
        BinaryTree left;
        BinaryTree right;
        public BinaryTree(int data)
        {
            this.data = data;
        }
    }
    public static void zhiprint(BinaryTree root)
    {
        Stack<BinaryTree> s1 = new Stack<BinaryTree>();
        Stack<BinaryTree> s2 = new Stack<BinaryTree>();
        s1.push(root);
        while(!s1.isEmpty()||!s2.isEmpty()) {
            while (!s1.isEmpty()) {
                BinaryTree node1 = s1.pop();
                System.out.print(node1.data);
                if (node1.left != null)
                    s2.push(node1.left);
                if (node1.right != null)
                    s2.push(node1.right);
            }
            System.out.println();
            while (!s2.isEmpty()) {
                BinaryTree node2 = s2.pop();
                System.out.print(node2.data);
                if (node2.right != null)
                    s1.push(node2.right);
                if (node2.left != null)
                    s1.push(node2.left);
            }
            System.out.println();
        }
    }
    public static void main(String[] args){
        BinaryTree t1 = new BinaryTree(1);
        BinaryTree t2 = new BinaryTree(2);
        BinaryTree t3 = new BinaryTree(3);
        BinaryTree t4 = new BinaryTree(4);
        BinaryTree t5 = new BinaryTree(5);
        BinaryTree t6 = new BinaryTree(6);
        BinaryTree t7 = new BinaryTree(7);
        BinaryTree t8 = new BinaryTree(8);
        BinaryTree t9 = new BinaryTree(9);
        BinaryTree t10 = new BinaryTree(10);
        BinaryTree t11 = new BinaryTree(11);
        BinaryTree t12 = new BinaryTree(12);
        BinaryTree t13 = new BinaryTree(13);
        BinaryTree t14 = new BinaryTree(14);
        BinaryTree t15 = new BinaryTree(15);
        t1.left = t2;
        t1.right = t3;
        t2.left = t4;
        t2.right = t5;
        t3.left = t6;
        t3.right = t7;
        t5.left = t10;
        t5.right = t11;
        t4.left = t8;
        t4.right = t9;
        t6.right = t13;
        t6.left = t12;
        t7.left = t14;
        t7.right = t15;
        t8.left = null;
        t8.right = null;
        t9.left = null;
        t9.right = null;
        t10.left = null;
        t10.right = null;
        t11.left = null;
        t11.right = null;
        t12.left = null;
        t12.right = null;
        t13.left = null;
        t13.right = null;
        t14.left = null;
        t14.right = null;
        t15.left = null;
        t15.right = null;
        zhiprint(t1);
    }
}
6. Test33.java
public class Test33{
    public static boolean arrrySearchTree(int[] num,int start,int end)
    {
        if(num==null|num.length<1||start<0||end<0||start>end)
            return false;
        if(start==end)
            return true;
        int root = num[end];
        int i=start;
        for(;i<end;i++)
        {
            if(num[i]>root)
                break;
        }
        System.out.println(i);
        int j = i;
        for(;j<end;j++)
        {
            if(num[j]<root)
                return false;
        }
        boolean left = true;
        if(i>0)
        left = arrrySearchTree(num,start,i-1);
        boolean right = true;
        if(i<=end-1)
        right = arrrySearchTree(num,i,end-1);
        return (left&&right);
    }
    public static void main(String[] args)
    {
        int[] num = {5,7,6,9,11,10,8};
        System.out.println(arrrySearchTree(num,0,6));
    }
}
7. Test34.java
import java.util.ArrayList;
public class Test34{
    public static class BinaryTree{
        int data;
        BinaryTree left;
        BinaryTree right;
        public BinaryTree(int data)
        {
            this.data = data;
        }
    }
    public static void exceptPathSum(BinaryTree root,int exception) {
        if (root == null)
            return;
        int sum = 0;
        ArrayList<Integer> st = new ArrayList<Integer>();
        exception(root, exception, sum, st);
    }
    public static void exception(BinaryTree root,int exception,int sum, ArrayList st)
    {
        st.add(root.data);
        sum = sum + root.data;
        if(root.left==null&&root.right==null)
        {
            if(sum==exception) {
                System.out.print(st);
                System.out.println();
            }
        }
        if(root.left!=null)
        {
            exception(root.left,exception,sum,st);
        }
        if(root.right!=null)
        {
            exception(root.right,exception,sum,st);
        }
        st.remove(st.size()-1);
    }
    public static void main(String[] args) {
        BinaryTree t1 = new BinaryTree(10);
        BinaryTree t2 = new BinaryTree(5);
        BinaryTree t3 = new BinaryTree(12);
        BinaryTree t4 = new BinaryTree(4);
        BinaryTree t5 = new BinaryTree(7);
        t1.left = t2;
        t1.right = t3;
        t2.left = t4;
        t2.right = t5;
        t3.left = null;
        t3.right = null;
        t4.left = null;
        t4.right = null;
        t5.left = null;
        t5.right = null;
        exceptPathSum(t1,22);
    }
}
8. Test35.java
public class Test35{
    public static class Linklist{
        int data;
        Linklist next;
        Linklist psibling;
        public Linklist(int data)
        {
            this.data = data;
        }
    }
    public static Linklist copyLink(Linklist phead)
    {
        if(phead==null)
        return null;
        Linklist linkedhead = copylink1(phead);
        copypsibling(linkedhead);
        Linklist copyhead  = sperateLink(linkedhead);
        return copyhead;
    }
    public static Linklist copylink1(Linklist phead)
    {
        Linklist pheadtd = phead;
        while(pheadtd!=null) {
            Linklist copynode = new Linklist(pheadtd.data);
            copynode.next = pheadtd.next;
            pheadtd.next = copynode;
            pheadtd = copynode.next;
        }
        return phead;
    }
    public static void copypsibling(Linklist phead)
    {
        Linklist pheadtd2 = phead;
        Linklist copyhead = phead.next;
        while(pheadtd2.psibling!=null)
        {
            copyhead.psibling = pheadtd2.psibling.next;
            pheadtd2 = copyhead.next;
            copyhead = pheadtd2.next;
        }
    }
    public static Linklist sperateLink(Linklist phead)
    {
        Linklist phead3 = phead;
        Linklist copyhead = phead.next;
        Linklist copyhead2  =phead.next;
        while(copyhead2.next!=null)
        {
            phead3.next = copyhead2.next;
            phead3 = phead3.next;
            copyhead2.next = phead3.next;
            copyhead2 = copyhead2.next;
        }
        phead3.next = null;
        copyhead2.next = null;
        return copyhead;
    }
    public static void print(Linklist head)
    {
        if(head==null)
            return;
        Linklist chead = head;
        while(chead!=null)
        {
            System.out.print(chead.data);
            if(chead.psibling!=null)
                System.out.print(chead.psibling.data);
            chead = chead.next;
        }
    }
    public static void main(String[] args)
    {
        Linklist n1 = new Linklist(1);
        Linklist n2 = new Linklist(2);
        Linklist n3 = new Linklist(3);
        Linklist n4 = new Linklist(4);
        Linklist n5 = new Linklist(5);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        n5.next = null;
        n1.psibling = n3;
        n4.psibling =n2;
        print(n1);

    }
}
9. Test36.java
public class Test36{
    public static class BinaryTree{
        int data;
        BinaryTree left;
        BinaryTree right;
        public BinaryTree(int data){this.data=data;}
    }
    public static BinaryTree treetransferLink(BinaryTree root)
    {
        if(root==null)
            return null;
        BinaryTree[]  bt = new BinaryTree[1];
        converse(root,bt);
        BinaryTree head = bt[0];
        while(head.left!=null)
            head = head.left;
        return head;
    }
    public static void converse(BinaryTree root,BinaryTree[] bt)
    {
        if(root==null)
            return;
        if(root.left!=null)
            converse(root.left,bt);
        root.left = bt[0];
//        if(bt[0]!=null)
//        System.out.println(bt[0].data);
        if(bt[0]!=null)
            bt[0].right = root;
        bt[0] = root;
        if(root.right!=null)
            converse(root,bt);
    }
    public static void main(String[] args)
    {
        BinaryTree t1 = new BinaryTree(10);
        BinaryTree t2 = new BinaryTree(5);
        BinaryTree t3 = new BinaryTree(12);
        BinaryTree t4 = new BinaryTree(4);
        BinaryTree t5 = new BinaryTree(7);
        t1.left = t2;
        t1.right = t3;
        t2.left = t4;
        t2.right = t5;
        t3.left = null;
        t3.right = null;
        t4.left = null;
        t4.right = null;
        t5.left = null;
        t5.right = null;
        BinaryTree head = treetransferLink(t1);
        System.out.println(head.data);
    }
}
10. Test37.java
import java.lang.StringBuilder;
import java.lang.String;
import java.lang.Integer;
public class Test37 {
    public static  int index =0;
    public static class BinaryTree {
        int data;
        BinaryTree left;
        BinaryTree right;

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

    public static String serialize(BinaryTree root) {
        if (root == null)
            return null;
        StringBuilder sb = new StringBuilder();
        StringBuilder sb2 =  serialize1(root, sb);
        sb2.deleteCharAt(sb2.length()-1);
        String s = sb2.toString();
        return s;

    }

    public static StringBuilder serialize1(BinaryTree root, StringBuilder sb) {
        if(root==null)
        {sb.append("$").append(",");
            return sb;}
        sb.append(root.data).append(",");
        serialize1(root.left,sb);
        serialize1(root.right,sb);
        return sb;
    }
    public static BinaryTree constract(String s)
    {
        if(s==null||s.length()<1)
            return null;
        String[] arr = s.split(",");
        BinaryTree root = null;
        if(!arr[index].equals("$")&&index<arr.length)
        {
            root = new BinaryTree(Integer.valueOf(arr[index]));
            index++;
            root.left = constract(s);
            root.right = constract(s);
        }
        return root;
    }
    public static void printTree(BinaryTree root)
    {
        if(root!=null)
        {
            System.out.println(root.data);
        }
        printTree(root.left);
        printTree(root.right);
    }
    public static void main(String[] args)
    {
        BinaryTree t1 = new BinaryTree(10);
        BinaryTree t2 = new BinaryTree(5);
        BinaryTree t3 = new BinaryTree(12);
        BinaryTree t4 = new BinaryTree(4);
        BinaryTree t5 = new BinaryTree(7);
        t1.left = t2;
        t1.right = t3;
        t2.left = t4;
        t2.right = t5;
        t3.left = null;
        t3.right = null;
        t4.left = null;
        t4.right = null;
        t5.left = null;
        t5.right = null;
        System.out.println(serialize(t1));
        printTree(constract(serialize(t1)));
    }
}
11. Test38.java
public class Test38{
    public static void permutition(char[] arr)
    {
        if(arr==null|arr.length<1)
            return;
        permutition(arr,0);
    }
    public static void permutition(char[] arr,int begin)
    {
        if(begin==arr.length-1)
            System.out.println(new String(arr));
        else {
            for (int i = begin; i < arr.length; i++) {
                char temp = arr[i];
                arr[i] = arr[begin];
                arr[begin] = temp;
                permutition(arr, begin + 1);
                temp = arr[i];
                arr[i] = arr[begin];
                arr[begin] = temp;
            }
        }
    }
    public static void main(String[] args)
    {
        char[] arr = {'a','b','c'};
        permutition(arr);
    }
}
12. Test39.java
public class Test39{
    public static int morehalfnum(int[] arr)
    {
        if(arr==null||arr.length<1)
            return -1;
        int count = 1;
        int temp = arr[0];
        for(int i=1;i< arr.length;i++)
        {
            if(arr[i]==temp)
                count++;
            else
                count--;
            if(count==0) {
                temp = arr[i];
                count = 1;

            }
        }
        return temp;
    }
//    public static int partition(int[] arr,int start,int end)
//    {
//        if(arr==null||arr.length<1||start<0||end<0||start>end)
//            return -1;
//        int i=start;
//        int j =end;
//        int temp = arr[i];
//        if(i<j)
//        {
//            while(arr[j]>temp&&i<j) j--;
//            arr[i] = arr[j];
//            i++;
//        }
//        if(i<j)
//        {
//            while(arr[i]<=temp&&i<j)i++;
//            arr[j] = arr[i];
//            j--;
//        }
//        arr[i] = temp;
//        return i;
//    }
//    public static int morehalfnum(int[] arr)
//    {
//        if(arr==null||arr.length<1)
//            return -1;
//        int start = 0;
//        int end = arr.length-1;
//        int middle = (start+end)/2;
//        int result = 0;
//        result = partition(arr,start,end);
//        while(result<middle) {
//            start = result+1;
//            result = partition(arr,start,end);
//        }
//        while(result>middle)
//        {
//            end = result-1;
//            result = partition(arr,start,end);
//        }
//        return arr[result];
//    }
    //必须要计算一下出现在数组夹中间位置上面的数字出现的次数是否大于数组长度的一半。
//    public static boolean checkishalf(int[] arr,int number)
//    {
//        int count =0;
//        for(int i=0;i<arr.length;i++)
//        {
//            if(arr[i]==number)
//                count++;
//        }
//        if(count*2>arr.length)
//            return true;
//        else
//            return false;
//    }
    public static void main(String[] args)
    {
        int[] arr = {1,2,3,5,2,5,5,5,5};
        System.out.println(morehalfnum(arr));
    }
}
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值