Anagrams: the result is right in compiler, but the submitting result is wrong. why?


  • 0
    A

    this my code:
    class Solution {
    public:
    vector<string> anagrams(vector<string> &strs) {
    vector<string>::iterator it;
    const vector<string>::iterator it_end = strs.end();
    vector<string> result;
    int * sign = new int[strs.size()];
    memset(sign, 0, sizeof(sign));
    map<string, int> sNum;
    int i;
    map<string, int>::iterator sNumIt;
    string temp;
    for(it = strs.begin(), i = 0; it != it_end; ++ it, ++ i)
    {
    temp = *it;
    sort(temp.begin(), temp.end());
    sNumIt = sNum.find(temp);
    if(sNumIt != sNum.end())
    {
    if(sign[sNumIt->second] != 1)
    {
    result.push_back(strs[sNumIt->second]);
    sign[sNumIt->second] = 1;
    }
    result.push_back(*it);
    sign[i] = 1;
    }
    else
    sNum.insert(pair<string, int> (temp, i));
    }
    delete sign;
    return result;
    }
    };
    this is my submit result:
    Input: ["tin","ram","zip","cry","pus","jon","zip","pyx"]
    Output: ["zip"]
    Expected: ["zip","zip"]


Log in to reply
 

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