因此,在给定gridView.setAdapter(customAdopter);
customAdopter.setItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(int position) {
Intent intent = new Intent(MainActivity.this, SwipeView.class);
intent.putExtra("id", position);
startActivity(intent);
}
});
列表和vector<unsigned>
(这是找到子集总和的总和)时,我很难返回vector<unsigned> universal
。
我尝试实现的给教授的伪代码是:
unsigned target
要弄清代码,请执行以下操作:subset_sum_dp(U[0...n-1], t):
T = new 2D array with n+1 rows and t+1 columns, each element initialized
to None
for i from 0 through n:
T[i][0] = []
for i from 1 through n:
x = U[i-1]
for j from 0 through t:
if T[i-1][j] is not None:
T[i][j] = T[i-1][j]
else if T[i-1][j - x] is not None:
T[i][j] = T[i-1][j - x] + [x]
else:
T[i][j] = None
return T[n][t]
正在串联列表。
我认为我最困惑的问题是将向量设置为空并将向量设置为T[i-1][j-x] + [x]
。在None
和T[i][0] = []
行中,因为在将向量设置为T[i][j] = None
时不断出现错误。
给出的给定骨架代码为:
NULL
到目前为止,我所实施的使我有所进步的是:
#pragma once
#include <optional>
#include <vector>
namespace subsetsum {
using natural_vector = std::vector<unsigned>;
// Solve the subset sum problem: return subset of universe that adds
// up to exactly target. If no such subset exists, return an empty optional.
// In the special case that target is zero, return an empty vector.
// universe must not be empty.
// This uses a dynamic programming search algorithm that takes pseudopolynomial
// time.
std::optional<natural_vector>
subset_sum_dp(const natural_vector& universe, unsigned target) {
assert(!universe.empty());
// TODO: Rewrite the body of this function so that it actually works. That
// includes rewriting the return statement. After you do that, delete this
// comment.
return std::nullopt;
}
}