Detailed answers to. NET reverse-order topics 1th/2 page _ Practical Tips

Source: Internet
Author: User
Please add the method complete:
Copy Code code as follows:

static void Reverse (int[] array, int begin, int end)
{
...
}

The function of the reverse method is to reverse the elements in an array array, from the beginning to the subscript, such as the initial value of an array is [1, 2, 3, 4, 5, 6],begin is 1,end 4, then when reverse is invoked, The elements in array arrays are then sorted by [1, 5, 4, 3, 2, 6], and the elements before them from array[1] to array[4 are reversed. In addition, add a little ... In fact, there is no need to add: This method needs to verify the correctness of incoming parameters, if the user calls the method when the illegal parameters, then need to throw an exception, and write the reason. You can use the language you like to implement: C#,vb,java,ruby,python ... However, do not use the functionality already in the built-in library. :)

It's simple, isn't it? Unfortunately, only 1 people have given the right answer to the current deadline. If you have not done this topic, then in view of the analysis below, might as well take a piece of paper with a pen, write your answer, and then listen to Lao Zhao slowly speak ...

subject Logic
The subject logic of this topic is actually very simple. is not to reverse the sequence of parts of the array? But from this point on, there is a big gap between the clarity of the code. Good practice and common practice, from the difficulty of programming and understanding of a certain gap. For example:

1, a lot of friends do is: since it is a part of the inverse array of elements, so long as the middle of the position, and then calculate the offset and begin, then ... How to do it, it's done-well, it seems to be necessary to deal with the number of elements in the middle of the begin and end, either odd or even.

2. Other friends ' practice is to open a new array (length End-begin + 1), place the elements between the begin and end into the new array, then reverse the sequence, and then copy back.

3, there is a friend think with the stack: to the end of the element to push to the stack, and then a pop out in order to assign values to begin to end, so the reverse sequence ... Well! Data structure is good to learn!

Unfortunately, this approach is a bit more complicated. The time complexity of the 3 approaches is O (End–begin), but the space complexity of the first approach is O (1), and both are linear spatial complexities. So what's the best thing about old Zhao's eyes?
Copy Code code 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--;
}
}

End and begin two subscript begin at the beginning to the middle, each time exchanging elements in the array. Eventually, while judging, the end = = is found to be a total number of odd elements, or the total number of even elements between end < Begin (between Begin and end). In either case, the reverse sequence is complete.
parameter Checksum
The above method should be said to be the simplest one, but the actual in the evaluation of the answer, the direct result is correct, old Zhao all think is correct. Unfortunately, almost no friends in the "parameter check" in this respect.

By the way, a friend left a message for me is very interesting: "The teacher only gave a test data, if you want to use other test data, such as special, then ask the teacher next time the test data list." "This sentence makes me callous: test data is endless, do you need to list it?" Usually write a program, the user will point out all his steps? The purpose of the test data is to help understand the question, the requirements of the topic are written clearly, do the problem only meaningful. If a problem requires only the test data that is given, then who is not going to do it? Lao Zhao here can immediately give a universal template:
Copy Code code as follows:

if (...)
{
Return ...
}
else if (...)
{
Return ...
}

...

Logically speaking, even if the topic does not specify the need for parameter verification, a good implementation should also bring this.

In fact, as long as the careful, it is not difficult to enumerate all the errors of the parameters:
Copy Code code as follows:

1, array = = NULL
2, begin < 0;
3, End < begin
4, end >= array. Length

Are there other situations that should be judged together? For example, end < 0,array. Length = = 0 or begin >= length. Mr. Zhao said that "no judgment also has nothing to do", because the above judgment has ensured that there will be no additional errors. So is the BEGIN = = end a problem? Lao Zhao thinks, this judgment also can omit. But...... If begin > end, should the values of both be exchanged? I don't know why some friends do it, but Mr. Zhao believes that in general a method should not be an extra "adjustment" for parameters--in fact, the large class libraries are not so superfluous. If any friend has a different view, we can continue the discussion.
Current 1/2 page 12 Next read the full text

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.