Common Algorithm Questions (5) - Sword Pointing to Offer

The sword refers to the part of the written test questions of the offer

Input an array of integers, implement a function to adjust the order of the numbers in the array, so that all odd numbers are in the first half of the array, all even numbers are in the second half of the array, and ensure that odd and odd, even and even numbers are between The relative position remains unchanged.

If the order is not required, then you can use the double pointers starting from both sides, clip to the middle, and then exchange the two numbers; this question requires the original order, then the idea is to find the first odd number, take it out, and move all the previous numbers back by one. , of course, pay attention to the boundary, use an indexl=-1 to store the index of the last odd number, if the first number is an odd number, the index ++=0, the newly inserted odd number starts from ++index

  import java.util.Scanner;

     public class Solution {

     public static void main(String [] args) {
            Scanner sc = new Scanner(System.in);
            Solution ad = new Solution();
            int n = sc.nextInt();
            int[] array = new int[n];

            for(int i=0;i<n;i++) {
                array[i] = sc.nextInt();
            }
            sc.close();
            ad.reOrderArray(array);
            for(int i=0;i<n;i++) {
                System.out.println(array[i]);
            }

      }
     public void reOrderArray(int [] array) {
      int index = -1;
         for(int i=0;i<array.length;i++) {
            if(array[i]%2!=0) {
                if(i>0) {
                    int temp = array[i];
                    while(i-1>index) {
                        array[i] = array[i-1];
                        i--;
                    }
                    array[++index] = temp;
                }else {
                    index++;
                }   
            }
        }
     }

    }

reverse linked list

Reverse the linked list, and then output sequentially. The key to the inversion is that the next node of the first node is saved as temp, and next points to the new header at the beginning of head=null, then head=p, p=temp

Input two monotonically increasing linked lists, and output the linked list after combining the two linked lists. Of course, we need the combined linked list to satisfy the monotonically non-decreasing rule.

You can compare the headers of the two tables separately, and insert the small ones into the new linked list.

Input two binary trees A and B, and determine whether B is a substructure of A. (ps: we agree that an empty tree is not a substructure of any tree)

First judge whether B is the top of the tree of A, whether it is the top of the left subtree of A, or whether it is the top of the right subtree, in fact, it is recursion. tree equal && right subtree equal

public void createTree(TreeNode parent,int index,int flag) {
        if(flag==1) {
            if(index>array.length/2-1) {
                return;
            }
            if(array[index*2+1].equals("#")) {
                parent.left=null;
            }else {
                int num1 = Integer.parseInt(array[index*2+1]);
                TreeNode left = new TreeNode(num1);
                parent.left = left;
                createTree(left,index*2+1,1);
            } 

            if(array[index*2+2].equals("#")) {
                parent.right=null;
            }else {
                int num2 = Integer.parseInt(array[index*2+2]);
                TreeNode right = new TreeNode(num2);
                parent.right = right;
                createTree(right,index*2+2,1);
            } 
        }else {
            if(index>child.length/2-1) {
                return;
            }
            if(child[index*2+1].equals("#")) {
                parent.left=null;
            }else {
                int num1 = Integer.parseInt(child[index*2+1]);
                TreeNode left = new TreeNode(num1);
                parent.left = left;
                createTree(left,index*2+1,1);
            } 

            if(child[index*2+2].equals("#")) {
                parent.right=null;
            }else {
                int num2 = Integer.parseInt(child[index*2+2]);
                TreeNode right = new TreeNode(num2);
                parent.right = right;
                createTree(right,index*2+2,2);
            } 
        }   

    }

    public boolean HasSubtree(TreeNode root1,TreeNode root2) {
        if(root1==null||root2==null) return false;

        boolean flag = false;
        flag = isSame(root1,root2);
        if(!flag) {
            flag = HasSubtree(root1.left,root2);
        }
        if(!flag) {
            flag = HasSubtree(root1.right,root2);
        }
        return flag;
    }

    //判断r2是不是r1的树顶
    public boolean isSame(TreeNode r1,TreeNode r2) {
        if(r1==null&&r2!=null) return false;
        if(r2==null) return true;
        if(r1.val==r2.val) {
            return isSame(r1.left,r2.left)&&isSame(r1.right,r2.right);
        }else {
            return false;
        }
    }

    }

    class TreeNode{
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

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

Enter a matrix and print each number in clockwise order from the outside to the inside. For example, if you enter the following matrix: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Then print the number 1 in turn, 2,3,4,8,12,16,15,14,13,9,5,6,7,11,10

Traverse a circle clockwise each time, divided into → ↓ ← ↑, the loop condition is row>2*y&&colum>2*x, the starting point of each time is 00 11 22 33.. Pay attention to the boundary

 public ArrayList<Integer> printMatrix(int [][] matrix) {
        ArrayList<Integer> list = new ArrayList<>();
        int i=0;
        int j=0;
        int row = matrix.length;
        int colum = matrix[0].length;
        //遍历一圈
        while(row>2*j&&colum>2*i) {
            for(int k=i;k<colum-i;k++) {
                list.add(matrix[i][k]);
            }   
            if(i+1<row-i) {
                for(int k=i+1;k<row-i;k++) {
                    list.add(matrix[k][colum-i-1]);
                }
            }
            if(colum-i-2>=j&&row-i-1>i) {
                for(int k=colum-i-2;k>=j;k--) {
                    list.add(matrix[row-1-i][k]);
                }
            }
            if(row-i-2>=i+1&&j<colum-i-1) {
                for(int k=row-i-2;k>=i+1;k--) {
                    list.add(matrix[k][j]);
                }
            }
            ++i;
            ++j;
        }
        return list;
    }

Input an integer array to determine whether the array is the result of a post-order traversal of a binary search tree. If yes, output Yes, otherwise output No. Assume that any two numbers in the input array are different from each other

Find the first one greater than the root node, then all left subtrees are left subtrees, and all right subtrees are right subtrees, compare the root node of the left subtree and the root node of the right subtree, pay attention to the boundary (post-order binary search tree)

public boolean VerifySquenceOfBST(int [] sequence) {
         if(sequence.length==1) return true;
            int index = -1;
            for(int i=0;i<sequence.length-1;i++) {
                if(sequence[i]>sequence[sequence.length-1]) {
                    index = i;
                    break;
                }
            }
            int root = sequence[sequence.length-1];
            //只有左子树
            if(index==-1) {
                int l = sequence[sequence.length-2];
                if(l<root) {
                    int[] s = Arrays.copyOfRange(sequence, 0, sequence.length-1);
                    return VerifySquenceOfBST(s);
                }else {
                    return false;
                }
            }
            //只有右子树
            if(index==0) {
                int r = sequence[sequence.length-2];
                if(r>root) {
                    int[]s = Arrays.copyOfRange(sequence, index, sequence.length-1);
                    return VerifySquenceOfBST(s);
                }else {
                    return false;
                }
            }else{
                int l = sequence[index-1];
                int r = sequence[sequence.length-2];
                if(l<root&&r>root) {
                    int[]s1 = Arrays.copyOfRange(sequence, 0, index);
                    int[]s2 = Arrays.copyOfRange(sequence, index, sequence.length-1);
                    return VerifySquenceOfBST(s1)&&VerifySquenceOfBST(s2);
                }else {
                    return false;
                }
            }

    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325727497&siteId=291194637