Cattle customer network _ wins the title set offer - a character first appeared only (java achieve)

Topic links:

https://www.nowcoder.com/practice/1c82e8cf713b4bbeb2a5b31cf5b0417c?tpId=13&tqId=11187&rp=2&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

 

Idea: using the ASCII code mapping, the number of times each character appears and the location of storage into the last occurrence of two-dimensional array, and then re-iterate two-dimensional array, and identify the number 1 position appear minimal, return to this location is ok, complex of O (n)

 

Implementation source code;

Package Penalty for niuke; 

/ ** 
 * az: 97-122 
 * AZ: 65-90 
 * / 
public  class first appears only one character {
     public  int FirstNotRepeatingChar (String str) {
         IF (str.length () == 0 | | STR == null ) return 0 ;
         int [] [] = statistic new new  int [53 is] [2 ];
         // Mapping the ASCII, Times First IS, IS SECOND LOCATION 
        char [] TEMP = str.toCharArray ();
         for ( int I = 0; I <temp.length; ++ I) {
             IF (TEMP [I]> = 97) {
                int trans = temp[i] - 96;
                statistic[trans][0] += 1;
                statistic[trans][1] = i;
            }else{
                int trans = temp[i] - 38;
                statistic[trans][0] += 1;
                statistic[trans][1] = i;
            }
        }//statistic is over
        int min = 54;
//        char res = '#';
        for(int i = 1;i<53;++i){
            if(statistic[i][0]==1&&statistic[i][1]<min){
                min = statistic[i][1];
//                res = (char)i;
            }
        }
        if(min==54)
            return -1;
        else
            return  min;
    }
}

 

Code is AC

I hope to be helpful

the above

Guess you like

Origin www.cnblogs.com/lavender-pansy/p/12484956.html
Recommended