2013年11月2日星期六

This is a cup of tea string flip problem ......

The This is a cup of tea into
tea of ​​cup a is This
finished writing the code did not find that last turn or sihT This
public class TheSwap {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String str="This is a cup of tea";
        char[] chr=str.toCharArray();
        for(char ele:chr){
            System.out.print(ele);
        }
        int l=chr.length;
        for(int i=0;i<l/2;i++){
            char temp=chr[i];
            chr[i]=chr[l-i-1];
            chr[l-i-1]=temp;
        }
        System.out.println();
        for(char ele:chr){
            System.out.print(ele);
        }
        System.out.println();
        swapIn(chr);
        System.out.println("FINAL IS:");
        for(char ele:chr){
            System.out.print(ele);
        }
    }
    
    private static void swapIn(char[] c){
        int count=0;
        for(int i=0;i<c.length;i++){ 
            if(c[i]!=' '){
                count++;
                continue;
        }
            if(c[i]==' ' || (i==c.length-1)){
                if(count==1){
                    count=0;
                    continue;
                }
                else{
                    int tempCount=0;
                    for(int j=i-count;j<i-Math.floor(count/2);j++){
                        char ctemp=c[j];
                        c[j]=c[i-1-tempCount];
                        c[i-1-tempCount]=ctemp;
                        tempCount++;
                    }
                    count=0;
                }
            }
        }
        
    }
}

------ Solution ------------------------------------- -------
LZ head to see the code to a simple point ~

public class TheSwap {
public static void main(String[] args) {
String str = "This is a cup of tea";
char[] arr = str.toCharArray();
System.out.println("before:" + str);
swapIn(arr);
System.out.println("after:" + String.valueOf(arr));
}

private static void swapIn(char[] arr) {
if(arr.length == 0) return;//空串
//反转整个字符串
rotate(arr, 0, arr.length - 1);
//以空格区分,逐个反转子字符串
int count = 0;//非空格字符数量
for(int i = 0;i < arr.length;++i) {
if(arr[i] != ' ') ++count;
else {
rotate(arr, i - count, i - 1);
count = 0;
}
}
//反转最后一个字符串
rotate(arr, arr.length - count, arr.length - 1);
}

//反转字符串
private static void rotate(char[] arr,int start,int end) {
for(int i = start;i <= (start + end) / 2;++i) {
char temp = arr[i];
arr[i]= arr[end - (i - start)];
arr[end - (i - start)] = temp;
}
}
}


------ For reference only ---------------------------------------
String str="This is a cup of tea ";
        String arr [] = str.split(" ");
        for (int i=arr.length-1; i>=0; i--) {
            System.out.print(arr[i] + " ");
        }

- ----- For reference only ---------------------------------------

not split (), I forgot to say, is to write a reverse algorithm , can not use an existing function .
------ For reference only -------------------------------------- -
string split it. You converted to char too much trouble . Recommendations upstairs .
------ For reference only -------------------------------------- -
amount is not stated clearly
The purpose of this program is to write an O (n) algorithm
Also, do not use any existing functions , of course I know that split, if the interviewer can not let you use it ?
------ For reference only -------------------------------------- -
converted to char is because the problem is actually located was originally a char [] array where the program is to facilitate one by one with a tochar avoid assignment
------ For reference only - -------------------------------------
can refer to:

public static String reverse(String source){
String[] array = source.split(" ");  //分割字符串得到单词数组
int length = array.length;  //数组元素的个数
int count = length / 2; //数组元素首尾交换需要的趟数
String temp;
for(int i = 0; i < count; i++){ //首尾交换数组元素的位置, 0和length-1交换, 1和length-2交换...
temp = array[i];
array[i] = array[length - i - 1];
array[length - i - 1] = temp;
}
//将数组转为String
StringBuilder target = new StringBuilder();
for(int i = 0; i < length; i++){
target.append(array[i]).append(" ");
}
return target.deleteCharAt(source.length()).toString();
}

Or:

public static String reverse(String source){
StringBuilder sb = new StringBuilder();
String[] sources = source.split(" "); //分割字符串得到单词数组
for(String s : sources){
//在第0号位置插入单词,原先在第0号的单词就会不断的往后移,最先进来的被移动最后面
sb.insert(0, s + " "); 
}
return sb.deleteCharAt(sb.length() - 1).toString();
}

------ For reference only ----------------------------------- ----

ah can only be this line of thinking only of my own to make a change I do not know where the problem is actually with you out of the same ideas < br>

没有评论:

发表评论