My LeetCode Diary - Day8 String
344. Reverse String
Link
class Solution {
public void reverseString(char[] s) {
char[] newArray = new char[s.length];
for (int i = 0; i < s.length; i++) {
newArray[i] = s[s.length - i - 1];
}
for (int i = 0; i < s.length; i++) {
s[i] = newArray[i];
}
}
}
541. Reverse String II
Link
class Solution {
public String reverseStr(String s, int k) {
StringBuilder sb = new StringBuilder();
while (s.length() >= 2 * k) {
String temp = s.substring(0, k);
sb.append(new StringBuilder(temp).reverse());
sb.append(s.substring(k, 2 * k));
s = s.substring(2 * k);
}
if (s.length() <= k) {
sb.append(new StringBuilder(s).reverse());
} else {
sb.append(new StringBuilder(s.substring(0, k)).reverse());
sb.append(s.substring(k));
}
return sb.toString();
}
}
class Solution {
public String reverseStr(String s, int k) {
StringBuffer res = new StringBuffer();
int length = s.length();
int start = 0;
while (start < length) {
// 找到k处和2k处
StringBuffer temp = new StringBuffer();
// 与length进行判断,如果大于length了,那就将其置为length
int firstK = (start + k > length) ? length : start + k;
int secondK = (start + (2 * k) > length) ? length : start + (2 * k);
//无论start所处位置,至少会反转一次
temp.append(s.substring(start, firstK));
res.append(temp.reverse());
// 如果firstK到secondK之间有元素,这些元素直接放入res里即可。
if (firstK < secondK) { //此时剩余长度一定大于k。
res.append(s.substring(firstK, secondK));
}
start += (2 * k);
}
return res.toString();
}
}
151. Reverse Words in a String
Link
class Solution {
void reverse(StringBuilder str, int i, int j) {
while (i < j) {
char temp = str.charAt(i);
str.setCharAt(i, str.charAt(j));
str.setCharAt(j, temp);
i++;
j--;
}
}
public String reverseWords(String s) {
s = s.trim();
StringBuilder str = new StringBuilder(s);
int j = str.length() - 1;
int i = j;
boolean space = false;
while (i >= -1) {
if (i == -1 || str.charAt(i) == ' ') {
if (!space) {
reverse(str, i + 1, j);
space = true;
} else {
str.delete(i, i + 1);
}
} else {
if (space) {
j = i;
space = false;
}
}
i--;
}
return str.reverse().toString();
}
}
class Solution {
/**
* 不使用Java内置方法实现
* <p>
* 1.去除首尾以及中间多余空格
* 2.反转整个字符串
* 3.反转各个单词
*/
public String reverseWords(String s) {
// System.out.println("ReverseWords.reverseWords2() called with: s = [" + s + "]");
// 1.去除首尾以及中间多余空格
StringBuilder sb = removeSpace(s);
// 2.反转整个字符串
reverseString(sb, 0, sb.length() - 1);
// 3.反转各个单词
reverseEachWord(sb);
return sb.toString();
}
private StringBuilder removeSpace(String s) {
// System.out.println("ReverseWords.removeSpace() called with: s = [" + s + "]");
int start = 0;
int end = s.length() - 1;
while (s.charAt(start) == ' ') start++;
while (s.charAt(end) == ' ') end--;
StringBuilder sb = new StringBuilder();
while (start <= end) {
char c = s.charAt(start);
if (c != ' ' || sb.charAt(sb.length() - 1) != ' ') {
sb.append(c);
}
start++;
}
// System.out.println("ReverseWords.removeSpace returned: sb = [" + sb + "]");
return sb;
}
/**
* 反转字符串指定区间[start, end]的字符
*/
public void reverseString(StringBuilder sb, int start, int end) {
// System.out.println("ReverseWords.reverseString() called with: sb = [" + sb + "], start = [" + start + "], end = [" + end + "]");
while (start < end) {
char temp = sb.charAt(start);
sb.setCharAt(start, sb.charAt(end));
sb.setCharAt(end, temp);
start++;
end--;
}
// System.out.println("ReverseWords.reverseString returned: sb = [" + sb + "]");
}
private void reverseEachWord(StringBuilder sb) {
int start = 0;
int end = 1;
int n = sb.length();
while (start < n) {
while (end < n && sb.charAt(end) != ' ') {
end++;
}
reverseString(sb, start, end - 1);
start = end + 1;
end = start + 1;
}
}
}