bool isAlienSorted(vector<string> &words, string order) {
int map[26];
for (int i = 0; i < 26; i++)
map[order[i] - 'a'] = i;
for (string &w : words)
for (char &c : w)
c = map[c - 'a']+'a';//“hello” 转换成正常的排序
return is_sorted(words.begin(), words.end());
}
思想:
将 char &c : order 作为索引,用整数进行标记。
for (int i = 0; i < 26; i++)
map[order[i] - 'a'] = i;// 将order里面的顺序用数字做标记,从0开始
for (string &w : words)
for (char &c : w)
c = map[c - 'a']+'a';//“hello” 转换成正常ASCII码的排序
将单词顺序转变成正常的ACSII码顺序