剑指offer_java_11-20

在这里插入图片描述

  1. 数值的整数次方
/**
 * @author yangfan
 * @date 2020/3/19 20:30
 * @description 数值的整数次方
 */
public class A {

    private static double Power(double base, int ex) {
        double result;
        if (String.valueOf(base).equals("0.0")) {
            return 0;
        }
        if (ex == 0) {
            return 1.0;
        }

        if (ex > 0) {
            result = mutiply(base, ex);
        } else {
            result = mutiply(1 / base, -ex);
        }
        return result;
    }

    private static double mutiply(double base, int ex) {
        double sum = 1;
        for (int i = 0; i < ex; i++) {
            sum *= base;
        }
        return sum;
    }

    public static void main(String[] args) {
        System.out.println(Power(0, -3));
    }
}
  1. 打印1到最大的n位数
/**
 * @author yangfan
 * @date 2020/3/19 21:35
 * @description 打印1到最大的n位数
 */
public class B {

    private static void printMax(int n) {
        int[] array = new int[n];
        if (n <= 0) {
            return;
        }
        printArray(array, 0);
    }

    private static void printArray(int[] array, int n) {
        for (int i = 0; i < 10; i++) {
            if (n != array.length) {
                array[n] = i;
                printArray(array, n + 1);
            } else {
                boolean isFirst0 = false;
                for (int value : array) {
                    if (value != 0) {
                        System.out.print(value);
                        if (!isFirst0)
                            isFirst0 = true;
                    } else {
                        if (isFirst0)
                            System.out.print(value);
                    }
                }
                System.out.println();
                return;
            }
        }

    }

    public static void main(String[] args) {
        printMax(2);
    }
}
  1. 在O(1)时间删除链表结点
/**
 * @author yangfan
 * @date 2020/3/19 22:27
 * @description 在O(1)时间删除链表结点
 */
public class C {

    private static class Node {
        Node(int value) {
            this.value = value;
        }

        int value;
        Node next;
    }

    private static void deleteNode(Node head, Node del) {
        if (head == null || del == null)
            return;

        if (head == del) {
            head.value = -1;
        } else if (del.next != null) {
            del.value = del.next.value;
            del.next = del.next.next;
        } else {
            Node n = head;
            while (n.next.next != null) {
                n = n.next;
            }
            n.next = null;
        }
    }

    public static void main(String[] args) {
        Node head = new Node(888);
        Node tail = head;
        for (int i = 0; i < 5; i++) {
            Node node = new Node(i);
            tail.next = node;
            tail = node;
        }
        Node del = head.next.next.next.next.next;

        deleteNode(head, del);

        Node n = head;
        while (n != null) {
            System.out.println(n.value);
            n = n.next;
        }
    }
}
  1. 调整数组顺序使奇数位于偶数前面
/**
 * @author yangfan
 * @date 2020/3/19 22:48
 * @description 调整数组顺序使奇数位于偶数前面
 */
public class D {

    private static void reOrderArray(int[] array) {
        if (array == null || array.length == 0)
            return;

        int left = 0;
        int right = array.length - 1;
        int flag;
        while (left < right) {
            while (left<right && array[left] % 2 != 0)
                left ++;
            while (left<right && array[right] % 2 == 0)
                right --;

            if (left<right){
                flag = array[right];
                array[right] = array[left];
                array[left] = flag;
            }
        }

        System.out.println(Arrays.toString(array));
    }

    public static void main(String[] args) {
        int[] array = new int[]{1,2,3,4,5,6,7};
        reOrderArray(array);
    }
}
  1. 链表中倒数第k个结点
/**
 * @author yangfan
 * @date 2020/3/19 23:03
 * @description 链表中倒数第k个结点
 */
public class E {

    private static class Node {
        Node(int value) {
            this.value = value;
        }

        int value;
        Node next;
    }

    private static Node findKfromTail(Node head, int k) {
        if (head == null || k == 0)
            return null;

        Node before = head;
        Node after = head;

        while (k-- > 1) {
            if (before.next == null)
                return null;
            before = before.next;
        }

        while (before.next != null) {
            before = before.next;
            after = after.next;
        }

        return after;
    }


    public static void main(String[] args) {
        Node head = new Node(777);
        Node tail = head;
        for (int i = 0; i < 5; i++) {
            Node node = new Node(i);
            tail.next = node;
            tail = node;
        }

        System.out.println(findKfromTail(head, 2));

    }
}
  1. 反转链表
/**
 * @author yangfan
 * @date 2020/3/19 23:14
 * @description 反转链表
 */
public class A {

    private static class Node {
        Node(int value) {
            this.value = value;
        }

        int value;
        Node next;
    }

    private static Node reverse(Node head) {
        if (head == null)
            return null;

        Node pre = null;
        Node node = head;
        Node next;

        while (node != null) {
            next = node.next;
            node.next = pre;
            pre = node;
            node = next;
        }
        return pre;
    }

    public static void main(String[] args) {
        Node head = new Node(666);
        Node tail = head;
        for (int i = 0; i < 5; i++) {
            Node node = new Node(i);
            tail.next = node;
            tail = node;
        }
        
        Node n = reverse(head);
        while (n != null) {
            System.out.println(n.value);
            n = n.next;
        }
    }
}
  1. 合并两个排序的链表
/**
 * @author yangfan
 * @date 2020/3/20 12:03
 * @description 合并两个排序的链表
 */
public class B {

    /**
     * @param node1
     * @param node2
     * @return jzoffer.utils.Node
     * @description 递归
     */
    private static Node mergeTwoLists1(Node node1, Node node2) {
        if (node1 == null)
            return node2;
        if (node2 == null)
            return node1;

        if (node1.value < node2.value) {
            node1.next = mergeTwoLists1(node1.next, node2);
            return node1;
        } else {
            node2.next = mergeTwoLists1(node1, node2.next);
            return node2;
        }
    }

    /**
     * @param node1
     * @param node2
     * @return jzoffer.utils.Node
     * @description 迭代
     */
    private static Node mergeTwoLists2(Node node1, Node node2) {
        Node newHead = new Node();
        Node pre = newHead;

        while (node1 != null && node2 != null) {
            if (node1.value < node2.value) {
                pre.next = node1;
                node1 = node1.next;
            } else {
                pre.next = node2;
                node2 = node2.next;
            }
            pre = pre.next;
        }
        pre.next = node1 == null ? node2 : node1;
        return newHead.next;
    }

    public static void main(String[] args) {
        Node.show(mergeTwoLists1(Node.getNode1(10), Node.getNode2(10)));
        Node.show(mergeTwoLists2(Node.getNode1(10), Node.getNode2(10)));
    }
}
  1. 树的子结构
/**
 * @author yangfan
 * @date 2020/3/20 14:00
 * @description 树的子结构
 */
public class C {

    private static boolean hasSubTree(Tree source, Tree target) {
        if (target == null)
            return true;
        if (source == null)
            return false;
        if (doesRoot1HasRoot2(source, target))
            return true;
        return hasSubTree(source.left, target) || hasSubTree(source.right, target);

    }

    private static boolean doesRoot1HasRoot2(Tree source, Tree target) {
        if (source == null && target == null)
            return true;
        if (source == null || target == null)
            return false;
        if (source.value != target.value)
            return false;
        return doesRoot1HasRoot2(source.left, target.left) && doesRoot1HasRoot2(source.right, source.right);
    }


    public static void main(String[] args) {
        Tree source = Tree.creat(new Tree());
        System.out.println("------------");
        Tree target = Tree.creat(new Tree());
        System.out.println("------------");
        if (hasSubTree(source,target))
            System.out.println("yes");
        else
            System.out.println("no");
    }
}
  1. 二叉树的镜像
/**
 * @author yangfan
 * @date 2020/3/20 14:33
 * @description 二叉树的镜像
 */
public class D {

    private static void MirrorTree1(Tree root) {
        if (root == null || (root.left == null && root.right == null))
            return;
        Tree temp = root.left;
        root.left = root.right;
        root.right = temp;

        if (root.left != null)
            MirrorTree1(root.left);
        if ((root.right != null))
            MirrorTree1(root.right);
    }

    public static void main(String[] args) {
        Tree source = Tree.creat(new Tree());
        MirrorTree1(source);
        Tree.show(source);
    }
}
  1. 顺时针打印矩阵
/**
 * @author yangfan
 * @date 2020/3/20 14:51
 * @description 顺时针打印矩阵
 */
public class E {

    private static List spiralOrder(int[][] matrix) {
        List<Integer> result = new ArrayList<>();
        if (matrix == null || matrix[0].length == 0)
            return null;

        int r1 = 0, r2 = matrix[0].length - 1;
        int c1 = 0, c2 = matrix.length - 1;
        while (r1 <= r2 && c1 <= c2) {
            for (int i = c1; i <= c2; i++) {
                result.add(matrix[r1][i]);
            }
            for (int j = r1+1; j <= r2; j++) {
                result.add(matrix[j][c2]);
            }
            if (r1 < r2 && c1 < c2) {
                for (int i = c2-1; i > c1; i--) {
                    result.add(matrix[r2][i]);
                }
                for (int j = r2; j > r1; j--) {
                    result.add(matrix[j][c1]);
                }
            }
            c1++;
            r1++;
            c2--;
            r2--;
        }
        return result;
    }

    public static void main(String[] args) {
        int[][] array = {
                {1,2,3,4},
                {5,6,7,8},
                {9,10,11,12},
                {13,14,15,16}
        };
        System.out.println(spiralOrder(array).toString());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值