Link: https://leetcode.com/problems/reverse-string/
Write a function that reverses a string. The input string is given as an array of characters s
.
You must do this by modifying the input array in-place with O(1)
extra memory.
start
points to the first element of s
, end
points to the last element of s
.start
and end
, then both pointers move towards the middle at the same time.start
and end
meet.class Solution {
public void reverseString(char[] s) {
int left = 0;
int right = s.length - 1;
while (left < right) {
char temp = s[left];
s[left] = s[right];
s[right] = temp;
left++;
right--;
}
}
}
Link: https://leetcode.com/problems/reverse-string-ii/
Given a string s
and an integer k
, reverse the first k
characters for every 2k
characters counting from the start of the string.
If there are fewer than k
characters left, reverse all of them. If there are less than 2k
but greater than or equal to k
characters, then reverse the first k
characters and leave the other as original.
s
:
k
characters left, reverse k
elements; else, reverse all of themi = i + 2k
, traverse every 2k
characters.class Solution {
public String reverseStr(String s, int k) {
char[] c = s.toCharArray();
for (int i = 0; i < c.length; i += 2 * k) {
if (i + k < c.length) {
swap(c, i, i + k - 1);
}
else {
swap(c, i, c.length - 1);
}
}
return new String(c);
}
private void swap(char[] c, int left, int right) {
while (left < right) {
char temp = c[left];
c[left] = c[right];
c[right] = temp;
left++;
right--;
}
}
}
class Solution {
public String reverseStr(String s, int k) {
StringBuffer result = new StringBuffer();
int start = 0;
while (start < s.length()) {
StringBuffer temp = new StringBuffer();
int firstK = start + k < s.length() ? start + k : s.length();
int secondK = start + 2 * k < s.length() ? start + 2 * k : s.length();
temp.append(s.substring(start, firstK));
result.append(temp.reverse());
if (firstK < secondK)
result.append(s.substring(firstK, secondK));
start += 2 * k;
}
return result.toString();
}
}
请实现一个函数,把字符串 s
中的每个空格替换成"%20"
。
left
points to the end of the previous array, right
to the end of the expanded array.left
:
s[left]= ' '
, replace it with “%20”; else let s[left] = s[right]
left--, right--
class Solution {
public String replaceSpace(String s) {
if (s == null || s.length() == 0)
return s;
StringBuilder sb = new StringBuilder();
for (char c: s.toCharArray()) {
if (c == ' ')
sb.append(" ");
}
int left = s.length() - 1;
s += sb.toString();
int right = s.length() - 1;
char[] ch = s.toCharArray();
while (left >= 0) {
if (ch[left] == ' ') {
ch[right--] = '0';
ch[right--] = '2';
ch[right] = '%';
}
else
ch[right] = ch[left];
right--;
left--;
}
return new String(ch);
}
}
Link: https://leetcode.com/problems/reverse-words-in-a-string/
Given an input string s
, reverse the order of the words.
A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.
Return a string of the words in reverse order concatenated by a single space.
Note that s
may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.
start
and end
start
and end
meet.start
and end
start
of a word, end points to the end
of the word, which is the element before the space.start
and end
points to the next wordclass Solution {
public String reverseWords(String s) {
StringBuilder sb = removeSpace(s);
reverseString(sb, 0, sb.length() - 1);
return reverseEachWord(sb);
}
private StringBuilder removeSpace(String s) {
char[] ch = s.toCharArray();
int start = 0;
int end = s.length() - 1;
StringBuilder sb = new StringBuilder();
while (ch[start] == ' ')
start++;
while (ch[end] == ' ')
end--;
while (start <= end) {
char c = ch[start];
if (c != ' ' || sb.charAt(sb.length() - 1) != ' ')
sb.append(c);
start++;
}
return sb;
}
private void reverseString(StringBuilder sb, int start, int end) {
while (start < end) {
char temp = sb.charAt(start);
sb.setCharAt(start, sb.charAt(end));
sb.setCharAt(end, temp);
start++;
end--;
}
}
private String reverseEachWord(StringBuilder sb) {
int start = 0;
int end = 1;
while (start < sb.length()) {
while (end < sb.length() && sb.charAt(end) != ' ')
end++;
reverseString(sb, start, end - 1);
start = end + 1;
end = start + 1;
}
return sb.toString();
}
}
Link: https://leetcode.cn/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/
字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。
class Solution {
public String reverseLeftWords(String s, int n) {
StringBuilder sb= new StringBuilder(s);
reverseString(sb, 0, n - 1);
reverseString(sb, n, s.length() - 1);
return sb.reverse().toString();
}
private void reverseString(StringBuilder sb, int start, int end) {
while (start < end) {
char temp = sb.charAt(start);
sb.setCharAt(start, sb.charAt(end));
sb.setCharAt(end, temp);
start++;
end--;
}
}
}