剑指offer_java_1-10

在这里插入图片描述

  1. 设计一个类,我们只能生成该类的一个实例。
/**
 * @author yangfan
 * @date 2020/3/19 14:43
 * @description 实现Singleton模式
 * 题目:设计一个类,我们只能生成该类的一个实例。
 */
public class B {
    private static class BHodler{
        private static final B INSTANCE = new B();
    }

    private static B getInstance(){
        return BHodler.INSTANCE;
    }
}
  1. 二维递增数组中的查找
/**
 * @author yangfan
 * @date 2020/3/19 15:23
 * @description 二维递增数组中的查找
 */
public class C {
    
    /**
     * @param array
     * @param target
     * @return void
     * @description 从左下或右上开始查找
     */
    private static void find(int[][] array, int target) {
        if (array == null || array.length == 0) {
            return;
        }
        int row = 0;
        int column = array[0].length - 1;
        while (row < array.length && column >= 0) {
            if (array[row][column] == target)
                System.out.println(row + " " + column);
            if (array[row][column] > target)
                column--;
            else
                row++;

        }
    }

    public static void main(String[] args) {
        int[][] array = new int[][]{
                {1, 2, 8, 9},
                {2, 4, 9, 12},
                {4, 7, 10, 13},
                {6, 8, 11, 15}
        };
        find(array,10);

    }

    private void show(int[][] array) {
        for (int[] ints : array) {
            for (int anInt : ints) {
                System.out.print(anInt + " ");
            }
            System.out.println();
        }
    }
}
  1. 替换字符串中的空格
/**
 * @author yangfan
 * @date 2020/3/19 15:49
 * @description 替换字符串中的空格
 */
public class D {

    private static void replaceSpace(String str) {
        if (str == null)
            return;

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            if (String.valueOf(str.charAt(i)).equals(" ")) {
                sb.append("%20");
                continue;
            }
            sb.append(str.charAt(i));
        }
        System.out.println(sb);
    }

    public static void main(String[] args) {
        replaceSpace("Hello My World");
    }
}
  1. 从尾到头打印链表
/**
 * @author yangfan
 * @date 2020/3/19 15:59
 * @description 从尾到头打印链表
 */
public class E {
    private static class Node {
        Node(int value) {
            this.value = value;
        }

        int value;
        Node next;
    }

    private static void printListFromTail(Node head) {
        if (head == null) {
            return;
        }

        Stack<Node> stack = new Stack<>();
        Node n = head;
        while (n != null) {
            stack.push(n);
            n = n.next;
        }
        while (!stack.empty()) {
            System.out.println(stack.pop().value);
        }
    }

    private static void printListReverse(Node node) {
        if (node != null) {
            if (node.next != null) {
                printListReverse(node.next);
            }
            System.out.println(node.value);
        }
    }

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

        /*Node n = head;
        while (n != null) {
            System.out.println(n.value);
            n = n.next;
        }*/
    }
}
  1. 重建二叉树
/**
 * @author yangfan
 * @date 2020/3/19 17:45
 * @description 重建二叉树
 */
public class A {
    private static class Node {
        Node(int value) {
            this.value = value;
        }

        int value;
        Node left;
        Node right;
    }

    private static Node constructTree(int[] pre, int[] in) {
        if (pre == null || in == null || pre.length == 0 || in.length == 0)
            return null;
        if (pre.length != in.length)
            return null;

        Node root = new Node(pre[0]);
        for (int i = 0; i < pre.length; i++) {
            if (in[i] == pre[0]) {
                root.left = constructTree(Arrays.copyOfRange(pre, 1, i + 1), Arrays.copyOfRange(in, 0, i));
                root.right = constructTree(Arrays.copyOfRange(pre, i + 1, pre.length), Arrays.copyOfRange(in, i + 1, in.length));
            }
        }
        return root;
    }

    public static void main(String[] args) {
        int[] pre = {1, 2, 4, 7, 3, 5, 6, 8};
        int[] in = {4, 7, 2, 1, 5, 3, 8, 6};
        Node node = constructTree(pre, in);
        show(node);
    }

    private static void show(Node node) {
        if (node == null)
            return;
        show(node.left);
        System.out.print(node.value + " ");
        show(node.right);
    }
}
  1. 用两个栈实现队列
/**
 * @author yangfan
 * @date 2020/3/19 18:17
 * @description 用两个栈实现队列
 */
public class B {

    private static Stack<Integer> stack1 = new Stack<>();
    private static Stack<Integer> stack2 = new Stack<>();

    private static Integer pop() {
        if (stack1 == null && stack2 == null)
            return null;

        if (stack2.empty()) {
            while (!stack1.empty()) {
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }

    private static void push(Integer i) {
        stack1.push(i);
    }

    public static void main(String[] args) {
        push(1);
        push(2);
        push(3);
        push(4);
        System.out.println(pop());
        push(5);
        System.out.println(pop());
        push(6);
        push(7);
        System.out.println(pop());
        System.out.println(pop());
        System.out.println(pop());
        System.out.println(pop());
        System.out.println(pop());
    }
}
  1. 旋转数组的最小数字
/**
 * @author yangfan
 * @date 2020/3/19 18:41
 * @description 旋转数组的最小数字
 */
public class C {

    private static int printMin(int[] array) {
        if (array == null || array.length == 0) {
            return -1;
        }
        if (array[0] < array[array.length - 1]) {
            return array[0];
        }

        int left = 0;
        int right = array.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (array[mid] > array[mid + 1]) {
                return array[mid + 1];
            }
            if (array[mid] < array[mid - 1]) {
                return array[mid];
            }

            if (array[mid] > array[0]) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return -1;
    }

    private static int printMin2(int[] array) {
        if (array == null || array.length == 0) {
            return -1;
        }
        int left = 0;
        int right = array.length - 1;
        while (left < right) {
            int mid = (left + right) / 2;
            if (array[mid] > array[right])
                left = mid + 1;
            else
                right = mid;
        }
        return array[left];
    }

    public static void main(String[] args) {
        int[] array = {1, 0, 1, 1, 1};
        //int[] array = {3, 4, 5, 1, 2};
        System.out.println(printMin(array));
        System.out.println(printMin2(array));
    }
}
  1. 斐波那契数列
/**
 * @author yangfan
 * @date 2020/3/19 19:13
 * @description 斐波那契数列
 */
public class D {

    private static int fibonacci(int n) {
        if (n <= 1)
            return n <= 0 ? -1 : 1;

        int result = 0;
        int preone = 1;
        int pretwo = 0;

        for (int i = 2; i <= n; i++) {
            result = preone + pretwo;
            pretwo = preone;
            preone = result;
        }
        return result;
    }

    /**
     * @param n
     * @return int
     * @description 青蛙跳台阶
     */
    private static int gua(int n) {
        if (n <= 0)
            return -1;
        if (n < 2)
            return n;

        int result = 0;
        int preone = 2;
        int pretwo = 1;

        for (int i = 3; i <= n; i++) {
            result = preone + pretwo;
            pretwo = preone;
            preone = result;
        }
        return result;
    }

    /**
     * @param n
     * @return int
     * @description 2*1覆盖2*n方式
     */
    private static int cover(int n) {
        if (n <= 1)
            return n <= 0 ? -1 : 1;

        int number = 1;
        int sum = 1;
        while (n-- >= 2) {
            sum += number;
            number = sum - number;
        }
        return sum;
    }

    public static void main(String[] args) {
        System.out.println(fibonacci(3));
        System.out.println(gua(3));
        System.out.println(cover(3));
    }
}
  1. 二进制中1的个数
/**
 * @author yangfan
 * @date 2020/3/19 19:31
 * @description 二进制中1的个数
 */
public class E {

    private static int numberOf1(int num) {
        int count = 0;
        while (num != 0) {
            count++;
            num = num & (num - 1);
        }
        return count;
    }

    public static void main(String[] args) {
        System.out.println(numberOf1(11));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值