剑指offer面试题 java解答26-30

面试题26:复杂链表的复制

import java.util.HashMap;
public class Test26{
    private class Node<T>{
        T valueT=null;
        Node next=null;
        Node sibling=null;
        public Node(T valueT) {
            this.valueT = valueT;
        }
    }
    private Node head=null;
    public Node getHeadNode(){
        return head;
    }
    public <T> Node insertTail(T value){
        Node nextNode=null;
        if (head==null) {
            head=new Node(value);
            return head;
        }else {
            nextNode=head;
            while (nextNode.next!=null) 
            {
                nextNode=nextNode.next;
            }
            nextNode.next=new Node(value);
            return nextNode.next;
        }
    }
    private Node ReconnectNodes(Node head) {
        Node nowNode=head;
        Node cloneNodeHead=null;
        Node cloneNode=null;
        if (head!=null) {
            cloneNodeHead=nowNode.next;
            cloneNode=cloneNodeHead;
            nowNode=cloneNodeHead.next;
            while (nowNode!=null) {
                cloneNode.next=nowNode.next;
                cloneNode=nowNode.next;
                nowNode=cloneNode.next;
            }
        }
        return cloneNodeHead;
    }
    private void ConnectSiblingNodes(Node head) {
        if (head!=null) {
            Node nowNode=head;
            while (nowNode!=null) {
                Node nowCloneNode=nowNode.next;
                if (nowNode.sibling!=null) {
                    nowCloneNode.sibling=nowNode.sibling.next;
                }
                nowNode=nowCloneNode.next;
            }
        }       
    }
    private void CloneNodes(Node head) {
        if (head!=null) {
            Node nowNode=head;
            while (nowNode.next!=null) {
                Node cloneNode=new Node(nowNode.valueT);
                Node nextNode=nowNode.next;
                nowNode.next=cloneNode;
                cloneNode.next=nextNode;
                nowNode=nextNode;
            }
            Node cloneNode=new Node(nowNode.valueT);
            nowNode.next=cloneNode;
        }
    }
    public Node Clone(Node head){
        CloneNodes(head);
        ConnectSiblingNodes(head);
        return ReconnectNodes(head);
    }
    public static void main(String[] args) {
        HashMap<Character, Object> map=new HashMap<Character, Object>();
        Object[] a={'A','B','C','D','E'};
        Test26 t=new Test26();
        for (int i = 0; i < a.length; i++) {
            Node node=t.insertTail(a[i]);
            map.put((Character) a[i], node);
        }
        ((Node)(map.get('A'))).sibling=((Node)(map.get('C')));
        ((Node)(map.get('B'))).sibling=((Node)(map.get('E')));
        ((Node)(map.get('D'))).sibling=((Node)(map.get('B')));
        Node cloneHead=t.Clone(t.getHeadNode());
        if (cloneHead!=null) 
        {
            while (cloneHead!=null) 
            {
                System.out.print(cloneHead.valueT);
                if (cloneHead.sibling!=null) 
                {
                    System.out.println(cloneHead.sibling.valueT);
                }else {
                    System.out.println();
                }
                cloneHead=cloneHead.next;
            }           
        }
    }
}

面试题27:二叉搜索树与双向链表

public class Test27{
    private class Node<T> {
        public T value;
        public Node<T> lChild;
        public Node<T> rChild;
        public Node(T value) {
            this.value = value;
        }
    }
    public Node root=null;
    private int pos = 0;
    public <T> void creatBiTree(T[] value){
        pos=0;
        root=creatBiTree(root, value);
    }
    // 以node为根节点,创建一棵树,返回此根节点
    private <T> Node creatBiTree(Node node, T[] value) {
        T t = value[pos];
        pos++;
        if (t == null) {
            return null;
        } else {
            node = new Node<T>(t);
            node.lChild = creatBiTree(node.lChild, value);
            node.rChild = creatBiTree(node.rChild, value);
        }
        return node;
    }
    public Node Convert(Node root){
        Node lastNode=null;
        Node tailNode=ConvertNode(root, lastNode);
        Node head=tailNode;
        while (head.lChild!=null) {
            head=head.lChild;
        }
        return head;
    }
    private Node ConvertNode(Node nowNode,Node lastNode){
        if (nowNode==null)
        {
            return null;
        }
        if (nowNode.lChild!=null)
        {
            lastNode=ConvertNode(nowNode.lChild, lastNode);
        }
        nowNode.lChild=lastNode;
        if (lastNode!=null)
        {
            lastNode.rChild=nowNode;
        }
        lastNode=nowNode;
        if (nowNode.rChild!=null) {
            lastNode=ConvertNode(nowNode.rChild, lastNode);
        }
        return lastNode;
    }
    public static void main(String[] args) {
        Object[] a = { 10, 6, 4, null, null, 8, null, null, 14, 12, null, null, 16, null, null };
        Test27 t = new Test27();
        t.creatBiTree(a);
        Node head=t.Convert(t.root);
        while (head!=null) {
            System.out.println(head.value);
            head=head.rChild;
        }
    }
}

面试题28:字符串的排列

public class Test28 {
    public void Permutation(String str){
        permutation(str, 0);
    }
    //第i个字符和后面的全排列交换
    private static void permutation(String s, int i) {  
        char[] str=s.toCharArray();
        if (i>s.length()) {
            return ;
        }else if (i==s.length()) {
            System.out.println(String.valueOf(str));
        }else {
            for (int j = i; j < str.length; j++) {
                char tmp=str[j];
                str[j]=str[i];
                str[i]=tmp;
                permutation(String.valueOf(str), i+1);
            }
        }
    }
    public static void main(String[] args) {
        Test28 t=new Test28();
        String str="abcd";
        t.Permutation(str);
    }
}

面试题29:数组中出现次数超过一半的数字

public class Test29 {
    public int MoreThanHalfNum(int[] numbers) throws Exception{
        if (numbers==null) {
            throw new Exception("invalid input");
        }
        int number=numbers[0];
        int times=1;
        for (int i = 1; i < numbers.length; i++) {
            if (numbers[i]!=number) {
                if (--times==0) {
                    number=numbers[i];
                    times=1;
                }
            }else {
                times++;
            }
        }
        int sum=0;
        for (int i = 0; i < numbers.length; i++) 
        {
            if (numbers[i]==number) 
            {
                sum++;
            }
        }
        if (sum>numbers.length/2) {
            return number;
        }else {
            throw new Exception("without");
        }       
    }
    public static void main(String[] args) throws Exception {
        int[] numbers={1,2,3,2,2,2,5,4,2};
        Test29 t=new Test29();
        int number=t.MoreThanHalfNum(numbers);
        System.out.println(number);
    }
}

面试题30:最小的K个数

public class Test30 {
    private static int leftChild(int i)
    {
        return 2*i+1;
    }
    //下滤实现大顶堆
    public static <T extends Comparable<? super T>> void percolateDownSort(T[] a,int i,int n)
    {
        int child;
        T tmp;
        for (tmp=a[i];leftChild(i)<n;i=child) 
        {
            child=leftChild(i);
            if (child<n-1&&a[child].compareTo(a[child+1])<0)
            {
                child++;
            }
            if (tmp.compareTo(a[child])<0) 
            {
                a[i]=a[child];
            }else {
                break;
            }
        }
        a[i]=tmp;
    }
    public static <T extends Comparable<? super T>> T returnMax(T[] a)
    {
        int length=a.length;
        percolateDownSort(a, 0, length);
        return a[0];
    }
    public Integer[] getLeastNumbers(int k,Integer[] a) throws Exception{
        if (a==null||k<0||k>a.length) {
            throw new Exception("invalid input");
        }
        Integer[] result=new Integer[k];
        System.arraycopy(a, 0, result, 0, k);
        if (a.length<k) 
        {
            return result;
        }else {
            for (int i = k; i < a.length; i++) 
            {
                Integer max=returnMax(result);
                if (max>a[i]) 
                {
                    result[0]=a[i];
                }
            }
        }
        return result;
    }
    public static void main(String[] args) throws Exception {
        Integer[] a={4,6,1,2,8,3,0,9,7,5};
        Test30 t=new Test30();
        Integer[] result=t.getLeastNumbers(4, a);
        for (int i = 0; i < result.length; i++) {
            System.out.println(result[i]);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值