Step-by-step write algorithm (number of returns)

Source: Internet
Author: User

Original: Step-by-step write algorithm (number of returns)

"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


The concept of the number of returns is interesting, that is, there is a string str, the length is N, now index begins to traverse from 0->INDEX/2, then str[index] = Str[n-1-index], then this data is what we usually say the number of returns. For example a = "A" is a return number, a = "ABA" is the number of returns, A = "Strarts" is also the number of returns. Because the problem is relatively simple, many companies like to use it to check the programmer's basic programming skills. Not only that, it also examines the programmer's consideration of whether the problem is well-conceived and whether to consider it from different perspectives.

For example, now we want the characters in the string to be lowercase letters or uppercase letters, not other characters, how to write? Friends can try.

int issymbolright (const char* str, int length) {int Index;char symbol;for (index = 0; index < length; index++) {symbol = s Tr[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, le ngth)) 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 interviewer said the interview with a recursive method how to calculate it? 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)) Retu RN 0;return _isanagramornot (str, length);}
Then, we will increase the difficulty a little more, if the comparison of data a lot, there are 10 million, then how to use multi-core programming to improve the speed of data processing?

int _isanagramornot (const char* STR, int start, int end, int length) {int Index;char symbol;for (index = 0; index < lengt H  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 forfor (index = 0; index < 2; index + +) Result[index] = _isanagramornot (str, start[index], End[index], lengt H >> 2); return (Result[0] && result[1])? 1:0;}

Summarize:

(1) From the above topics can be seen, even if very simple topic, you can also examine the total ability of candidates

(2) To improve the efficiency of the algorithm implementation of a lot of ways, friends usually can pay more attention to the class, accumulate

(3) The execution of all algorithms is based on correctness and robustness, and must be built on the basis of adequate testing.



Step-by-step write algorithm (number of returns)

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.