Offer explanations to prove safety record

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.

  solution:

     Left for the array, you can start comparing from lower left or top right corner, behavior i, as j, when x is less than a [i] [j], you should look up that i--, otherwise that is right j ++ .

 

 

 

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.

  solution:

  First of all to count the number of spaces, re-allocate space for the string. Traversing forwardly from the rear, from the string copied and replaced.

 

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.

  And then stored in the output stack.

 

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.

  solution:

  As examples: the first is a preorder traversal the root, root establishment, to find the root node in preorder, after finding the left is the left subtree, the right is the right subtree, then the preamble sequence in divided into left subtree and right subtree, and it does the same operation until its complete division.

 

The two stacks queue

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

  solution:

  a first push operation directly to the stack push, pop operation will have to check whether the second of the stack is empty, if not empty the first element pushed onto the stack, the stack and finally the top element.

 

6. Turn the minimum number array

  The beginning of an array of several elements moved to the end of the array, the array we call rotation.
  A non-descending order of the input array of a rotation of the output rotary smallest element array.
  For example, an array {3,4,5,1,2} {1,2,3,4,5} is a rotation of the array to a minimum.
  NOTE: All the elements are given in greater than 0, if the array size is 0, return 0.

  solution:

  Binary Search: a [left] a [right] && a [left]> a [mid], to the left statute, or to the right (a [mid]> a [left] <a [right] or [left] a> > a [right])

 

6. frog jump stairs

  A frog can jump on a Class 1 level, you can also hop on level 2. The frog jumped seeking a total of n grade level how many jumps (the order of different calculation different results).

  Feibolaqi number of columns. a [0] = 1, a [1] = 2;

 

7. metamorphosis jump stairs

  A frog can jump on a Class 1 level, you can also hop on level 2 ...... n It can also jump on stage. The frog jumped seeking a total of n grade level how many jumps.

   pow(2,n-1)

 

8. rectangular footprint

  We can use a small rectangle 2 * 1 sideways or vertically to cover a larger rectangle. Will the small rectangle of n 2 * 1 coverage without overlap a large rectangle 2 * n, a total of how many ways?
  Fibonacci number.

 

9. A binary number of the number 1

  An integer that indicates the number of the output of the binary number 1 in. Wherein a negative number indicates a complement.

  solution:

  Because there are negative numbers, the number of right judgment is not feasible, and should be left as 1 and continue operation.

 

10. The value of the integer power

  Given a double base type and floating-point type int integer exponent. Seeking exponent of the power base. Ensure the base and exponent are not simultaneously 0

  solution:

  Two equal 0 is first determined, Base type double, and need 0 determines the absolute value of the subtraction is smaller than a very small number. If the exponent is less than 0, then invert the count, and taking the reciprocal of the result.

  Calculation: In addition to a better circulation method

  

 

   Integer division is rounded down, it is only necessary to go on a multiple n is odd it.

  

11

Guess you like

Origin www.cnblogs.com/wshr007/p/11511739.html