这是我第一次听说简单功能的复杂性让我很困惑。 我是对的吗?
这是我的类,我需要为每个函数编写复杂性,但我认为它都是O(1)。
public class PolyNode
{
private int _power;
private double _coefficient;
private PolyNode _next;
public PolyNode (int power, double coefficient)
{
if (power < 0)
{
_power = 0;
_coefficient = 0;
_next = null;
}
else
{
_power = power;
_coefficient = coefficient;
_next = null;
}
}
public PolyNode(int power, double coefficient,PolyNode next)
{
if (power < 0)
{
_power = 0;
_coefficient = 0;
_next = next._next;
}
else
{
_power = power;
_coefficient = coefficient;
_next = next._next;
}
}
public PolyNode(PolyNode p)
{
_power = p._power;
_coefficient = p._coefficient;
_next = p._next;
}
public int getPower()
{
return _power;
}
public double getCoefficient()
{
return _coefficient;
}
public PolyNode getNext()
{
return _next;
}
public void setCoefficient (double coefficient)
{
_coefficient = coefficient;
}
public void setNext(PolyNode next)
{
_next = next;
}
public String toString()
{
String st = "";
if (_coefficient == 0)
return "";
else if (_power == 0)
{
st += _coefficient;
return st;
}
else if (_power == 1 && _coefficient != 1 && _coefficient != -1)
return _coefficient + "x";
else if (_coefficient == 1 || _coefficient == -1)
{
if (_coefficient == 1 && _power != 0)
{
if (_power == 1)
return "x";
else
return "x^" + _power;
}
else if (_coefficient == 1 && _power == 0)
return "1";
else if (_coefficient == -1 && _power != 0)
return "-x^" + _power;
else if (_coefficient == -1 && _power == 0)
return "-1";
}
return _coefficient + "x^" +_power;
}
}
答案 0 :(得分:3)
由于你没有任何循环,我猜复杂性是O(1)。如果您有这样的for循环:
for(int i=0;i<n;i++){}
将是O(N),并且:
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
将是O(n * n)