Write an algorithm step by step (the return value)

Source: Internet
Author: User

 

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

The concept of return number is more fun, that is to say, there is such a string str, the length is n, now the index starts to traverse from 0-> index/2, then str [index] = str [n-1-index], this type of data is usually called the return number. For example, a = "a" indicates the return number, a = "aba" indicates the return number, and a = "strarts" indicates the return number. Because this question is relatively simple, many companies like to use it to check the basic programming capabilities of programmers. Not only that, but it can also examine whether programmers consider the problem carefully and from different perspectives.

 

For example, we now require that the characters in a string must be lower-case letters or upper-case letters, but not other characters. What should we do? You can try it.

 

 

Int isSymbolRight (const char * str, int length)

{

Int index;

Char symbol;

 

For (index = 0; index <length; index ++ ){

Symbol = str [index];

 

If (symbol> = 'A' & symbol <= 'Z' | symbol> = 'A' & symbol <= 'Z ')

Continue;

 

Return 0;

}

 

Return 1;

}

 

Int isAnagramOrNot (const char * str, int length)

{

Int index;

 

If (NULL = str | 0 = length)

Return 0;

 

If (! IsSymbolRight (str, length ))

Return 0;

 

For (index = 0; index <(length> 1); index ++ ){

If (str [index]! = Str [length-1-index])

Return 0;

}

 

Return 1;

}

Int isSymbolRight (const char * str, int length)

{

Int index;

Char symbol;

 

For (index = 0; index <length; index ++ ){

Symbol = str [index];

 

If (symbol> = 'A' & symbol <= 'Z' | symbol> = 'A' & symbol <= 'Z ')

Continue;

 

Return 0;

}

 

Return 1;

}

 

Int isAnagramOrNot (const char * str, int length)

{

Int index;

 

If (NULL = str | 0 = length)

Return 0;

 

If (! IsSymbolRight (str, length ))

Return 0;

 

For (index = 0; index <(length> 1); index ++ ){

If (str [index]! = Str [length-1-index])

Return 0;

}

 

Return 1;

} The above method is only a traditional comparison method. If the interview Examiner says that the recursive method is used, how can this problem be calculated? Friends can try again.

 

 

Int _ isAnagramOrNot (const char * str, int length ){

If (0 = length | 1 = length)

Return 1;

 

Return (str [0] = str [length-1])? _ IsAnagramOrNot (str + 1, length-2): 0;

}

 

Int isAnagramOrNot (const char * str, int length)

{

If (NULL = str | 0 = length)

Return 0;

 

If (! IsSymbolRight (str, length ))

Return 0;

 

Return _ isAnagramOrNot (str, length );

}

Int _ isAnagramOrNot (const char * str, int length ){

If (0 = length | 1 = length)

Return 1;

 

Return (str [0] = str [length-1])? _ IsAnagramOrNot (str + 1, length-2): 0;

}

 

Int isAnagramOrNot (const char * str, int length)

{

If (NULL = str | 0 = length)

Return 0;

 

If (! IsSymbolRight (str, length ))

Return 0;

 

Return _ isAnagramOrNot (str, length );

} So we have increased the difficulty. If there are more than 10 million pieces of data compared, how can we use multi-core programming to increase the data processing speed?

 

Int _ isAnagramOrNot (const char * str, int start, int end, int length)

{

Int index;

Char symbol;

 

For (index = 0; index <length; index ++ ){

If (str [start + index]! = Str [end-1-index])

Return 0;

 

Symbol = str [start + index];

If (symbol> = 'A' & symbol <= 'Z' |

Symbol> = 'A' & symbol <= 'Z ')

Continue;

 

Return 0;

}

 

Return 1;

}

 

Int isAnagramOrNot (const char * str, int length)

{

Int index;

Int start [2];

Int end [2];

Int result [2] = {0 };

 

If (NULL = str | 0 = length)

Return 0;

 

Start [0] = 0;

Start [1] = length> 2;

End [0] = length;

End [1] = length-(length> 2 );

 

# Pragma omp parallel

For (index = 0; index <2; index ++)

Result [index] = _ isAnagramOrNot (str, start [index], end [index], length> 2 );

 

Return (result [0] & result [1])? 1: 0;

}

Int _ isAnagramOrNot (const char * str, int start, int end, int length)

{

Int index;

Char symbol;

 

For (index = 0; index <length; index ++ ){

If (str [start + index]! = Str [end-1-index])

Return 0;

 

Symbol = str [start + index];

If (symbol> = 'A' & symbol <= 'Z' |

Symbol> = 'A' & symbol <= 'Z ')

Continue;

 

Return 0;

}

 

Return 1;

}

 

Int isAnagramOrNot (const char * str, int length)

{

Int index;

Int start [2];

Int end [2];

Int result [2] = {0 };

 

If (NULL = str | 0 = length)

Return 0;

 

Start [0] = 0;

Start [1] = length> 2;

End [0] = length;

End [1] = length-(length> 2 );

 

# Pragma omp parallel

For (index = 0; index <2; index ++)

Result [index] = _ isAnagramOrNot (str, start [index], end [index], length> 2 );

 

Return (result [0] & result [1])? 1: 0;

}

 

 

Summary:

 

(1) From the above questions, we can see that even a very simple question can also examine the total abilities of candidates.

 

(2) There are many ways to improve Algorithm Execution efficiency. Friends can pay more attention to and accumulate more in their daily classes.

 

(3) The execution of all algorithms is based on correctness and robustness, and must be based on full testing.

Related Article

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.