Algorithm series (I) inverted output of string Arrays

Source: Internet
Author: User

I haven't updated my blog for a long time. When I interviewed some candidate, I found that many of my friendsAlgorithmThe problem is always difficult. It may be the reason why there are not many problems in your work. Let's review some typical algorithms.

This algorithm should be optimal in terms of time complexity and minimum space usage.

N/2 times of array execution in time

Space Operations are only performed in the original array

Code
1 /**/ ///   <Summary>
2 /// Reverse String Array
3 ///   </Summary>
4 ///   <Param name = "strsource"> Original String Array </Param>
5 ///   <Returns> Returned results </Returns>

6 Public   String [] Reverseword ( String [] Strsource)
7 {
8
9 // Calculate the number of times to traverse
10 Int Count = Strsource. Length %   2   =   1   ? (Strsource. Length -   1 ) /   2 : Strsource. Length /   2 ;
11 // Index of the last digit of the array
12 Int Length = Strsource. Length -   1 ;
13 // Temporary Variable
14 String Tempstring =   String . Empty;
15
16 For ( Int I =   0 ; I < Count; I ++ )
17 {
18 Tempstring = Strsource [I];
19 Strsource [I] = Strsource [Length - I];
20 Strsource [Length - I] = Tempstring;
21 }

22 Return Strsource;
23 }

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.