High-intensity training to learn the ninth day summary: 5 to prove safety offer title

Really I do not want to see the JVM. Brush a few questions to prove safety of the Offer, a water bar water today, his mind confused.

1. Find a two-dimensional array

(The length of each one-dimensional array of same), each row from left to right in order of ascending sort, to sort each column in a two-dimensional array in order of increasing from top to bottom. A complete function, enter such a two-dimensional array and an integer, it is determined whether the array contains the integer.

Problem-solving ideas: the upper right corner to start the search from top to bottom increments, from right to left is diminishing. To search, the complexity of the algorithm is based on this idea of ​​n + m

public class Solution {
    public boolean Find(int target, int [][] array) {
        int x=0,y=array[0].length-1;
        while(x<array.length&&y>-1){
            if(array[x][y]>target){
                y--;
            }else if(array[x][y]<target){
                x++;
            }else{
                return true;
            }
        }
        return false;
    }
}

2. Replace spaces

Please implement a function, a string to replace each space to "20%." For example, when the string is We Are Happy. After the string is replaced after We% 20Are% 20Happy.

Problem-solving ideas: You can not insert one by one, complexity is too high, to use space for time. A an assignment better.

public class Solution {
    public String replaceSpace(StringBuffer str) {
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<str.length();i++) {
            if(str.charAt(i)==' ') {
                sb.append("%20");
            }else {
                sb.append(str.charAt(i));
            }
        }
        return sb.toString();
    }
}

3. The print head from the end of the list

Enter a list, by returning a list sequentially from the tail to the head of ArrayList.

Problem-solving ideas: This problem can directly reverse pointers in C ++, then, but Java is passed in a collection class. Even if the bar. A fool solution.

/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> al = new ArrayList<Integer>();
        while(listNode!=null) {
            al.add(0,listNode.val);
            listNode = listNode.next;
        }
        return al;
    }
}

4. reconstruction of binary tree

And enter the result in a preorder traversal of a binary tree in preorder traversal of the binary tree a rebuild. Suppose Results preorder traversal order and input of duplicate numbers are free. Before entering e.g. preorder traversal sequence {1,2,4,7,3,5,6,8} and {4,7,2,1,5,3,8,6} order traversal sequence, and the reconstructed binary tree return.

Problem-solving ideas: Recursive achievements. Basic questions.

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
        if(pre.length==0) return null;
        TreeNode trn = new TreeNode(pre[0]);
        if(pre.length==1) return trn;
        else for(int i=0;i<in.length;i++) {
            if(in[i]==pre[0]) {
                int [] preleft = new int[i];
                int [] preright = new int[in.length-i-1];
                int [] inleft = new int[i];
                int [] inright = new int[in.length-i-1];
                System.arraycopy(pre,1,preleft,0,i);
                System.arraycopy(pre,i+1,preright,0,in.length-i-1);
                System.arraycopy(in,0, inleft, 0, i);
                System.arraycopy(in,i+1, inright, 0, in.length-i-1);
                trn.left = reConstructBinaryTree(preleft, inleft);
                trn.right = reConstructBinaryTree(preright,inright);
                return trn;
            }
        }
        return trn;
    }
}

The two stacks queue

Two stacks to achieve a queue, the completion queue Push and Pop operations. Queue elements int.

Problem-solving ideas: on the basis of last-out and FIFO understood to want to understand.

import java.util.Stack;

public class Solution {
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();
    
     public void push(int node) {
        stack1.push(node);
    }
    
    public int pop() {
        if(!stack2.empty()) {
            return stack2.pop();
        }
        else{
            while(!stack1.empty()) {
                stack2.push(stack1.pop());
            }
            return stack2.pop();
        }
    }
}

Guess you like

Origin www.cnblogs.com/godoforange/p/11575258.html