Java face question Zero-based: Write a function to reverse the string

Source: Internet
Author: User

Preface: In order to find a representative of my poetry from the beginning of the zero, I have to find, but how can not find a fit my mind, forced on the degree Niang also never found.        I really want to take the first grade of primary school language textbooks out well read through, it is sure to find the answer. Well, since you can't find it, you don't have to worry about it. Today read the "High-performance Programmer's Training," the fourth chapter, found himself rotten, if according to the author's view, I was no matter what his discernment, so, I told myself, from scratch, the author put forward the face of the question according to Java to write again, whether it is to refer to others or their own slightly can think of ways,        Knock them out yourself and record them! Write a function to reverse the string, as the beginning of this series, Java naturally also has a lot of implementations, I believe many people have done this blog, but I still have these records in their own blog, to the best of my ability to master them:
Package Com.honzh.mwq.reverse;import Java.util.arrays;import Java.util.collections;import java.util.List;import Java.util.stack;public class Test {public static void main (string[] args) {//First, I can get what I want first, use subscript reverse to remove character to reorganize SYSTEM.OUT.P  Rintln (Reverse1 ("Diyizhong"));  The second kind, I thought for a long while to realize, I found that I am not suitable for programming the all//cycle is still from 0 to Length-1, but will be taken out first put in after take out the back System.out.println (Reverse2 ("Dierzhong"));  Third, the use of collections class reversal List of the way, more trouble System.out.println (Reverse3 ("Disanzhong"));  Fourth, this is more concise than the third one, but I myself have a deep memory of the reversal method of collections//And the memory of StringBuilder or stringbuffer is not very deep, so now I remember  System.out.println (Reverse4 ("Disizhong")); Fifth, the use of stacks, the so-called advanced after//Of course, first you know the stack from the API, it is the expansion of the vector, by push to put into operation, and then pop method to eject//I think the stack is still necessary to conduct in-depth research,  See how the source code is extended, I must master System.out.println (REVERSE5 ("Diwuzhong")); The sixth kind, is completely copy other people's, I can't even understand why? I've always felt that I was a logical dementia//I don't know how I've been through a long programming career for 7 years, including college ha, I've always wondered how far I can go,//because for the sixth way, I want to know the kind of things that will soon be forgotten in a moment, and I wonder what I'm living on. This has always been my pain point, I am a bit allergic to various sorting algorithms, I hate these sorting algorithms, but it seems like the learning//programming people have to understand. Otherwise unworthy to doA programmer, and I belong to the unworthy of this. The only way to do this is to replace the first position of the character Sing Woo in the position of the opposite character//its key is whether the length of the string is odd or even when the time is still able to symmetry processing, it is clear that this practice in the process of 1234 cycles two times//in the processing of 123 is also two times, 123 of the time will be 2 as a symmetrical position to deal with//But the most important thing is that the number of processing is obviously the most efficient//still remember when an interview candidate to answer the question, I was very ashamed, I feel that I can not face their boss, because I did not think there is such a treatment method//  This method is still worth remembering well System.out.println (Reverse6 ("Diliuzhong"));  System.out.println (Reverse6 ("1234")); System.out.println (Reverse6 ("123"));  public static string Reverse1 (string old) {string result = "";  for (int i = Old.length ()-1; I >= 0; i--) {result + = String.valueof (Old.charat (i)); } return result;  public static string Reverse2 (string old) {string result = "";  for (int i = 0; i < old.length (); i++) {result = Old.charat (i) + result; } return result;  public static string Reverse3 (string old) {string result = "";  list<string> olds = arrays.aslist (Old.split (""));  Collections.reverse (olds);  for (String s:olds) {result + = s; } return result; } public static string Reverse4 (string old) {return new STringbuffer (old). Reverse (). toString (); return new StringBuilder (old). Reverse (). toString ();  public static string Reverse5 (string old) {char[] chars = Old.tochararray ();  stack<character> oldstack = new stack<character> ();  for (Character c:chars) {Oldstack.push (c);  } String result = "";  while (!oldstack.empty ()) {result + = Oldstack.pop (); } return result;  public static string Reverse6 (string old) {char[] chars = Old.tochararray ();  int n = chars.length-1;   for (int i = 0; I <= n/2; i++) {Char temp = chars[i];   Chars[i] = chars[n-i];  Chars[n-i] = temp; } return new String (chars); }}





Java face question Zero-based: Write a function to reverse the string

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.