剑指offer面试题 java解答11-15

面试题11:数值的整数次方

public class Test11 {
    public double Power(double base,int exponent) throws Exception{
        Double baseValue=new Double(base);
        //利用java Double对象的equals方法判断两个double类型是否相等 ,注意+0.0与-0.0调用此方法返回false
        if (baseValue.equals(new Double(+0.0))||baseValue.equals(new Double(-0.0)))
        {
            if (exponent<0) 
            {
                throw new Exception("invalid input");               
            }
        }
        int absExponent=0;
        if (exponent<0) 
        {
            absExponent=-exponent;
        }else if (exponent>0)
        {
            absExponent=exponent;
        }else if (exponent==0)
        {
            return 1.0;
        }
        double result=PowerWithUnsignedExponent(base,absExponent);
        if (exponent<0)
        {
            result=1.0/result;
        }
        return result;
    }
    private double PowerWithUnsignedExponent(double base,int absExponent) {
        double result=1.0;
        for (int i = 0; i <absExponent; i++) {
            result*=base;
        }
        return result;
    }
    public static void main(String[] args) throws Exception {
        Test11 t=new Test11();
        System.out.println(t.Power(0.2,-2));
    }
}

面试题12:打印1到最大的n位数

public class Test12 {
    private void Print1ToMaxOfDigits_1(String s,int len){
        if (len==0) {
            System.out.println(s);
            return;
        }
        for (int i = 0; i < 10; i++) {
            Print1ToMaxOfDigits_1(s+i, len-1);
        }
    }
    private void Print1ToMaxOfDigits(int n) {
        Print1ToMaxOfDigits_1("", n);
    }
    public static void main(String[] args) {
        Test12 t=new Test12();
        t.Print1ToMaxOfDigits(4);
    }
}

面试题13:在O(1)时间删除链表结点

public class Test13 {
    private class Node<T>{
        private T value;        
        private Node<T> next = null;

        Node(T obj) {
            this.value = obj;
        }
    }
    public Node DeleteNode(Node head,Node nodeToBeDeleted) throws Exception {
        if (head==null||nodeToBeDeleted==null)
        {
            throw new Exception("invalid");
        }
        //不是尾结点
        if (nodeToBeDeleted.next!=null) 
        {
            Node nextNode=nodeToBeDeleted.next;
            nodeToBeDeleted.value=nextNode.value;
            nodeToBeDeleted.next=nextNode.next;
            nextNode=null;
        }
        //为头结点
        else if (head==nodeToBeDeleted) 
        {
            nodeToBeDeleted=null;
            head=null;
        }
        //多个结点 删除尾结点
        else
        {
            Node pNode=head;
            while (pNode.next!=nodeToBeDeleted) 
            {
                pNode=pNode.next;
            }
            pNode.next=null;
            nodeToBeDeleted=null;
        }
        return head;
    }
}

面试题14:调整数组顺序使奇位位于偶位前面

public class Test14 {
    public void Reorder(int[] a) throws Exception{
        if (a==null&&a.length==0) 
        {
            throw new Exception("Invalid");
        }
        int left=0;
        int right=a.length-1;
        while (left<right) {
            while (left<right&&(a[right]&1)==0) {
                right--;
            }
            while (left<right&&(a[left]&1)==1) {
                left++;
            }
            int tmp=a[left];
            a[left]=a[right];
            a[right]=tmp;
        }       
    }
    public static void main(String[] args) throws Exception {
        int[] a={1,2,3,4,5,6,7};
        Test14 t=new Test14();
        t.Reorder(a);
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }
}

面试题15:链表中倒数第K个结点

public class Test15 {
    private class Node<T>{
        private T obj;      
        private Node<T> next = null;
        Node(T obj) {
            this.obj = obj;
        }
    }
    private Node first = null;
    public Node getHeadNode(){
        return first;
    }
    public <T> void insertFirst(T obj){  
        Node node = new Node(obj);  
        node.next = first;  
        first = node;  
    }
    public Node FindKthToTail(Node head,int k) throws Exception{
        if (head==null||k<1) {
            throw new Exception("invalid");
        }
        Node listHead=head;
        Node node;
        for (int i = 0; i < k-1; i++) 
        {
            if (head.next!=null) 
            {
                head=head.next;
            }else {
                throw new Exception("链表长度小于K");
            }
        }
        node=listHead;
        while (head.next!=null) {
            head=head.next;
            node=node.next;
        }
        return node;
    }
    public static void main(String[] args) throws Exception {
        int[] a={6,5,4,3,2,1};
        Test15 t=new Test15();
        for (int i = 0; i < a.length; i++) 
        {
            t.insertFirst(a[i]);//头插法
        }
        //链表为1->2->3->4->5->6
        Node node=t.FindKthToTail(t.getHeadNode(), 3);
        System.out.println(node.obj);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值