[关闭]
@myecho 2019-09-17T11:14:16.000000Z 字数 1113 阅读 589

模板

LeetCode


大数加法:

string add(string & n, string & m){
    string res;
    int i=n.size()-1, j=m.size()-1, carry=0;
    while(i>=0 || j>=0) {
        int sum=carry+(i>=0 ? (n[i--]-'0') : 0) + (j>=0?  (m[j--]-'0') : 0);
        res.push_back(sum%10+'0');
        carry=sum/10;
    }
    if(carry) res.push_back(carry+'0');
    reverse(res.begin(), res.end());
    return res;
}

// 三路partition
for (int i = 0, j = 0, k = n - 1; j <= k;) {
    if (copy[j] < median) {
        swap(copy, i++, j++);
    } else if (copy[j] > median) {
        swap(copy, j, k--);
    } else {
        j++;
    }
}

class Solution {
    public int countSubstrings(String S) {
        int N = S.length(), ans = 0;
        for (int center = 0; center < 2*N-1; ++center) {
            int left = center / 2;
            int right = left + center % 2;
            while (left >= 0 && right < N && S.charAt(left) == S.charAt(right)) {
                ans++;
                left--;
                right++;
            }
        }
        return ans;
    }
}

for (size_t i = 0; i < s.size(); i++) {
    f[i][i] = true;
    for(size_t j=i-1; j >= 0; --j) { //[j,i]
        f[j][i] = (s[j] == s[i] && (i - j < 2 || f[j + 1][i - 1])); 
        if (f[j][i] && max_len < (i - j + 1)) {
            max_len = i - j + 1;
            start = j;
        }
    }
}

c++ indice排序

// Sort indices based on increasing 1. difficulty 2. profit
sort(indices.begin(), indices.end(), [&difficulty, &profit] (int i1, int i2) {
    return difficulty[i1] == difficulty[i2] ? profit[i1] < profit[i2] 
                                                    : difficulty[i1] < difficulty[i2];   
});

// 组合数计算 c(n, i)
c[0] = 1;
for(int i = 1; i <= n; ++i) c[i] = c[i-1] * (n-i+1)/i;
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注