15
Độ phức tạp về thời gian đếm trong clojure là bao nhiêu?Độ phức tạp của hàm đếm trong clojure là bao nhiêu?
Độ phức tạp về thời gian đếm trong clojure là bao nhiêu?Độ phức tạp của hàm đếm trong clojure là bao nhiêu?
Đây là việc thực hiện:
public static int count(Object o){
if(o == null)
return 0;
else if(o instanceof Counted)
return ((Counted) o).count();
else if(o instanceof IPersistentCollection) {
ISeq s = seq(o);
o = null;
int i = 0;
for(; s != null; s = s.next()) {
if(s instanceof Counted)
return i + s.count();
i++;
}
return i;
}
else if(o instanceof String)
return ((String) o).length();
else if(o instanceof Collection)
return ((Collection) o).size();
else if(o instanceof Map)
return ((Map) o).size();
else if(o.getClass().isArray())
return Array.getLength(o);
throw new UnsupportedOperationException("count not supported on this type: " + o.getClass().getSimpleName());
}
Vì vậy, nó là O (1) cho mảng, chuỗi, bộ sưu tập, bản đồ và bất cứ điều gì mà thực hiện tính. Đó là O (n) cho bất kỳ thứ gì thực hiện IPersistentCollection, nhưng không được tính.