leetcode 문자열 한 번만 나타나는 첫 번째 문자

그만큼

문제

해결책

암호



/*
思路: 这种带次数的数组和字符串问题最好的方法就是采用hashtable去解决,如果条件限制比较多的话,就可以结合双指针去解决。

我们看这个问题发现其只是统计出现一次的字符, 一个hash+ 加逻辑判断就可以解决。 注意map 是有序的, 按照序列大小去排序了。 我们要用hash。

-  define hashtable , make all data into hash  table.
-   loop to search what value is equal 1  return 
*/


class Solution {
    
    
public:
   int FirstNotRepeatingChar(string str) {
    
    
       unordered_map<char,int> need;
       for(int i = 0; i < str.size(); i++){
    
    
           
           need[str[i]]++;
         
       }
       for(char c: str){
    
    
           
           if(need[c]==1){
    
    
               
               for(int i= 0;i<str.size();i++){
    
    
                   
                   if(str[i]==c) return i;
               }
           }
       }
       return -1;
       
   }
};










요약 및 반성

  1. 이 질문을 통해 해시와 맵의 순회를 이해합니다. 맵의 순서는 키에 따라 정렬되므로 직접 순회 순서는 삽입 순서와 다릅니다. 순서를 유지하려면 키의 크기를 확인해야만 찾을 수 있습니다. 그 자체로는 원래 주문이 저장되지 않습니다.

추천

출처blog.csdn.net/liupeng19970119/article/details/114178295