The sword refers to Offer programming questions

 1. Title: Search in a 2D Array

    In a two-dimensional array, each row is sorted in increasing order from left to right, and each column is sorted in increasing order from top to bottom. Please complete a function, input such a two-dimensional array and an integer, and determine whether the array contains the integer.

Solution: Start the comparison from the lower left corner of the two-dimensional array, let i = n - 1 , j = 0; (assuming the array is a, i is the row number to be compared, and j is the column number), when the target value is the same as a[i ][j], when comparing, if it is equal to the target value, it will end, if the target value is less than a[i][j], then i--, otherwise j++, pay attention to the range restrictions of i and j.

Code

2. Topic: Replace spaces

    Please implement a function to replace spaces in a string with "%20". For example, when the string is We Are Happy., the replaced string is We%20Are%20Happy.

Solution: This problem is a replacement done on an array. The length of the array meets the requirements. First, set two pointers, p1, p2, p1 points to the position of the last character in the original array, p2 points to the last character after the calculation, and the processing is completed. The pointer of the character, and then mainly p1, first determine whether p1 is a space, if so, copy "02%" (note that it is reversed) to the position corresponding to p2, p2-1, p2-2, pay attention to p2 The processing of the pointer, if the character pointed to by p1 is not a space, then copy the value to the position pointed to by p2, p2--, and keep doing this until p1 == p2.

Code

3. Topic: Print a linked list from beginning to end  

    Enter a linked list and print the value of each node in the linked list from end to beginning.

    Solution: traverse the linked list to input the value into the vector , and then use reverse to flip the vector.

Code

4. Topic: Rebuild a binary tree

    Enter the results of preorder traversal and inorder traversal of a binary tree, and reconstruct the binary tree. It is assumed that the results of the input preorder traversal and inorder traversal do not contain duplicate numbers. For example, input the preorder traversal sequence {1,2,4,7,3,5,6,8} and the inorder traversal sequence {4,7,2,1,5,3,8,6}, then rebuild the binary tree and return.

Solution : First of all, you need to understand the characteristics of pre-order traversal and in-order traversal. First find , and then find the same node in the in-order traversal table, then its left side is The left subtree, the right subtree is the right subtree , and then continue to divide according to this rule, and the recursive establishment can be done.

Code

5. Topic: Two stacks implement queues

    Use two stacks to implement a queue to complete the Push and Pop operations of the queue. The elements in the queue are of type int.

Solution: There are two stacks stack1, stack2. Use stack1 to complete the push operation of the queue, and use stack2 to complete the pop operation. When popping, first determine whether there are elements in stack2. If not, first put the elements in stack1. Put it into stack2, and then implement pop.

Code

6. Title: Rotate the smallest number of an array

    Moving the first elements of an array to the end of the array is called the rotation of the array. Input a rotation of a non-decreasingly sorted array, output 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. NOTE: All elements given are greater than 0, if the array size is 0, please return 0.

    Solution: This problem uses the properties of rotating arrays. The rotation array has two or one ordered sequence, and the minimum value can be gradually approximated by the method of bisection. Suppose the middle element is mid, the left value of the bisection is lt, and the right value is rt. If mid >= lt, it is still in the first In an increasing sequence, let the bipartite lvalue start at mid, otherwise it is not processed. If mid <= lt , indicating that mid is in the second increasing sequence, let the rvalue of the bisection start at mid. A special case is: 5 2 2 1 2 2, which needs to add continue after completing the first branch; (Nioke.com data is too watery).

Code

7. Topic: Fibonacci Sequence

    Everyone knows the Fibonacci sequence, and now you are asked to input an integer n, please output the nth item of the Fibonacci sequence. (n<=39)

    Solution: F( i ) = F( i - 1 ) + F( i - 2 ), where F( i ), F(i - 1) and F( i - 2 ) can be directly replaced by variables.

    Code

8. Topic: Jumping the stairs

    A frog can jump up 1 steps at a time, or 2 steps at a time. Find how many ways the frog can jump up a n-level stair.

    Solution: It can be done with the idea of ​​dp: dp( i ) = dp( i - 1) + dp( i - 2) , that is, the number of jumps for i-level steps is equal to the number of jumps for i-1 level and i- The sum of the number of hops for level 2.

    Code

   Variations of this topic: A frog can jump up one level of steps at a time, or up to two levels at a time. However, since the frog is relatively young, it cannot jump two steps in a row. Find the total number of jumping methods that the frog can jump on an n-level step.

   Solution: Go directly to the formula: dp[ i ] = dp[ i - 1] + dp[ i - 3] , that is, one step from i - 1 + 1 step from i - 3 and two steps. 

9. Topic: Jumping the stairs abnormally

    A frog can jump up 1 steps at a time, it can jump up 2 steps...it can also jump up n steps. Find how many ways the frog can jump up a n-level stair.

    Solution: Formula: F[ i ] = F [ i - 1] + F[ i - 2] + F[ i - 3] .....F[1], similarly F[i - 1] = F[ i - 2] ......F[1]. F[i ] = 2F[i - 1] for the subtraction of the two equations. further simplification

F[ i ] = 2^(i-1)(i >= 1)。

Code

10. Title: Rectangle Covering

    We can use 2*1 small rectangles to cover larger rectangles horizontally or vertically. How many ways are there to cover a large 2*n rectangle with n small rectangles of 2*1 without overlapping?

   解法:公式 : dp[ i ] = dp [ i - 1] + dp[ i - 2],i - 1的加上 ,i - 2的,其中i - 2的放两个竖型的小矩形和i - 1的重复了,所以加一个i - 2。

代码实现

12.题目:数值的整数次方

    给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。

    解法:用C++完全可以用pow( )函数直接搞定。但是看网上许多人都是考虑了许多的情况。。。。。。。

    代码实现

13.题目:调整数组顺序使得奇数位于偶数前面

    输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
    解法:一种复杂度高点的方法是,利用两个指正,i 指向偶数,j 指向奇数,且 j 在 i 后面,因为题目中提到移动后相对顺序不变,所以如果 i 和 j 之间还有偶数,则让 i 和 j 之间的数统一后移一位,然后将 j 处的值赋给 i 处,继续这样做。另一个种方法是牺牲空间换时间,只要另开辟一个数组就好了。
      代码实现

30.题目:连续子数组的最大和

    HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决。但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢?例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。你会不会被他忽悠住?(子向量的长度至少是1)。

    解法:最大连续子串和。

代码实现

37.题目:数字在排序数组中出现的次数

     统计一个数字在排序数组中出现的次数。

      解法:直接用lower_bound()计算一个位置,upper_bound()计算一个位置,然后相减就可以了,复杂度O(2logn);

代码实现

  






Guess you like

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