The sword refers to the offer surface question note 11~20 question (Java implementation)

Source: Internet
Author: User

First, interview question 1: copy operator function (P24)

Title: The following is a declaration of type cmstring, please add an assignment operator function for that type.

Class cmystring { public: cmystring (char* pData = NULL);  CMyString (const cmystring& str);  ~cmystring (void);  private: char* m_pdata; }

Problem Solving Ideas:

Second, interview question 2: Realize singleton mode (P31)

Title: Designing a class, we can only generate an instance of the class.

Problem Solving Ideas:

Iii. Interview Question 3: Finding in a two-dimensional array (P38)

Title: In a two-dimensional array, each row is ordered in ascending order from left to right, and each column is sorted in ascending order from top to bottom. Complete a function, enter a two-dimensional array and an integer to determine if the array contains the integer.

Thinking: According to the characteristics of the two-dimensional array, starting from the first number in the last column, if the number is greater than that integer, the number in the column is greater than the integer, so you can exclude the column. Always exclude to a column the first number is less than the integer. The last digit of the first row of the remaining array is compared to an integer, if the number is less than an integer, the number in the row is less than the integer, the row can be excluded, and so on, until the integer is found.

PublicStaticBoolean Find (Int[][] Matrix,int rows,int columns,IntNumber) {Boolean found =False;if (Matrix! =Null && rows >0 && columns >0) {int row = 0; int column = Columns-1; While (Row < rows && column >= 0) { if (matrix[row][column] = = number ) {found = True
                          
                           ; 
                           break;} Else if (Matrix[row][column] > number ) column-- ; Else row + +;}} return found;}           
                               

Iv. interview question 4: Replace space (P44)

Title: Implement a function that replaces each space in a string with "%20". For example, enter "We are happy.", then output "we%20are%20happy."

Problem-Solving ideas: If you look for and replace from the go, each replacement of a space, you need to move the following characters backwards, the last "happy." Will move two times. Therefore, the total time efficiency is O (N2) for strings that contain O (n) space characters. Will replace from the back to the back to replace, the time complexity of O (n).

Problem-Solving ideas : first traversal, statistics out the string of the number of empty lattice n, the length of the replacement string is the previous length plus 2n. Prepare two pointers, P1 and P2. 1 points to the end of the original string, and 2 points to the end of the replaced string. Move the pointer 1 forward, copying the characters to 2 points, one by one, until you hit the first space. Move 1 forward one cell, and insert the string "%20" before 2. Continue looping the operation.

V. Interview question 5: Print the list from the end (P51)

Title: Enter the head node of a linked list, which in turn prints the value of each node from the end of the head.

The linked list node is defined as follows:

struct listnode { int m_nkey;  listnode* M_pnext; }

The idea of solving a problem: Advanced, you can use the stack to achieve this order, each time through a node, it will be placed in the stack, after traversing the entire list, starting from the top of the stack output node value.

Static stack<listnode> reverselist_2 (ListNode node) {        new Stack ();        null) {Stack.push (node); node =return stack;}      

Two: Dynamic programming to solve the problem of large problems gradually, when the beginning to return the results, just realize the inverted function.

Static ListNode reverselist_1 (ListNode node) {        nullreturn node; ListNode ListNode = reverselist_1 (node.next); node.next.next =nullreturn ListNode;}  

Vi. Interview Question 6: Rebuilding a binary tree (P55)

Title: Enter a binary tree's pre-sequence traversal and the results of the middle sequence traversal, rebuild the two-fork tree. Assume that no duplicate numbers are included in the result of the input's pre-order traversal and the middle-order traversal.

The binary tree nodes are defined as follows:

struct binarytreenode { int m_nvalue;    binarytreenode* M_pleft;  binarytreenode* m_pright; }

problem-solving ideas: Based on the pre-order traversal to find the root node, in the middle sequence traversal, the node before the root node is the left subtree of the root node, the node after the root node is the right subtree of the root node. It is done by recursive method.

PublicStatic Binarytreenode construct (Int[] Preorder,IntBEGINP,IntENDP,Int[] inorder,IntBegini,IntEndi) {if (Beginp > ENDP | | begini >Endi)ReturnNull;int rootdata =PREORDER[BEGINP]; Binarytreenode head =NewBinarytreenode (); Head.value =Rootdata;int divider =Findindexinarray (Inorder, Rootdata, Begini, Endi);int offSet = Divider-begini-1; Binarytreenode left = construct (preorder, BEGINP + 1, BEGINP + 1 + offSet, inorder, Begini, Begini +OffSet); Binarytreenode right = construct (preorder, BEGINP + OffSet + 2, ENDP, inorder, divider + 1, Endi); Head.left =Left Head.right =RightReturnHead }PublicStaticint Findindexinarray (Int[] A,int x,int begin,IntEnd) {for (int i = BEGIN; I <= end; i + +)if (a[i] = =XReturnIReturn-1; }PublicStaticvoidPreorder (Binarytreenode N) {if (n!=Null) {System.out.print (n.value+ ","); Preorder (N.left); Preorder (n.right); }} public static void inorder (binarytreenode N) { if (n!=null) {inorder (n.left); System.out.print (n.value+ ","); inorder (n.right);}} public static void Afterorder (binarytreenode N) { if (n!=null) {afterorder (n.left); Afterorder (N.right); System.out.print (n.value+ ",");}} 

Vii. Interview Question 7: Implementing a queue with two stacks (P59)

Title: Implement a queue with two stacks. The declaration of the queue is as follows: implement its two functions Appendtail and Deletehead, respectively, to complete the function of inserting nodes at the end of the queue and deleting nodes at the head of the queue.

Class Cqueue { public: Cqueue (void);     ~cqueue (void);    void Appendtail (const t& node);   T Deletehead ();    private: stack<t> stack1;  Stack<t> Stack2; }

Viii. Interview Question 8: Minimum number of rotated arrays (P66)

Title: Move a number of elements at the beginning of an array to the end of the array, which we call the rotation of the array. Enter a rotation of an incrementally sorted array, outputting the smallest element of the rotated array. For example, the array {3,4,5,1,2} is a rotation of {1,2,3,4,5}, and the minimum value of the array is 1.

Problem Solving Ideas:

Ix. Interview Question 9: Fibonacci sequence (P73)

Title One: Write a function, enter N, and find the nth term of the Fibonacci sequence. The Fibonacci sequence is defined as follows:

  

Problem Solving Ideas:

Title Two: A frog can jump up to 1 steps at a time, you can also jump on the level 2. Ask the frog to jump on an n-level step with a total number of hops.

Problem Solving Ideas:

X. Interview question 10: Number of 1 in binary (P78)

  Title: Please implement a function, enter an integer, output the number of binary representation of 1. For example, 9 is represented as a binary is 1001, and 2 bits are 1. So if you enter 9, the function outputs 2.

Problem Solving Ideas:

Sword refers to the offer surface question note 11~20 (Java implementation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.