I really don't want to see JVM anymore. Brush a few finger-to-finger questions on Offer. Let's have water today. My brain is confused.
1. Finding in two-dimensional arrays
In a two-dimensional array (each one-dimensional array has the same length), each row is sorted in the order of increasing from left to right, and each column is sorted in the order of increasing from top to bottom. Please complete a function, input such a two-dimensional array and an integer, to determine whether the array contains the integer.
Solution: Search from the top right corner, increase from top to bottom, decrease from right to left. According to this idea to search, the algorithm complexity 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. Replacement of blanks
Implement a function that replaces each space in a string with "% 20". For example, when the string is We Are Happy., the replaced string is We%20Are%20Happy.
Solution: You can't insert one by one, the complexity is too high, you need to use space for time. It's better to assign values one by one.
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. Print the linked list from end to end
Enter a linked list and return an ArrayList in the end-to-end order of the linked list.
Solution: This problem can be directly inverted with C++, but Java comes in a collection class. Forget it. Use a fool's 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. Reconstructing Binary Trees
Enter the results of the pre-order and middle-order traversals of a binary tree, and rebuild the binary tree. It is assumed that the results of the input preamble traversal and the intermediate order traversal do not contain duplicate numbers. For example, the binary tree is reconstructed and returned if the pre-order traversal sequence {1,2,4,7,3,5,6,8} and the middle-order traversal sequence {4,7,2,1,5,3,8,6} are input.
Solving the problem: building a tree recursively. 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; } }
5. Queuing with two stacks
Two stacks are used to implement a queue to complete the Push and Pop operations of the queue. The elements in the queue are of type int.
Solution: The basic understanding of FIFO and FIFO can be understood.
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(); } } }