(ハッシュテーブル、スライディングウィンドウ、スタック)646 lintcode

 

 

 

 

 

 

クラスのソリューション{
 パブリック/ * * 
     * @param S:文字列
     * @return:それのインデックス
     * / 
    int型 firstUniqChar(文字列S){
         // ここにコードを記述する 
        ベクトル< int型 > CNT(2560 );
        以下のためにint型 i = 0 ; iがs.sizeを()<; iは++ 
            のCNT [S [I]] ++ ; 
        
        以下のためにint型 i = 0 ; iはs.sizeを()<; iは++ ){
             場合(CNTの[S [I]] ==1 を返す私を。
        } 
        リターン - 1 
    } 
}。

 

 

 

 

 

 

 必要十分条件は、AとBは互いにアナグラムである:各番号Bにおける文字の発生数=文字の各出現

 

 

 

クラスのソリューション{
 パブリック/ * * 
     * @param S:文字列
     * @paramのP:文字列
     * @return:インデックスのリスト
     * / 
     // スライディングウィンドウ+ハッシュ 
    ベクトル< int型 > findAnagrams(文字列&S、文字列P ){
         // ここにコードを記述
         // 返回所有同构串的首地址 
        ベクトル< int型 > ANS; 
        ベクター < INT > CNT(2560)、CNP(2560 ); 
        
        もし(s.size()< P.SIZE())
             戻りANS; 
            
        のためにint型 I = 0を、私はP.SIZE()を<;私は++ ){
             // カウント各文字の数が表示されます 
            CNT [秒[I]] ++ ; 
            CNP [P [I] ++ ; 
        } 
        
        IF(CNT == CNP)
            ans.push_back(0 ); 
            
        // ウィンドウをスライドさせる
        ためのint型 I = P.SIZE();私は(s.sizeを<) ; I ++ ){ 
            CNT [S [I]] ++;   // 右増加 
            CNTを[S [ip.size()]] - ;    // 左減少
            場合(CNT == CNP)
                ans.push_back(I -p.size()+ 1 )。
        } 
        
        戻りANS。
    } 
}。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

class ValidWordAbbr {
public:
    /*
    * @param dictionary: a list of words
    */
    unordered_map<string, int> abbr;
    unordered_map<string, int> dict;
    
    ValidWordAbbr(vector<string> dictionary) {
        // do intialization if necessary
        for(int i=0; i<dictionary.size(); i++){
            string w = dictionary[i];
            dict[w]++;   //统计字符串的个数
            
            //abbr dictionary 统计每个字符串缩写的个数
            if(w.size() <= 2)
                abbr[w]++;   
            else
                abbr[w.front()+to_string(w.size()-2)+w.back()]++;
        }
    }

    /*
     * @param word: a string
     * @return: true if its abbreviation is unique or false
     */
    bool isUnique(string &word) {
        // write your code here
        if(word.size()<=2)
            return abbr[word]==dict[word];
        else
            return abbr[word.front()+to_string(word.size()-2)+word.back()]==dict[word];
    }
};

/**
 * Your ValidWordAbbr object will be instantiated and called as such:
 * ValidWordAbbr obj = new ValidWordAbbr(dictionary);
 * bool param = obj.isUnique(word);
 */

 

 

 

 要求O(n)的时间复杂度。

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        unordered_set<int> map(nums.begin(), nums.end());
        int ans = 0;
        for(int i=0; i<nums.size(); i++){
            if(map.find(nums[i]) != map.end()){
                map.erase(nums[i]);
                int pre = nums[i]-1;
                int next = nums[i]+1;
                while(map.find(pre) != map.end()){
                    map.erase(pre);
                    pre--;
                }
                while(map.find(next) != map.end()){
                    map.erase(next);
                    next++;
                }
                ans = max(ans, next-pre-1);
            }
        }
        return ans;
    }
};

 

 

 

 

 

 

 解法:栈

 

 

 首先在栈中加入空串,这样可以将b和c情况合为一种情况,如:()) 这样会判断为 ‘ ’ 和 ) 不匹配,返回false

 

class Solution {
public:
    /**
     * @param s: A string
     * @return: whether the string is a valid parentheses
     */
    bool isValidParentheses(string &s) {
        // write your code here
        //
        vector<char> stack;
        stack.push_back(' ');  //压入空字符,作为dummy 0
        for(char w : s){
            if(w == '(' || w == '[' || w == '{')
                stack.push_back(w);
            else if(w == ')'){
                if(stack.back() != '(')
                    return false;
                else
                    stack.pop_back();
            }
            else if(w == ']'){
                if(stack.back() !='[')
                    return false;
                else
                    stack.pop_back();
            }
            else if(w == '}'){
                if(stack.back() !='{')
                    return false;
                else
                    stack.pop_back();
            }
        }
        if(stack.back() != ' ')
            return false;
        return true;
    }
};

 

 

 

 

 

 

 

 

 

 

class LoadBalancer {
public:
    int p;
    vector<int> array;
    unordered_map<int, int> mp;
    
    LoadBalancer() {
        // do intialization if necessary
        p = 0;
        srand((unsigned)time(NULL));
    }

    /*
     * @param server_id: add a new server to the cluster
     * @return: nothing
     */
    void add(int server_id) {
        // write your code here
        if(mp.find(server_id) == mp.end()){
            //mp中没找到对应元素,插入
            array.push_back(server_id);
            mp[server_id] = p;
            p++;
        }
    }

    /*
     * @param server_id: server_id remove a bad server from the cluster
     * @return: nothing
     */
    void remove(int server_id) {
        // write your code here
        if(mp.find(server_id) != mp.end()){
            mp[array[p-1]] = mp[server_id];  //将mp里array最后一个元素,对应的位置更新为mp中key为server_id对应的位置
            array[mp[server_id]] = array[p-1];   //将array中server_id的位置用array的最后一个元素替换
            array.pop_back();    //删掉array的最后一个元素
            mp.erase(server_id);  //删掉mp中的 server_id
            p--;
        }
    }

    /*
     * @return: pick a server in the cluster randomly with equal probability
     */
    int pick() {
        // write your code here
        return array[rand()%p];
    }
};

 

おすすめ

転載: www.cnblogs.com/Bella2017/p/11448405.html