I got a compiler error when I use a compare function, how to fix that?


  • 0
    V
    bool arrcmp(int *arr1, int *arr2){
    	int cmp=memcmp(arr1, arr2, 26);
    	return cmp<0;
    }
    
    vector<string> anagrams(vector<string> &strs){
    	vector<string> res;
    	vector<int*> freq;
    	for(int i=0;i<strs.size();i++){
    		int *arr = new int[27];  
         //26 int to count the num of lower_case, the last to store the index
    		memset(arr, 0, sizeof(int)*27);
    		arr[26]=i;
    		for(int j=0;j<strs[i].size();j++){
    			arr[strs[i][j]-'a']++;
    		}
    		freq.push_back(arr);
    	}
    	sort(freq.begin(), freq.end(), arrcmp);
    	for(int i=1;i<freq.size();i++){
    		//use memcmp to compare the int array
    		int cmp=memcmp(freq[i-1], freq[i],sizeof(int)*26);
    		if(cmp==0){
    			res.push_back(strs[freq[i-1][26]]);
    			res.push_back(strs[freq[i][26]]);	//store the string
    		}
    	}
    	//release new memory
    	for(int i=0;i<freq.size();i++){
    		delete [] freq[i];
    	}
    	return res;
    }

  • 1
    S

    Define cmp like
    static bool arrcmp(int *arr1, int *arr2).


Log in to reply
 

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