In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "what is the method of Java left rotation string". In daily operation, I believe that many people have doubts about the method of Java left rotation string. Xiaobian consulted all kinds of data and sorted out a simple and easy-to-use operation method. I hope it will be helpful to answer the doubt of "what is the method of Java left rotation string?" Next, please follow the editor to study!
Public class LeftRotateString {/ * Q 26 left rotation string * title: define the left rotation operation of a string: move several characters in front of the string to the end of the string. * for example, rotate the string abcdef by 2 bits to the left to get the string cdefab. * Please implement the function of rotating the string to the left. The complexity of requiring time to operate on a string of length n is O (n) and the auxiliary memory is O (1). * / public static void main (String [] args) {String data = "abcdef"; String re = leftRotateString (data, 2); System.out.println (re);} / * * abcdef- > ab.cdef- > ba.fedc- > cdefab * / public static String leftRotateString (String str, int n) {if (str = = null | str.length () = 0) {return str } if (n = str.length ()) {return str;} int begin = 0; int end = str.length ()-1; char [] letters = str.toCharArray (); # consider reference passing to modify values reverseString (letters, begin, n-1); reverseString (letters, n, end); reverseString (letters, begin, end) Return new String (letters);} public static void reverseString (char [] letters, int begin, int end) {/ * * of course we can do it like this: StringBuilder sb=new * StringBuilder (str); sb.reverse (). ToString (); but we are learning * algorithm so let's' reinvent the wheel'. * / if (begin > = end) {return;} for (int I = begin, j = end; I
< j; i++, j--) { char tmp = letters[i]; letters[i] = letters[j]; letters[j] = tmp; } System.out.println("---"+new String(letters)+"---"); /** * ---bacdef--- ---bafedc--- ---cdefab--- cdefab * */ }}================================public class ShiftLeft { static String str; public ShiftLeft(String str){ this.str = str; } public static void main(String[] args) { ShiftLeft sl = new ShiftLeft("abcdefghi"); System.out.printf(sl.shift(12)); } /** * abcdef循环左移两位得到cdefab *1 暴力求解,将左移字母的暂存临时变量中,将其他字母移位,再将临时变量,补充到移位后字符串的后面 *2 ba ihgfedc ->Cdefghiab * / private String shift (int digits) {if (digits = = 0) {return str;} else if (digits > 0) {digits = digits% str.length (); / / is the key to ensure cyclic shift. You can enter shift String left = reverse (str.substring (0MagneDigits) that is greater than the length of the string. String right = reverse (str.substring (digits)); System.out.println ("left:" + left); System.out.println ("right:" + right); return reverse (left + right);} else {/ /
< 0 digits = -digits; digits = digits % str.length(); //同上 return shift(str.length() - digits); } } private static String reverse(String s) { int start = 0; int end = s.length() - 1; char[] temp = new char[s.length()]; for (int i = 0; i< temp.length; i++){ temp[start] = s.charAt(end); temp[end] = s.charAt(start);// s.charAt(start) = s.charAt(end); 这样写是有问题的 start++; end--; } return String.valueOf(temp); }}=====================package com.lifeibigdata.algorithms.blog;public class LeftRotateString { /** * Q 26 左旋转字符串 * 题目:定义字符串的左旋转操作:把字符串前面的若干个字符移动到字符串的尾部。 * 如把字符串abcdef左旋转2位得到字符串cdefab。 * 请实现字符串左旋转的函数。要求时间对长度为n的字符串操作的复杂度为O(n),辅助内存为O(1)。 */ public static void main(String[] args) { String data = "abcdef"; String re = leftRotateString(data, 2); System.out.println(re); } /* * abcdef->Ab.cdef- > ba.fedc- > cdefab * / public static String leftRotateString (String str, int n) {if (str = = null | | str.length () = = 0) {return str;} if (n = str.length ()) {return str;} int begin = 0; int end = str.length ()-1; char [] letters = str.toCharArray () ReverseString (letters, begin, n-1); reverseString (letters, n, end); reverseString (letters, begin, end); return new String (letters);} / / public static String reverseString (String str,int begin,int end) {public static void reverseString (char [] letters, int begin,int end) {/ * of course we can do it like this: StringBuilder sb=new * StringBuilder (str) Sb.reverse (). ToString (); but we are learning * algorithm so let's' reinvent the wheel'. * / if (begin > = end) {return;} for (int I = begin, j = end; I < j; char tmp = letters [I]; letters [I] = letters [j]; letters [j] = tmp;} System.out.println ("-" + new String (letters) + "- -") / *-bacdef--- bafedc--- cdefab--- cdefab * * /}} at this point, the study on "what is the method of Java's left rotation of a string" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.