1. Length of Last Word

  2. Longest Common Prefix

  3. count binary substring

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

  1. group continuous binary number + use array to store group number
  2. same but don't use an extra array

两个方法都是很相似的,唯一不同的就是第二个没有用另外的array。

如果s = "110001111000000", 那么 groups = [2, 3, 4, 6], 每个pair可以最多组成的数是min(groups[i], groups[i - 1]),只要加起来就可以了。

results matching ""

    No results matching ""