2015-08-11 [pea pod]--Development--1 Noodle

Source: Internet
Author: User
Tags collabedit

Time: 2015-08-11 15:00 ~ 16:00

Location: Hung Tong Building, posts and telecommunications

Category: Electric surface

1. Ask your CV.

2. Algorithm questions:

Platform: Collabedit

/*

1. Converting a string to an integer
*/

#include <string>#include<climits>usingSTD::string;stringTrimConst string&str) {    intFirst_index =0;  while(First_index <str.size ()) {        if(Str[first_index]! =' ')             Break; First_index++; }    intLast_index = Str.size ()-1;  while(Last_index >=0) {        if(Str[last_index]! =' ')             Break; Last_index--; }    returnStr.substr (First_index, Last_index-first_index +1);}//1, "abc", not string//123456789012345, 2, too longintLast_err_code;//"123456", 123456//" -123456"- -123456//" ", 0 last_err_code = 1//"abc", 0 last_err_code = 1//"+" 0 last_err_code = 1//-0 Last_err_code = 1//"123456789012345", 0 last_err_code = 2intString_to_int (Const string&str) {    intindex =0; BOOLIs_negative =false; Long LongRET =0; Const stringNum_str =trim (str); if(num_str.size () = =0) {Last_err_code=1; return 0; }    if(num_str[0] =='-') {is_negative=false; Index=1; } Else if(num_str[0] =='+') {is_negative=true; Index=1; }    if(Index = =1&& index = =num_str.size ()) {        // "+"        // "-"Last_err_code =1; return 0; }     while(Index <num_str.size ()) {        if(Num_str[index] <'0'|| Num_str[index] >'9') {            //"ABC"//"123CBD"Last_err_code =1; return 0; } ret= RET *Ten+ (int) (Num_str[index]-'0'); if(Ret > (Long Long) {Int_max) {Last_err_code=2; return 0; } Index++; }        returnIs_negative? -1* (int) RET: (int) ret;}

3. Algorithm questions:

Platform: Collabedit

/*
Give two increments of integers a and B with lengths of N and M, respectively. Find out the small number of K in both arrays.
For example: A = [4, 8, ten], B = [2, 3, 4, 5], K = 4. The number of the first K small is 3.
*/

intLast_err_code;//Return-1 when error and set Last_err_codeintFind_kth_small (intA[],intNintB[],intMintK) {    intAns =0; intIndex_a =0; intIndex_b =0; intindex =1; if(N <0|| M <0|| K > n + M | | K <=0) {Last_err_code=1; return-1; }         while(Index <K) {if(Index_a < n && Index_b <m) {if(A[index_a] <B[index_b]) index_a++; ElseIndex_b++; } Else if(Index_a <N) {index_a++; } Else{Index_b++; } Index++;//ERROR forgot to increase the index at the time!     }    if(Index_a < n && Index_b <m) ans= A[index_a] < B[index_b]?A[index_a]: B[index_b]; Else if(Index_a <N) Ans=A[index_a]; Elseans=B[index_b]; returnans;}

Time complexity: O (K)

The interviewer lets optimize:

intLast_err_code;//Return-1 when error and set Last_err_codeintFind_kth_small (intA[],intNintB[],intMintK) {    intA_left =0; intB_left =0; intk_2; inta_k_2; intb_k_2; if(N <0|| M <0|| K > n + M | | K <=0) {Last_err_code=1; return-1; }     while(K >2) {k_2= (K-1) /2; A_k_2= A_left +k_2; A_k_2= A_k_2 < n-1? A_k_2:n-1; B_k_2= B_left +k_2; B_k_2= B_k_2 < M-1? B_k_2:m-1; if(A[a_k_2] = =B[b_k_2]) {            returnA[a_k_2]; } Else if(A[a_k_2] <B[b_k_2]) {K-= A_k_2-A_left; A_left=a_k_2; if(A_left = = N-1)                returnB[b_left + K-2]; } Else{K-= B_k_2-B_left; B_left=b_k_2; if(B_left = = M-1)                returnA[a_left + K-2]; }    }    if(K = =1)        returnA[a_left] < B[b_left]?A[a_left]: B[b_left]; returnA[a_left] > B[b_left]?A[a_left]: B[b_left];}

2015-08-11 [pea pod]--Development--1 Noodle

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.