Problem:
https://leetcode.com/problems/verifying-an-alien-dictionary/
Solution:
class Solution {
public boolean isAlienSorted(String[] words, String order) {
int[] ord = new int[26];
for (int i=0; i<26; i++) {
ord[order.charAt(i)-'a'] = i;
}
search: for(int i=0; i<words.length-1; i++) {
String w1 = words[i];
String w2 = words[i+1];
int mLen = Math.min(w1.length(), w2.length());
for (int j=0; j<mLen; j++) {
if (w1.charAt(j) != w2.charAt(j)) {
if(ord[w1.charAt(j)-'a'] > ord[w2.charAt(j)-'a']) {
return false;
}
continue search; //此处的情况为,w1.charAt(j) < w2.charAt(j), 满足条件,可以直接跳出本次循环
// 知识点: continue [flag]的用法
}
}
// 检查过所有字符都相等后,如果长的字符串在前,则返回false
if (w1.length() > w2.length()) {
return false;
}
}
return true;
}
}
1711




被折叠的 条评论
为什么被折叠?



