Interview Questions: Word flip (concise code & efficiency)

Source: Internet
Author: User

Author: Chen taihan

Word flip is a frequently used interview question by a large company. I have read a lot of implementation methods on the Internet and I feel defective. Today I am bored again. I wrote two implementation methods myself.
One is the simplified version and the other is the efficiency version.
The simplified version is concise and clear, with clear ideas and easy to understand, but it still needs to be improved in terms of efficiency. After improvement, we find that it is not so easy to understand, so we have an efficiency version,
I personally advocate the simplified version. It looks so comfortable that I am very satisfied.
Why is the efficiency of the simplified version defective? Because the invertword parameter of the method is a value transfer, there will be variable construction and analysis,
The efficiency version solves this problem and changes to reference transfer, but there are some other problems arising from reference transfer. Take a lookCodeYou will understand.

// Flip a sentence
// I am a student --> Student A am I
// Flip each word first, and then flip the entire sentence

Simplified Version

1 # Include < Iostream >
2 # Include < String >
3   Using Namespace STD;
4
5   Class Invertwords {
6 Public :
7 Invertwords ( String * Wo): words (WO ){}
8 Void Invert ()
9 {
10 Int Len = Words -> Size ();
11 Int Beg =- 1 ;
12 // Flip the entire string
13 Invertword (beg, Len );
14 // Flip every word
15 For ( Int I = 0 ; I < Len; I ++ )
16 {
17 If (Words -> At (I) = ' ' )
18 {
19 Invertword (beg, I );
20 Beg = I;
21 }
22 }
23 }
24
25 Private :
26 Void Invertword ( Int Beg, Int End)
27 {
28 Char TMP;
29 While ( ++ Beg <-- End)
30 {
31 TMP = Words -> At (BEG );
32 Words -> At (BEG) = Words -> At (end );
33 Words -> At (end) = TMP;
34 }
35 }
36 String * Words;
37 }; Efficiency Edition

1 # Include < Iostream >
2 # Include < String >
3 Using Namespace STD;
4
5 Class Invertwords {
6 Public :
7 Invertwords ( String * Wo): words (WO ){}
8 Void Invert ()
9 {
10 Int Len = Words -> Size ();
11 Int Beg =- 1 , K;
12 // Flip the entire string
13 Invertword (beg, Len );
14 // Because the invertword parameter of the method is of reference type, the beg and Len values are modified, so we need to restore their values.
15 Beg =- 1 ;
16 Len = Words -> Size ();
17 // Flip every word
18 For ( Int I = 0 ; I < Len; I ++ )
19 {
20 K = I; // K is used to save the site. In order to restore the I to the current value
21 If (Words -> At (I) = ' ' )
22 {
23 Invertword (beg, I );
24 I = K; // Restore I
25 Beg = I;
26 }
27 }
28 }
29
30 Private :
31 Void Invertword ( Int & Beg, Int & End)
32 {
33 Char TMP;
34 While ( ++ Beg <-- End)
35 {
36 TMP = Words -> At (BEG );
37 Words -> At (BEG) = Words -> At (end );
38 Words -> At (end) = TMP;
39 }
40 }
41 String * Words;
42 }; Test code

1 Int Main ()
2 {
3 String S = " I am a student " ;
4 Invertwords * RW = New Invertwords ( & S );
5 RW -> Invert ();
6 Cout < S < Endl;
7 Int A = 0 ;
8 CIN > A;
9 Return 0 ;
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.