算法笔试题

public class ArithmeticTest {

    public static void main(String[] args) throws Exception {

        // int[][] array = {{1, 2, 3}, {2, 3, 4}};
        // int target = 8;
        // System.out.println(find(array, target));

        // StringBuffer sb = new StringBuffer("we are happy");
        // String space = replaceSpace(stringBuffer);
        // System.out.println(space);

        // ListNode l1 = new ListNode(1);
        // ListNode l2 = new ListNode(2);
        // ListNode l3 = new ListNode(3);
        // l1.next = l2;
        // l2.next = l3;
        // List<Integer> list = printListFromTailToHead(l1);
        // System.out.println(list);

        // int[] pre = { 1, 2, 4, 7, 3, 5, 6, 8 };
        // int[] in = { 4, 7, 2, 1, 5, 3, 8, 6 };
        // TreeNode treeNode = reConstructBinaryTree(pre, in);
        // System.out.println(treeNode);

        // StackQueue stackQueue = new StackQueue();
        // stackQueue.push(2);
        // stackQueue.push(3);
        // stackQueue.push(5);
        // stackQueue.stack1.forEach(System.out::println);
        // System.out.println("pop--------------");
        // System.out.println(stackQueue.pop());
        // System.out.println(stackQueue.pop());
        // stackQueue.stack2.forEach(System.out::println);

        // System.out.println(fibonacci(3));
        System.out.println(numOf1(2));

    }

    /*
     * 输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示
     * 0001 0010 0011 0100
     * a&(a-1)的结果会将a最右边的1变为0,直到a = 0,还可以先将a&1 != 0,然后右移1位,但不能计算负数的值
     */
    public static int numOf1(int n) {
        int count = 0;
        while (n != 0) {
            count++;
            n = n & (n - 1);
        }
        return count;
    }

    /**
     * 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完
     * 成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数
     * 思路:从右上角或左下角开始找,逐行删除,或者用二分法查找
     */
    public static boolean find(int[][] array, int target) {
        int row = 0;
        int column = array[0].length - 1;
        // 二分查找
        while (row < array.length && column >= 0) {
            if (array[row][column] == target) {
                return true;
            } else if (array[row][column] > target) {
                column--;
            } else {
                row++;
            }
        }
        return false;
    }

    /**
     * .将一个字符串中的空格替换成“%20”。
     * 例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
     * 思路:从后往前复制,数组长度会增加,或使用StringBuilder、StringBuffer类
     */
    public static String replaceSpace(StringBuffer str) {
        if (str == null) {
            return null;
        }
        int length = str.length();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            if (String.valueOf(str.charAt(i)).equals(" ")) {
                sb.append("%20");
            } else {
                sb.append(str.charAt(i));
            }
        }
        return sb.toString();
    }

    /**
     * .输入一个链表,从尾到头打印链表每个节点的值。
     * 思路:借助栈实现,或使用递归的方法。
     */
    public static List<Integer> printListFromTailToHead(ListNode listNode) {
        if (listNode == null) {
            return null;
        }
        List<Integer> valueList = new ArrayList<>();
        Stack<ListNode> stack = new Stack<>();
        while (listNode != null) {
            stack.push(listNode);
            listNode = listNode.next;
        }
        // 将栈中弹出的值加入到集合中,即链表从后往前的值
        while (!stack.isEmpty()) {
            valueList.add(stack.pop().value);
        }
        return valueList;
    }

    /**
     * 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中
     * 都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并
     * 返回。
     * 思路:先找出根节点,然后利用递归方法构造二叉树
     * 由前序遍历知此时根节点为的值为1,中序遍历特性在值为1左边的为左子树,右边的为右子树
     */
    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int value) {
            this.val = value;
        }

        public void preOrder() {
            System.out.println(this);
            if (this.left != null) {
                this.left.preOrder();
            }
            if (this.right != null) {
                this.right.preOrder();
            }
        }

        @Override
        public String toString() {
            return "TreeNode{" +
                    "val=" + val +
                    ", left=" + left +
                    ", right=" + right +
                    '}';
        }
    }

    public static TreeNode reConstructBinaryTree(int[] pre, int[] in) {
        if (pre == null || in == null) {
            return null;
        }
        if (pre.length == 0 || in.length == 0) {
            return null;
        }
        if (pre.length != in.length) {
            return null;
        }

        TreeNode root = new TreeNode(pre[0]);// root节点

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

    /**
     * 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型
     */
    static class StackQueue {

        Stack<Integer> stack1;
        Stack<Integer> stack2;

        public StackQueue() {
            stack1 = new Stack<>();
            stack2 = new Stack<>();
        }

        public void push(int node) {
            stack1.push(node);
        }

        public int pop() throws Exception {
            if (stack1.isEmpty() && stack2.isEmpty()) {// 栈2缓存栈1的数据,当栈1和栈2都为空时才为空
                throw new Exception("栈为空");
            }
            if (stack2.isEmpty()) {// 当栈2为空时,才再次往栈2里添加栈1的数据,否则顺序有误
                while (!stack1.isEmpty()) {
                    stack2.push(stack1.pop());
                }
            }
            return stack2.pop();
        }
    }

    /**
     * 现在要求输入一个整数n,请你输出斐波那契数列的第n项。n<=39
     */
    public static int fibonacci(int n) {
        // if (n == 1) return 1;
        // if (n == 2) return 2;
        // return fibonacci(n - 1) + fibonacci(n - 2);
        int result = 0;
        int preOne = 1;
        int preTwo = 0;
        if (n == 0) {
            return preTwo;
        }
        if (n == 1) {
            return preOne;
        }
        for (int i = 2; i <= n; i++) {
            result = preTwo + preOne;
            preTwo = preOne;
            preOne = result;
        }
        return result;
    }

}

class ListNode {
    int value;
    ListNode next;

    public ListNode(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public ListNode getNext() {
        return next;
    }

    public void setNext(ListNode next) {
        this.next = next;
    }

    @Override
    public String toString() {
        return "ListNode{" +
                "value=" + value +
                '}';
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值