面试总结 算法02

2021-04-17  本文已影响0人  刘景昌
  //乱序
    public static void luan(int[] arr){
          for(int i=0;i<arr.length;i++){
              int random = (int)(Math.random()*(arr.length-i-1));
              int temp = arr[arr.length -i-1];
              arr[arr.length -i-1] = arr[random];
              arr[random] = temp;
          }
        for (int i : arr) {
            System.out.println(i);
        }

    }
    //View Group个数
    public int count1(View view) {
        int count = 0;
        if (view == null) {
            return 0;
        }
        if (view instanceof ViewGroup) {
            count++;
            ViewGroup group = (ViewGroup) view;
            for (int i = 0; i < group.getChildCount(); i++) {
                View child = group.getChildAt(i);
                if (child instanceof ViewGroup) {
                    count += count1(child);
                } else {
                    count++;
                }
            }
        }
        return count;
    }
   //View Group个数
    public int count2(View view) {
        int count = 0;
        if (null == view) {
            return 0;
        }
        if (view instanceof ViewGroup) {
            ViewGroup group = (ViewGroup) view;
            LinkedList<ViewGroup> quene = new LinkedList<>();
            quene.add(group);
            while (!quene.isEmpty()) {
                ViewGroup viewGroup = quene.removeFirst();
                count++;
                for (int i = 0; i < viewGroup.getChildCount(); i++) {
                    View child = viewGroup.getChildAt(i);
                    if (child instanceof ViewGroup) {
                        quene.add((ViewGroup) child);
                    } else {
                        count++;
                    }
                }
            }
        }
        return count;
    }

    //前序遍历
    public void recursivePreOrder(TreeNode p) {
        if (p == null) {
            return;
        }
        System.out.println(p.val);
        recursivePreOrder(p.left);
        recursivePreOrder(p.right);
    }
  // 前序

    public void recursivePreOrder1(TreeNode p) {
        if (p == null) {
            return;
        }
        Stack<TreeNode> treeNodes = new Stack<>();
        treeNodes.push(p);
        while (!treeNodes.empty()) {
            TreeNode node = treeNodes.pop();
            System.out.println(node.val);
            if (p.right != null) {
                treeNodes.push(p.right);
            }
            if (p.left != null) {
                treeNodes.push(p.left);
            }
        }


    }
   //中序
    public void mid(TreeNode p) {
        if (p == null) {
            return;
        }
        Stack<TreeNode> stacks = new Stack<>();
        while (!stacks.empty() || p != null) {
            while (p != null) {
                stacks.push(p);
                p = p.left;
            }
            p = stacks.pop();
            System.out.println(p.val);
            p = p.right;

        }

    }
    //后序
    public void after(TreeNode p) {
        if (p == null) {
            return;
        }
        Stack<TreeNode> stacks = new Stack<>();

        TreeNode pre = null;
        while (!stacks.empty() || p != null) {
            while (p != null) {
                stacks.push(p);
                p = p.left;
            }
            TreeNode right = stacks.peek().right;
            if (right == null || right == pre) {
                //如果又节点为空 或者有节点已经被访问过
                stacks.pop();
                System.out.println(p.val);
                pre = p;
                p = null;
            } else {
                p = p.right;
            }

        }

    }
    //后序

    public void after1(TreeNode p) {
        if (p == null) {
            return;
        }
        Stack<TreeNode> stacks = new Stack<>();

        List<Integer> list = new ArrayList<>();
        stacks.push(p);
        while (!stacks.empty()) {
            TreeNode cur = stacks.pop();
            list.add(0, cur.val);
            if (cur.left != null) {
                stacks.push(cur.left);
            }
            if (cur.right != null) {
                stacks.push(cur.right);
            }
        }
        System.out.println(list);
    }
   //层级排序
    public void levelOrder(TreeNode node) {
        List<List<Integer>> list = new ArrayList<>();
        LinkedList<TreeNode> quene = new LinkedList<>();
        quene.add(node);
        while (!quene.isEmpty()) {
            LinkedList<Integer> temp = new LinkedList<>();
            for (int i = 0; i < quene.size(); i++) {
                TreeNode poll = quene.poll();
                temp.add(poll.val);
                if (node.left != null) {
                    quene.add(node.left);
                }
                if (node.right != null) {
                    quene.add(node.right);
                }
            }
            list.add(temp);
        }
    }


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }
//棧實現隊列
    public class stackQuene {
        Stack<Integer> stack = new Stack<>();
        Stack<Integer> stack1 = new Stack<>();

        public void push(Integer i) {
            stack.add(i);
        }

        public void pop() {
            if (stack.size() != 0) {
                while (!stack.isEmpty()) {
                    int temp = stack.pop();
                    stack1.push(temp);
                }
            }
            stack1.pop();
        }

    }

    public int getN(int n) {
        if (n == 1) {
            return 1;
        }
        if (n == 2) {
            return 2;
        }
        return getN(n - 1) + getN(n - 2);
    }
//上樓梯
    public int getN1(int n) {
        if (n == 1) {
            return 1;
        }
        if (n == 2) {
            return 2;
        }
        int temp1 = 1;
        int temp2 = 2;
        int res = 0;
        for (int i = 3; i <= n; i++) {
            res = temp1 + temp2;
            temp1 = temp2;
            temp2 = res;
        }
        return res;
    }

    //二分法 在array中查找n
    public int sum(int[] array, int n) {
        int start = 0;
        int end = array.length;
        int mid;
        while (start <= end) {
            mid = start + (end - start) / 2;
            if (array[mid] == n) {
                return mid;
            } else if (n > array[mid]) {
                start = mid + 1;
            } else {
                end = mid - 1;
            }
        }
        return -1;
    }

    //链表求和
    public ListNode sumNode(ListNode node1, ListNode node2) {
        ListNode head = null;
        ListNode res = null;
        int carry = 0;
        while (node1 != null | node2 != null) {
            int n1 = node1 == null ? 0 : node1.val;
            int n2 = node2 == null ? 0 : node2.val;
            int sum = n1 + n2 + carry;
            if (head == null) {
                res = head = new ListNode(sum % 10);
            } else {
                head.next = new ListNode(sum % 10);
                head = head.next;
            }
            carry = sum/10;
            if(node1!=null){
                node1 = node1.next;
            }
            if(node2!=null){
                node2 = node2.next;
            }
        }
        if(carry!=0){
            head.next = new ListNode(carry);
        }
        return res;

    }


    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
上一篇下一篇

猜你喜欢

热点阅读