lintcode算法題之53-翻轉字符串中的單詞

給定一個字符串,逐個翻轉字符串中的每個單詞。

樣例

樣例  1:
	輸入:  "the sky is blue"
	輸出:  "blue is sky the"
	
	樣例解釋: 
	返回逐字反轉的字符串.

樣例 2:
	輸入:  "hello world"
	輸出:  "world hello"
	
	樣例解釋: 
	返回逐字反轉的字符串.

代碼區:public class Solution {
    /*

     *username:softstarhhy
     * @param s: A string
     * @return: A string
     */
    public String reverseWords(String s) {
        // write your code here
        s=s.trim();
        char[] chars=s.toCharArray();
        int[] spacelocnums=new int[chars.length];
        for (int i=0;i<chars.length ;i++ )
        {
            if(chars[i]==' ')
            {
                spacelocnums[i]=1;
            }
            
        }
        int beforenum=0,afternum=0;
        int current=0;
        for(int j=0;j<chars.length;j++,current++)//當前指針指向一個非空格值
        { 
            
            beforenum=j-1;
            afternum=j+1;
            if(j==0)
            {
                j=j;
                chars[0]=chars[j];
            }else if(spacelocnums[j]!=1)
          {
              chars[current]=chars[j];
          }else if(spacelocnums[beforenum]!=1&&spacelocnums[j]==1&&spacelocnums[afternum]==1)
        {
           current--;
        }else if(spacelocnums[beforenum]!=1&&spacelocnums[j]==1&&spacelocnums[afternum]!=1)
            {
                chars[current]=chars[j];
            }
            
            else if(spacelocnums[beforenum]==1&&spacelocnums[j]==1&&spacelocnums[afternum]!=1)
            {
                chars[current]=chars[j];
                
            }else if(spacelocnums[beforenum]==1&&spacelocnums[j]==1&&spacelocnums[afternum]==1)
            {
                current--;
            }else if(spacelocnums[beforenum]==1&&spacelocnums[j]!=1&&spacelocnums[afternum]!=1)
            {
                chars[current]=chars[j];
            }
        }
        
        String deleteallmutiplespace=String.valueOf(chars );
       String deleteallmutiplespace2=deleteallmutiplespace.substring(0,current);
       if(calsinglespacenum(deleteallmutiplespace2)==0)
       {
           return deleteallmutiplespace2;
       }
       else
       {
      String reversestr= reversetrueString(deleteallmutiplespace2);
      String finalstr="";
       char[] reversestrspac=reversestr.toCharArray();
       int [] intreversestrspac=calsinglespacenumways(reversestr);
       int judgefirstspace=0;
       for(int j=0, k=0;k<reversestrspac.length;j++,k++)
       {
            
           if(intreversestrspac[k]==1)
           {   
              
               String currentstr="";
               if(judgefirstspace==0)
               {
                   currentstr=""+reversetrueString(reversestr.substring(j,k));
                   finalstr=finalstr+currentstr;
                   j=k;
                   judgefirstspace++;
               }
               else
               {
               currentstr=" "+reversetrueString(reversestr.substring(j,k));
               
                finalstr=finalstr+currentstr;
                j=k;
                judgefirstspace++;
               }
           }else if(k==reversestrspac.length-1)
           {   
               
               String currentstr2=" "+reversetrueString(reversestr.substring(j,k+1));
               finalstr=finalstr+currentstr2;
               j=k;
           }
           
           else
           {
           j--;
           }
       }
      
        return finalstr;
       }
    }
    
    String reversetrueString(String truestring)
    {
        int strlength=truestring.length();
        char[] chars=truestring.toCharArray();
            char current='\0';
        for(int i=0;i<strlength/2;i++)
        {
        
            current=chars[i];
            chars[i]=chars[strlength-i-1];
            chars[strlength-i-1]=current;
        }
        String returnstr=String.valueOf(chars);
        return returnstr;
    }
    
    int[] calsinglespacenumways(String str)
    {
         char[] charssinglespacenum=str.toCharArray();
         int[] intsinglespacenum=new int[charssinglespacenum.length];
         for(int i=0;i<charssinglespacenum.length;i++)
         {
               if(charssinglespacenum[i]==' ')
               {
                   intsinglespacenum[i]=1;
               }
               else
               {
                   intsinglespacenum[i]=0;
               }
         }
         return intsinglespacenum;
    }
    
    int calsinglespacenum(String str)
    {
         char[] charssinglespacenum2=str.toCharArray();
         int intsinglespacenum=0;
         for(int i=0;i<charssinglespacenum2.length;i++)
         {
               if(charssinglespacenum2[i]==' ')
               {
                   intsinglespacenum++;
               }
               else
               {
                  intsinglespacenum=intsinglespacenum;
               }
         }
         return intsinglespacenum;
    }
    
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章