344. 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.
题目解析:
- 双指针从两边往中间移动
代码:
class Solution {
public void reverseString(char[] s) {
int left = 0, right = s.length - 1;
while (left < right) {
char temp = s[left];
s[left++] = s[right];
s[right--] = temp;
}
}
}
541. 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.
题目解析:
- 与上题类似使用双指针反转字符串
char[] to String: new String(char[])
代码:
class Solution {
public String reverseStr(String s, int k) {
int size = s.length();
char[] arr = s.toCharArray();
for (int left = 0; left < size; left += 2*k) {
int right = left + 2 * k - 1;
if (right >= size && size - left < k) {
reverse(arr, left, size - 1);
} else {
reverse(arr, left, left + k - 1);
}
}
return new String(arr);
}
public void reverse(char[] arr, int left, int right) {
while (left < right) {
char temp = arr[left];
arr[left++] = arr[right];
arr[right--] = temp;
}
}
}
剑指 Offer 05. 替换空格
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
题目解析:
- 为了保证有足够的空间,需要3倍的空间
代码:
class Solution {
public String replaceSpace(String s) {
char[] result = new char[s.length() * 3];
int idx = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ' ') {
result[idx++] = '%';
result[idx++] = '2';
result[idx++] = '0';
} else {
result[idx++] = s.charAt(i);
}
}
return new String(result, 0, idx);
}
}
151. 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.
题目解析:
- 第一种:可以先将字符串spit成单词数组,再反转单词的位置
- 第二种:可以先反转整个字符串,再把每个单词反转
代码: 第一种
class Solution {
public String reverseWords(String s) {
String[] words = s.trim().split("\\s+");
StringBuilder sb = new StringBuilder();
for (int i = words.length - 1; i >= 0; i--) {
sb.append(words[i]);
if (i != 0) {
sb.append(" ");
}
}
return sb.toString();
}
}
第二种
ass Solution {
public String reverseWords(String s) {
char[] arr = s.trim().toCharArray();
if (arr.length <= 1) {
return new String(arr);
}
reverse(arr, 0, arr.length - 1);
StringBuilder sb = new StringBuilder();
int start = 0;
for (int end = 0; end < arr.length; end++) {
if (arr[end] != ' ') {
if (end == 0 || end > 0 && arr[end - 1] == ' ') {
start = end;
}
if (end == arr.length - 1 || end < arr.length - 1 && arr[end + 1] == ' ') {
reverse(arr, start, end);
sb.append(new String(arr, start, end - start + 1));
if (end != arr.length - 1) {
sb.append(" ");
}
}
}
}
return sb.toString();
}
public void reverse(char[] arr, int start, int end) {
while (start < end) {
char temp = arr[start];
arr[start++] = arr[end];
arr[end--] = temp;
}
}
}
剑指 Offer 58 - II. 左旋转字符串
字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。
题目解析:
- 可以使用substring方法对字符串进行切片
代码:
class Solution {
public String reverseLeftWords(String s, int n) {
return s.substring(n, s.length()) + s.substring(0, n);
}
}