翻转字符串里的单词
1.基本方法
从后往前遍历源字符串,遇到空格就跳过,遇到单词就将其存入新字符串,之后在单词后面加入一个空格,以此类推,直到最后返回的时候去掉最后加入的空格即可。
//leetcode-151
class Solution {
public String reverseWords(String s) {
char []ch = s.toCharArray();
char []ch1 = new char[s.length() + 1];
//源数组坐标
int i = s.length() - 1;
//新数组坐标
int j = 0;
while(i >= 0){
while(i >= 0 && ch[i] == ' '){
i--;
}
int t = i;
while(i >= 0 && ch[i] != ' '){
i--;
}
for(int m = i + 1; m <= t; m++){
ch1[j++] = ch[m];
//每个单词后加一个空格
if(m == t){
ch1[j++] = ' ';
}
}
}
String str = String.valueOf(Arrays.copyOfRange(ch1, 0, j - 1));
return str;
}
}
2.特殊方法
代码随想录中提供了一种新思路,在源字符串上直接操作,空间复杂度为O(1)。
1.移除多余空格
2.将整个字符串反转
3.将每个单词反转
(在java中此方法的空间复杂度依然为O(n),因为字符串为不可变量,改变会在内部声明一个新的字符串)
下面引入代码随想录的相关代码以供参考。
//leetcode-151
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;
}
}
}
左旋转字符串
1.基本方法
声明一个新字符串,先存储源字符串右部分,在存储源字符串左部分。
//leetcode-剑指Offer.58-II
class Solution {
public String reverseLeftWords(String s, int n) {
char []ch = s.toCharArray();
char []re = new char[s.length()];
int j = 0;
for(int i = n; i < s.length(); i++){
re[j++] = ch[i];
}
for(int i = 0; i < n; i++){
re[j++] = ch[i];
}
return String.valueOf(re);
}
}
2.特殊方法
1.反转区间为前n的子串
2.反转区间为n到末尾的子串
3.反转整个字符串
下面引入代码随想录的配图及代码以便理解。
//leetcode-剑指Offer.58-II
class Solution {
public String reverseLeftWords(String s, int n) {
int len=s.length();
StringBuilder sb=new StringBuilder(s);
reverseString(sb,0,n-1);
reverseString(sb,n,len-1);
return sb.reverse().toString();
}
public 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--;
}
}
}
未完待续。。。