This article is participating in the “Java Theme Month – Java Brush questions punch card”, see the activity link for details
Topic describes
Left-rotation of a string moves several characters from the beginning to the end of the string. Define a function to rotate the string left. For example, if you enter the string “abcdefg” and the number 2, the function will return “cdefgab” as a result of the left rotation.
Example 1: Input: s = "abcdefg", k = 2 Output: "Cdefgab" source: LeetCode link: https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof Copyright belongs to The Collar buckle network. Commercial reprint please contact official authorization, non-commercial reprint please indicate the source.Copy the code
Thought analysis
- This is a string related question. You can either loop through the problem directly or call the system function substring() to solve the problem.
AC code
public String reverseLeftWords(String s, int n) {
int length = s.length();
n %= length;
StringBuilder ans = new StringBuilder();
for (int i = n; i < length; i++) {
ans.append(s.charAt(i));
}
for (int i = 0; i < n; i++) {
ans.append(s.charAt(i));
}
return ans.toString();
}
public String reverseLeftWords1(String s, int n) {
return s.substring(n, s.length()) + s.substring(0, n);
}
Copy the code
Submit tests:
conclusion
- The time complexity of the cyclic method is O(n), the space complexity is O(1).
- Substring system function time complexity is O(1), space complexity is O(1)
- In the actual code submission process, calling the system function substring() is better. Let’s look at the implementation of substring().
Substring () function analysis
The substring () implementation
public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > value.length) {
throw new StringIndexOutOfBoundsException(endIndex);
}
int subLen = endIndex - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return ((beginIndex == 0) && (endIndex == value.length)) ? this
: new String(value, beginIndex, subLen);
}
Copy the code
- From the above implementation analysis, subString fully analyzes the boundary and returns either the object itself or a new object.
New String(value, beginIndex, subLen) implementation
- Further analyze the new String(Value, beginIndex, subLen) method.
public String(char value[], int offset, int count) {
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
if (count <= 0) {
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
if (offset <= value.length) {
this.value = "".value;
return; }}// Note: offset or count might be near -1>>>1.
if (offset > value.length - count) {
throw new StringIndexOutOfBoundsException(offset + count);
}
this.value = Arrays.copyOfRange(value, offset, offset+count);
}
Copy the code
- Array.copyofrange (value, offset, offset+count);
The Arrays. CopyOfRange () implementation
public static char[] copyOfRange(char[] original, int from, int to) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + ">" + to);
char[] copy = new char[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}
Copy the code
- This is implemented by calling the System function directly
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
Copy the code
- Through the above code analysis, the review makes it clear that Java String Objects are immutable.
- Stick to the daily question, come on!