2. Longest Common Prefix
find the shortest word + compare each letter in each word
先找到最短的单词,以那个为标准。比较在最短单词里的字母,和每一个单词的对应位置的字母。
for (int c = 0; c < shortest.length(); c++) {
char ch = s.charAt(c);
for (int i = 0; i < array.length; i++) {
if (ch == array[i].charAt(c)) {//}
}
}
3. Count Binary Substring
- group continuous binary number + use array to store group number
- same but don't use an extra array
两个方法都是很相似的,唯一不同的就是第二个没有用另外的array。
如果s = "110001111000000", 那么 groups = [2, 3, 4, 6], 每个pair可以最多组成的数是min(groups[i], groups[i - 1]),只要加起来就可以了。