参考文章
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.
Example 1:
Input: s = "the sky is blue"
Output: "blue is sky the"
Example 2:
Input: s = " hello world "
Output: "world hello"
Explanation: Your reversed string should not contain leading or trailing spaces.
Example 3:
Input: s = "a good example"
Output: "example good a"
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
-
遇到的问题
去除多余空格后没有考虑重置数组大小。
class Solution {
public String reverseWords(String s) {
char[] ch = s.toCharArray();
// remove blank space
ch = removeExtraSpace(ch);
// reverse string s;
ch = reverseHelper(0, ch.length - 1, ch);
// reverse each word;
ch = reverseWord(ch);
return s.copyValueOf(ch);
}
public char[] removeExtraSpace(char[] ch){
int slow = 0;
// delete blank space
for(int fast = 0; fast < ch.length; fast++){
if(ch[fast] != ' '){
if(slow != 0){
ch[slow++] = ' ';
}
while(fast < ch.length && ch[fast] != ' '){
ch[slow++] = ch[fast++];
}
}
}
// resize the array
char[] newCh = new char[slow];
System.arraycopy(ch, 0, newCh, 0, slow);
return newCh;
}
public char[] reverseHelper(int left, int right, char[] ch){
while(left <= right){
char tmp = ch[right];
ch[right--] = ch[left];
ch[left++] = tmp;
}
return ch;
}
public char[] reverseWord(char[] ch){
int l = 0;
for(int r = 0; r < ch.length; r++){
if(ch[r] == ' '){
ch = reverseHelper(l, r-1, ch);
l = r + 1;
}
// the last word in the string
if(r == ch.length - 1){
ch = reverseHelper(l, r, ch);
}
}
return ch;
}
}
344. Reverse String
public void reverseString(char[] s) {
int left = 0;
int right = s.length - 1;
while(left <= right){
char tmp = s[left];
s[left++] = s[right];
s[right--] = tmp;
}
}
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.
Example 1:
Input: s = "abcdefg", k = 2
Output: "bacdfeg"
Example 2:
Input: s = "abcd", k = 2
Output: "bacd"
这题需要分情况讨论
解法
class Solution {
public String reverseStr(String s, int k) {
char[] c = s.toCharArray();
for(int i = 0; i < c.length; i += 2*k){
// 字符串长度大于K翻转K个字符
if(i + k < c.length){
c = reverseHelper(i,i+k-1,c);
}else{
// 字符串长度小于 K 则全部翻转
c = reverseHelper(i, c.length - 1, c);
}
}
return s.copyValueOf(c);
}
public char[] reverseHelper(int left, int right, char[] c){
while(left <= right){
char tmp = c[left];
c[left++] = c[right];
c[right--] = tmp;
}
return c;
}
}