代码随想录一刷-Day07
LeetCode344.反转字符串
public void reverseString(char[] s) {
// 倒是简单,双指针
if (s.length == 0 || s.length == 1) {
return;
}
int left = 0, right = s.length - 1;
while (left < right) {
char temp = s[left];
s[left] = s[right];
s[right] = temp;
left++;
right--;
}
}
时间复杂度: O(n)
空间复杂度: O(1)
541. 反转字符串II
public String reverseStr(String s, int k) {
char[] chars = s.toCharArray();
int left = 0, right = 0;
while (right < chars.length) {
right++;
if (right - left == 2 * k) {
reverse(chars, left, left + k - 1);
left = right;
}
}
reverse(chars, left, Math.min(left + k - 1, chars.length - 1));
return String.valueOf(chars);
}
private void reverse(char[] chars, int left, int right) {
while (left < right) {
char temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++;
right--;
}
}
时间复杂度: O(n)
空间复杂度: O(1)
剑指Offer 05.替换空格
用 StringBuilder 来做会简单点,不过这次没用那个
public String replaceSpace(String s) {
ArrayList<Integer> whiteSpace = new ArrayList<>();
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (Character.isWhitespace(chars[i])) {
whiteSpace.add(i);
}
}
if (whiteSpace.size() == 0) {
return s;
}
int whiteSpaceNum = whiteSpace.size();
char[] newChars = new char[chars.length + whiteSpaceNum * 2];
Iterator<Integer> iterator = whiteSpace.iterator();
Integer next = iterator.next();
for (int i = 0, j = 0; i < newChars.length; i++, j++) {
if (next == j) {
newChars[i] = '%';
newChars[++i] = '2';
newChars[++i] = '0';
if (iterator.hasNext()) {
next = iterator.next();
}
} else {
newChars[i] = chars[j];
}
}
return String.valueOf(newChars);
}
时间复杂度:O(n)
空间复杂度:O(n)
LeetCode151.翻转字符串里的单词
我的解法毫无技巧:
publicString reverseWords(String s) {
ArrayDeque<String> strs = new ArrayDeque<>();
char[] chars = s.toCharArray();
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= chars.length; i++) {
if (i == chars.length || Character.isWhitespace(chars[i])) {
if (sb.length() > 0) {
strs.add(sb.toString());
}
sb = new StringBuilder();
} else {
sb.append(chars[i]);
}
}
sb = new StringBuilder();
while (!strs.isEmpty()) {
String pop = strs.pollLast();
if (pop.length() == 0) {
continue;
}
sb.append(pop);
if (!strs.isEmpty()) {
sb.append(" ");
}
}
return sb.toString();
}
时间复杂度:O(n)
空间复杂度:O(n)
卡哥的题解
卡哥的题解想法挺妙的:先反转整个字符串,然后逐个反转单词,这个做法可以减少空间复杂度,但是时间复杂度仍是 O(n)
/**
* 不使用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;
}
}