least substring

https://www.lintcode.com/problem/least-substring/description?_from=ladder&&fromId=69

太久沒寫,有點卡住

這題就是要把想法改成程式碼而已,想法就是很簡單的兩個指針,一個是外面來偵測當前位置是否超過字串長度,裡面則是判斷是否數到 k 了。

我卡在內回圈不知道要做什麼

char[] c = s.toCharArray();
int index = 0;  //start from 0
int count = 0;  
while (index < s.length()) {
    int cnt = 1;
    // 這裏判斷下一個是否與當前相同,相同的話是否超過 k,
    // 因為可能做到結尾了所以要判斷一下是否到達字串長度
    while (cnt < k && index < s.length() && c[idnex] == c[index+1]) {
        // 沒有超過 k,下一個又與當前相同,則 cnt++ 進行下一個內迴圈比對
        cnt++;
        index++;
    }
    count++;
    index++;
}
return count;

results matching ""

    No results matching ""