Why Runtime Error for Input ""


  • 0
    A

    When the input is "", it is runtime error. It is so strange.

    class Solution {
        static const int M = 1311071;
        int hash[M];
        int cnt[M];
        int insert(int x) {
            int idx = x%M;
            if (hash[idx] == -1) {
                hash[idx] = x;
                cnt[idx]++;
            }
            else {
                while(hash[idx] != -1) idx= (idx+1)%M;
                hash[idx] = x;
                cnt[idx]++;
            }
        }
        int query(int x) {
            int idx = x%M;
            if (hash[idx] == x) {
                return cnt[idx];
            }
            while(hash[idx] != -1) {
                if (hash[idx] == x) return true;
                idx= (idx+1)%M;
            }
            return 0;
        }
        int charToBit(char ch) {
            if (ch == 'A') return 0;
            if (ch == 'C') return 1;
            if (ch == 'G') return 2;
            if (ch == 'T') return 3;
        }
    public:
        vector<string> findRepeatedDnaSequences(string s) {
            vector<string> vtr;
            if (s == "") return vtr;
            int len = s.length();
            if (len < 11) return vtr;
            memset(hash, -1, sizeof(hash));
            memset(cnt, 0, sizeof(cnt));
            int x = 0;
            for (int i = 10-1; i >= 0; i--) {
                x = x*4 + charToBit(s[i]);
            }
            insert(x);
            int top = 1 << 18;
            for (int i = 10; i < len; i++) {
                int bit = charToBit(s[i]);
                x = x/4+top*bit;
                int count = query(x);
                if (count == 1) {
                    vtr.push_back(s.substr(i-10+1, 10));
                }
                else {
                    insert(x);
                }
            }
            return vtr;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.