From: http://fayaa.com/tiku/view/3/
Well, it is said that the interview questions for it name enterprises such as MS/Google are as follows:
Given a string, for example:
The quick brown fox jumps over the lazy dog.
Please design the programSentence
Reversely, that is, after completion, it is as follows:
Dog. Lazy the over jumps Fox brown quick
BTW: time space is important
Update
: After giving a solution, the interviewer will generally let you "Back up", that is, it is not allowed to allocate an O (n) space, requiring space O (1)
A friend's famous saying is a routine. The idea is to flip the entire sentence first, and then take out each word.
This is basically the case:
A. "The quick brown fox jumps over the lazy dog." => ". God Zyal EHT revo spmuj XOF nworb kciuq EHT"
B. ". God Zyal EHT revo spmuj XOF nworb kciuq EHT" => (find each word) "dog. Lazy the over jumps Fox brown quick"
We know that it is relatively easy to flip words in situ. We can use swap before and after an intermediate variable. The following is an implementation of our own:
Void reversestring (char psztext [], int length) <br/>{< br/> If (psztext = NULL) return; <br/> char temp; <br/> for (INT I = 0; I <length/2; I ++) <br/> {<br/> temp = psztext [I]; <br/> psztext [I] = psztext [length-i-1]; <br/> psztext [length-i-1] = temp; <br/>}< br/>}
Next we need to find the words in a sentence to flip them separately, so we can do this:
Void reversestatement (char psztext [], int length) <br/>{< br/> reversestring (psztext, length); <br/> int p1 = 0; <br/> int P2 = 0; </P> <p> while (P2 <= length) <br/> {<br/> If (P2 = length | psztext [P2] = '') <br/>{< br/> reversestring (& psztext [P1], P2-P1); <br/> P1 = P2 + 1; <br/>}< br/> P2 ++; <br/>}< br/>}
After a try, the effect is good. In the beginning, the space that cannot be allocated an O (n) is afraid to re-extract an array to reverse copy it. That method is really bad. Haha