anagram 问题
妹的,不明白anagram什么吊意思,后来明白了,转译成中文就是要满足下面几个要求:
1) 将某个单词里面的字母顺序重新排列后得到的单词与原来的单词满足anagram(翻译成回文?反正没见过)
2) 重新排列后得到的单词与原单词必须满足长度相等,因为原单词有可能有相同的字母,解题的时候这点要注意,下面是我写的C++代码,用到了STL的map
class Solution { public: vector<string> anagrams(vector<string> &strs) { // Start typing your C/C++ solution below // DO NOT write int main() function map<string,vector<int> > hash; map<string,vector<int> >::iterator itr; vector<string> tmp(strs); vector<string> revalue; vector<string>::iterator it; for(it=tmp.begin() ; it!=tmp.end() ; it++) sort((*it).begin(),(*it).end()); int i=0; int count; for(it=tmp.begin() ; it!=tmp.end() ; it++,i++) hash[*it].push_back(i); for( itr=hash.begin() ; itr!=hash.end() ; itr++){ if((count=(itr->second).size())>1){ for(int j=0 ; j<count ; j++) revalue.push_back(strs[(itr->second)[j]]); } } return revalue; } };优质内容筛选与推荐>>