没通过的代码:
class Solution { public: /* * @param A: An integer array * @param k: A positive integer (k <= length(A)) * @param target: An integer * @return: An integer */ int kSum(vector<int> &A, int k, int target) { // write your code here int length = A.size(); vector<vector<vector< int> > > result(length+1,vector<vector<int > >(k+1,vetor<int >(target+1))); for(int i = 0;i <= length;i++) result[i][0][0] = 1; for(int i = 1;i <= length;i++){ for(int j = 1;j <= k;j++){ for(int p = 1;p <= target;p++){ if((p - A[i]) >= 0) result[i][j][p] = result[i-1][j-1][p-A[i]] + result[i-1][j-1][p]; else result[i][j][p] = result[i-1][j-1][p]; } } } return result[length][k][target]; } };
三维vector初始化:http://blog.csdn.net/u013630349/article/details/47777645