• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            jake1036

            面試100 13第一個只出現一次的字符

                             第一個只出現一次的字符

              方法1 :
                        第一個只出現一次的字符。
                       (1)考慮使用一個hash表,將各個字符映射到表中,然后表中存儲有該字符出現的次數,以及首次出現的下標。
                       (2)映射完成之后,掃描hash數組查找出現次數為1的字符,并且其首次出現下標為最小。

                缺點:
                      需要存儲首次出現的下標,造成存儲位置浪費。
                      
            #include <iostream>
            #include 
            <vector>
            #include 
            <iterator>
              
            using namespace std ;
             
              
            struct Value{
                 
            int times ;
                 
            int index ;        
              }
             ;
              
            const int N = 26 ;
              
            const int MAX = 66536 ;
              
            int hashstr(char * s , Value * list )
              
            {
                   
            int i = 0 ;
                   
            int index = MAX ;
                   
                  
            if(s == 0
                    
            return index;
                    
                  
            while(*!= '\0')
                    
            {
                      list[
            *s-'a'].times++ ;
                      
            if(list[*s-'a'].index == -1//若是首次出現下標,則統計 
                        list[*s-'a'].index = i ;           
                        i
            ++;
                        s
            ++;
                    }

                      
                   
                   
            for(i = 0 ;i < N ; i++)
                   
            {
                     
            if(list[i].times == 1)
                        
            {
                          
            if(index > list[i].index)   //求最小的下標       
                           index = list[i].index ;                     
                        }
                   
                   }

                   
            return index ;
              }

             
              
            int main()
              
            {
                
            int i ;
              
                
            char s[] = "wabacckdeffbzw" ; 
                Value  list[N] ;
               
                 
            for(i = 0;i < N ;i++)
                 
            {
                   list[i].times 
            = 0 ;
                   list[i].index 
            = -1 ;                     
                 }
             
               
                
            int index = hashstr(s , list) ;
                
            if(index == MAX)
                   cout
            <<"error"<<endl ;
                 
            else
                   cout
            <<s[index]<<endl ;
                    
                system(
            "pause") ;
                
            return 0 ;    
              }

              



              方法2 :

                建立一個長度為256的hash數組,掃描到對應的字符,即更新hash表內存儲出現的次 數。
             需要兩次掃描字符串,第一次掃描統計字符串的出現次數,第二次掃描確定出現一次的字符及其位置 ,比方法1,比較省空間。 
             
              代碼如下:
               
            #include <iostream>
             
            using namespace std ;
             
            const int N = 256 ;
             
             
            void hashstr(char * s , int * a)
             
            {
                
            char * p = s ;   
                
            while(*p)
                
            {
                  a[
            *p]++ ; //自增       
                  p++ ;         
                }
              
                
                
            while(*s) //掃描第二遍,當掃描都出現次數為1的字符,即停止 
                {
                  
            if(a[*s] == 1)
                  
            {
                    cout
            <<*s ;
                    
            break
                  }

                  s
            ++ ;         
                }

                
                  
             }

             
             
            int main()
             
            {
               
            char s[] = "wabacckdeffbz" ;
               
            int  a[N] ;
               memset(a , 
            0 , sizeof(a)) ;
               hashstr(s , a) ;
               system(
            "pause") ; 
               
            return 0 ;    
             }





            posted on 2011-05-17 10:25 kahn 閱讀(540) 評論(0)  編輯 收藏 引用 所屬分類: 算法相關

            久久精品国产2020| 国产精品99久久久久久人| 久久久国产一区二区三区| 亚洲国产成人久久一区久久| 无码精品久久久天天影视| 97久久久久人妻精品专区 | 久久青青草原精品国产不卡| 亚洲精品美女久久久久99小说 | 国内精品久久久久久不卡影院| 色婷婷久久综合中文久久一本| 久久精品aⅴ无码中文字字幕不卡| 久久精品成人| 久久综合给合久久狠狠狠97色| 久久乐国产精品亚洲综合| 99久久精品毛片免费播放| 精品国产乱码久久久久软件| 91精品国产高清久久久久久91 | 日韩精品国产自在久久现线拍| 久久受www免费人成_看片中文| 狠狠色丁香久久综合五月| 亚洲日本va中文字幕久久| 香蕉久久影院| 精品国产综合区久久久久久| 久久国产成人精品麻豆| 亚洲国产精品无码成人片久久| 久久久噜噜噜久久| 精品国产综合区久久久久久| 国产成人精品久久亚洲| 久久亚洲高清观看| 久久国产精品国产自线拍免费| 久久综合综合久久综合| 一本色道久久综合亚洲精品| 热久久视久久精品18| 亚洲欧美另类日本久久国产真实乱对白| 久久99精品国产麻豆婷婷| 久久精品国产亚洲av瑜伽| 国产一区二区三精品久久久无广告| 久久国产精品成人免费 | 久久婷婷五月综合国产尤物app | 久久亚洲AV成人无码电影| 青青草原精品99久久精品66 |