java算法3~翻转字符串

时间:2021-11-01 13:11:25

要求:

     1、"you       are  a      dog" 变为  great   are  you

     2、时间复杂度O(N)


思路:

       1、先将String转换成char[],

       2、将char[]整体逆序,得到        god      a  era       uoy

       3、将char[]中每一个单词再逆序   dog      a  are       you


package algorithm_database;
/**
* 翻转字符串:you are great 变为 great are you
*
*/
public class InverseString {

public static void main(String[] args){
String str = "you are a dog";
char[] charStr = str.toCharArray();
inverse(charStr,0,charStr.length-1);
System.out.println(charStr);
int l = -1;
int r = -1;
for(int i=0;i<charStr.length;i++){
if(charStr[i] !=' '){
l = i==0 || charStr[i-1]==' ' ?i:l; //很难理解?为了找每一个单词的初始下标
r = i==charStr.length-1||charStr[i+1]==' ' ?i:r; //很难理解?为了找每一个单词的结束下标
}
if(l!=-1 && r!=-1){
inverse(charStr,l,r);
l = -1;
r = -1;
}
}
System.out.println(charStr);
}

public static char[] inverse(char[] charStr,int start ,int end){
while(start<end){
char temp = charStr[start];
charStr[start] = charStr[end];
charStr[end] = temp;
start++;
end--;
}
return charStr;
}
}

找下标的逻辑有些不好理解,其他的都很容易