Cambridge offer series (1~10)

Source: Internet
Author: User

1. Title description 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. Idea: From the bottom of the left, small, upward, large, to the right.
classSolution { Public:    BOOLFind (vector<vector<int> > Array,inttarget) {                if(array.size () = =0)return false; intR=array.size (); intc=array[0].size (); inti=r-1, j=0;  while(i<r&&i>-1&&j<c&&j>-1){                        if(Array[i][j] = = target)return true; if(array[i][j]>target) {i--; }Else{J++; }        }        return false; }};
View Code

2. Title Description Please implement a function to replace the space in a string with "%20". For example, when the string is we are Happy. The string after substitution is we%20are%20happy. Idea: Replace spaces.
//Length is the maximum length for a cow system to specify a string output, fixed to a constantclassSolution { Public:    voidReplacespace (Char*STR,intlength) {    inti =0; intj =0; intNspace =0; Char*pstr =NULL; PStr= (Char*)malloc(sizeof(Char) *length *3);  for(i =0, j =0; i<length; i++, J + +)    {        if(' '==Str[i]) {Pstr[j]='\%'; pstr[++J] ='2'; pstr[++J] ='0'; }        Else{Pstr[j]=Str[i]; }    }     for(i=0; i<j;i++) {Str[i]=Pstr[i]; }     Free(PSTR); PStr=NULL;}};
View Code3. Title Description Enter a list of values for each node of the list to print from the end of the header. Idea: Use vector to pick up, then call reverse algorithm reverse (v.begin (), V.end ());
/** * struct ListNode {* int val;* struct ListNode *next;* listnode (int x):* val (x), Next (NULL) {*}*};*/classSolution { Public: Vector<int> Printlistfromtailtohead (structlistnode*head) {                structlistnode* tmp =Head; Vector<int>v;  while(TMP! =NULL) {V.push_back (TMP-val); TMP= tmp->Next;        } reverse (V.begin (), V.end ()); returnv; }};
View Code

4. The topic describes the input of a binary tree of the pre-sequence traversal and the results of the middle sequence traversal, please reconstruct 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.  For example, enter the pre-sequence traversal sequence {1,2,4,7,3,5,6,8} and the middle sequence traversal sequence {4,7,2,1,5,3,8,6}, then rebuild the binary tree and return. Idea: Through the preamble and the middle order, find the root, and then recursive call.
/** Definition for binary tree * struct TreeNode {* int val; * TreeNode *left; * TreeNode *right; * T Reenode (int x): Val (x), left (null), right (NULL) {} *}; */classSolution { Public:    structtreenode* Reconstructbinarytree (vector<int> pre,vector<int>inch) {        intIn_size =inch. Size (); if(In_size = =0)            returnNULL; Vector<int>Pre_left, Pre_right, In_left, in_right; intval = pre[0]; TreeNode* node =NewTreeNode (Val);//root node is the first element in pre        intp =0;  for(P < in_size; + +)p) {            if(inch[P] = = val)//Find The root position in                 Break; }         for(inti =0; i < in_size; ++i) {            if(I <p) {In_left.push_back (inch[i]);//Construct The left pre and inPre_left.push_back (Pre[i +1]); }            Else if(I >p) {In_right.push_back (inch[i]);//Construct The right pre and inPre_right.push_back (Pre[i]); }} node->left =Reconstructbinarytree (Pre_left, in_left); Node->right =Reconstructbinarytree (Pre_right, in_right); returnnode; }  };
View Code5. Title description use two stacks to implement a queue, complete the queue push and pop operations. The elements in the queue are of type int. Idea: The data passes in and out, after 2 stacks can be implemented in a queue.
classsolution{ Public:    voidPushintnode)    {Stack1.push (node); }    intpop () {if(Stack2.empty ()) { while(!Stack1.empty ())                {Stack2.push (Stack1.top ());            Stack1.pop (); }        }        intnode =Stack2.top ();        Stack2.pop (); returnnode; }Private: Stack<int>Stack1; Stack<int>Stack2;};
View Code6. The title describes moving 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 a non-descending 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.
Note: All elements given are greater than 0, and if the array size is 0, return 0. Idea: To find the mutation point, the next is the desired value.
classSolution { Public:    intMinnumberinrotatearray (vector<int>Rotatearray) {        intI=0; intLen =rotatearray.size (); if(len==0)return 0; if(len==1)returnrotatearray[0];  while(i<len-1){            if(rotatearray[i]<=rotatearray[i+1]) {i++; }Else{                 Break; }        }       returnrotatearray[i+1]; }};
View Code7. Title Description Everyone knows the Fibonacci sequence and now asks for an integer n, please output the nth of the Fibonacci sequence. N<=39 Fibonacci Series, also known as the Golden Section, refers to a sequence of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 、...... Mathematically, the Fibonacci sequence is defined as a recursive method: F (0) =0,f (1) =1,f (n) =f (n-1) +f (n-2) (n≥2,n∈n*)
classSolution { Public:    intFibonacci (intN) {if(n = =0)            return 0; if(n = =1)            return 1; intNUMFN1 =0, numfn2 =1; intCurrentnum;  for(intI=2; i<=n; ++i) {currentnum= numfn1+numfn2; Numfn1=numfn2; Numfn2=Currentnum; }        returnCurrentnum; }};
View Code8. The title describes a frog can jump up to 1 steps at a time, can also jump on the 2 level. Ask the frog to jump on an n-level step with a total number of hops. Thinking: With the 7th question
classSolution { Public:    intJumpfloor (intNumber ) {        if(Number = =1)return 1; if(Number = =2)return 2; intVal; intnum1=1; intNum2=2;  while(number-->2) {Val= num1+num2; NUM1=num2; Num2=Val; }        returnVal; }};
View Code9. The title describes a frog can jump up to 1 steps at a time, can also jump on the 2 level ... It can also jump on n levels. Ask the frog to jump on an n-level step with a total number of hops. Idea: a[n]=a[n-1]+a[n-2]+......+a[1]; ....... ............ ①a[n-1]= a[n-2]+......+a[1], ......... .......... ② two-type subtraction: a[n]=2*a[n-1];
class Solution {public:    int jumpfloorii (int number ) {          int f=1, fn=1;          for (int i=2; i<=number;i++) {            fn=2*F;            F=fn;        }         return fn;    }};
View Code10. The title describes that we can use a small rectangle of 2*1 to cover the larger rectangle horizontally or vertically. What is the total number of ways to cover a large rectangle of 2*n with n 2*1 small rectangles without overlapping? Idea: the same 7
classSolution { Public:    intRectcover (intNumber ) {        if(number==1)return 1; if(number==2)return 2; intn1=1, n2=2, val=0;  for(intI=2; i<number;i++) {Val= n1 +N2; N1=N2; N2=Val; }        returnVal; }};
View Code

Cambridge offer series (1~10)

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.