我必须用树方法解决这种递归关系,因为Master定理不适用。
T(n)=(2 + 1 / log n)T(n / 2)
答案 0 :(得分:2)
经过一番思考,我无法想出一个确切的解决方案。师父的定理在这里不起作用,展开树并没有给我任何合理的理由。所以我将以下列方式估计复杂性。
对于任何相当大的private LayoutInflater inflater;
private Context context;
private HashMap<String, Object> temp;
private HashMap<String, Object> original;
private List<String> selectedItems = new ArrayList<>();
public SearchAdapter(Context context, int resource, HashMap<String, Object> temp) {
this.temp = temp;
this.context = context;
inflater = LayoutInflater.from(this.context);
}
@Override
public String getItem(int position) {
return ((List<String>) temp.get("name")).get(position).toString();
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public int getCount() {
return ((List<String>) temp.get("name")).size();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.custom_seach_item, null);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
String label = getItem(position);
viewHolder.txtTitle.setText("" + label);
viewHolder.eveIcon.setImageResource(((List<Integer>) temp.get("image")).get(position))
if (selectedItems.contains(getItem(position))) {
viewHolder.radioButton.setChecked(true);
} else {
viewHolder.radioButton.setChecked(false);
}
viewHolder.radioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!((List<Boolean>) temp.get("check")).get(position)) {
((List<Boolean>) temp.get("check")).remove(position);
((List<Boolean>) temp.get("check")).add(position, true);
selectedItems.add(((List<String>) temp.get("name")).get(position).toString());
Set<String> hs = new HashSet<>();
hs.addAll(selectedItems);
selectedItems.clear();
selectedItems.addAll(hs);
((RadioButton) view).setChecked(true);
} else {
((List<Boolean>) temp.get("check")).remove(position);
((List<Boolean>) temp.get("check")).add(position, false);
selectedItems.remove(((List<String>) temp.get("name")).get(position).toString());
((RadioButton) view).setChecked(false);
}
notifyDataSetChanged();
}
});
viewHolder.txtTitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!((List<Boolean>) temp.get("check")).get(position)) {
((List<Boolean>) temp.get("check")).remove(position);
((List<Boolean>) temp.get("check")).add(position, true);
selectedItems.add(((List<String>) temp.get("name")).get(position).toString());
Set<String> hs = new HashSet<>();
hs.addAll(selectedItems);
selectedItems.clear();
selectedItems.addAll(hs);
//((RadioButton) view).setChecked(true);
} else {
((List<Boolean>) temp.get("check")).remove(position);
((List<Boolean>) temp.get("check")).add(position, false);
selectedItems.remove(((List<String>) temp.get("name")).get(position).toString());
// ((RadioButton) view).setChecked(false);
}
notifyDataSetChanged();
}
});
return convertView;
}
public List<String> getSelectedItems() {
return selectedItems;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
temp = (HashMap<String, Object>) results.values; // has the filtered values
notifyDataSetChanged(); // notifies the data with new filtered values
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults(); // Holds the results of a filtering operation in values
HashMap<String, Object> FilteredArrList = new HashMap<String, Object>();
if (original == null) {
original = new HashMap<String, Object>(temp); // saves the original data in mOriginalValues
}
/********
*
* If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values
* else does the Filtering and returns FilteredArrList(Filtered)
*
********/
if (constraint == null || constraint.length() == 0) {
// set the Original result to return
results.count = ((List<String>) original.get("name")).size();
results.values = original;
} else {
constraint = constraint.toString().toLowerCase();
List<String> name = new ArrayList<>();
List<Integer> image = new ArrayList<>();
List<Boolean> check = new ArrayList<>();
for (int i = 0; i < ((List<String>) original.get("name")).size(); i++) {
String data = ((List<String>) original.get("name")).get(i);
if (data.toLowerCase().contains(constraint.toString())) {
name.add(((List<String>) original.get("name")).get(i));
image.add(((List<Integer>) original.get("image")).get(i));
check.add(((List<Boolean>) original.get("check")).get(i));
}
}
FilteredArrList.put("name", name);
FilteredArrList.put("image", image);
FilteredArrList.put("check", check);
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
return results;
}
};
return filter;
}
@Override
public void onClick(View view) {
}
private class ViewHolder {
TextView txtTitle;
ImageView eveIcon;
RadioButton radioButton;
public ViewHolder(View convertView) {
txtTitle = (TextView) convertView.findViewById(R.id.search_txt);
eveIcon = (ImageView) convertView.findViewById(R.id.search_img);
radioButton = (RadioButton) convertView.findViewById(R.id.search_radio);
}
}
,您都可以估算n
。所以你可以得到:
0 < 1/log n < 1
和T1(n) = 2 * T1(n/2)
T2(n) = 3 * T2(n/2)
。您可以使用master theorem找到两次重复的复杂性。 O(T1) < O(T) < O(T2)
T1
和O(n)
的复杂程度为T2
。
因此,您可以确定重复的复杂性大于O(n^log2(3))
且小于O(n)
,因此小于二次。