. Net reverse question answer page 1/2

Source: Internet
Author: User

Complete the Methods: CopyCode The Code is as follows: static void reverse (INT [] array, int begin, int end)
{
...
}

The reverse method sorts the elements in the array from the in subscript to the end subscript in reverse order. For example, the initial values of an array are [1, 2, 3, 4, 5, 6], begin is 1, and end is 4. After the reverse is called, the elements in the array become [1, 5, 4, 3, 2, 6] in sequence. the elements from array [1] to array [4] are in reverse order. To add more ...... In fact, this does not need to be added: This method needs to verify the correctness of the input parameters. If the user calls this method to pass in invalid parameters, the user needs to throw an exception and clarify the cause. You can use your favorite languages to implement: C #, VB, Java, Ruby, Python ...... However, do not use the functions in the built-in library. :)

It's easy, isn't it? Unfortunately, up to now, only one person has given the correct answer. If you haven't done this, take a piece of paper, write down your answer, and then listen to Lao Zhao...

Subject Logic
The topic logic of this question is actually very simple. Isn't it a reverse order of part of the array? However, there is also a big gap in code clarity from this point. There is a gap between good practices and common practices in programming difficulty and understanding. For example:

1. The practice of many friends is: since it is a part of the element of the inverted array, you only need to find the middle position, then calculate the offset from begin, and then ...... How can this problem be solved? Well, it seems that the numbers of elements in the beginning and end must be processed separately based on the odd or even numbers.

2. Another friend's approach is to open a new array (Length: End-begin + 1), put the elements between begin and end in the new array, and then reverse order, and then copy it back.

3. Another friend thinks to use the stack: Push the elements between in and end to the stack, and assign values to begin and end one by one pop, so that the reverse order is ...... No! Data structure is good!

It is a pity that such an approach is complicated. The time complexity of the three methods is O (end-begin), but only the space complexity of the first method is O (1), and the latter two are linear spatial complexity. So what is the best practice in Zhao's eyes?Copy codeThe Code is as follows: public static void reverse (INT [] array, int begin, int end)
{
While (end> begin)
{
Int temp = array [begin];
Array [begin] = array [end];
Array [end] = temp;

Begin ++;
End --;
}
}

The two subscripts "end" and "begin" start from the initial value and start to approach the middle. Each time, the elements in the array are exchanged. In the end, while will find end = begin (an odd number of elements between begin and end), or end <begin (an even number of elements between begin and end ). In either case, the reverse order has been completed.
Parameter Verification
The above practice should be said to be the simplest one. However, when judging the answer, the result is correct. Zhao believes that the answer is correct. It is a pity that almost no friend is in the "parameter verification" aspect.

By the way, a friend gave me an interesting message: "The teacher only gave me one test data. If you want to use other test data, for example, it is special, ask the instructor to list the test data next time." This sentence makes me confused: The test data is endless. Do I need to list them all? Usually writeProgramWill the user point out all his operation steps? The purpose of the test data is to help you understand the meaning of the question. The requirements of the questions are clearly written so that the questions can be meaningful. If a question only needs to run the given test data, who will not? Lao Zhao can now provide the 10 thousand template immediately:Copy codeThe Code is as follows: if (...)
{
Return...
}
Else if (...)
{
Return...
}

...

It is reasonable to say that, even if the question does not indicate that parameter verification is required, an excellent implementation should also include this.

As a matter of fact, it is not difficult to list all parameter errors:Copy codeThe Code is as follows: 1. array = NULL
2. Begin <0;
3. End <begin
4. End> = array. Length

SO should other cases be determined together? For example, end <0, array. Length = 0 or begin> = length. Lao Zhao believes that "no judgment does not matter", because the above judgment has ensured no additional errors. So begin = end is a problem? Lao Zhao believes that this judgment can also be omitted. But ...... If begin> end, should the two values be exchanged? I don't know why some of my friends have done this, but Lao Zhao believes that a method should not "adjust" the parameters in general-in fact, all major types of libraries will not be so superfluous. If a friend has a different opinion, we can continue to discuss it.

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.