有人可以帮我理解下面代码的时间复杂度。该程序用于将所有零移位到数组的右侧。
class TestClass {
public static void main(String args[] ) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String []s = br.readLine().split(" ");
int a[] = new int[s.length];
for(int i=0;i<s.length;i++)
a[i]=Integer.parseInt(s[i]);
int j= a.length-1;
int i=0;
while(j>=0 && i<a.length-1 && j-i>0){
if(a[i]==0){
while(a[j]==0)
j--;
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
i++;
}
for(int k:a)
System.out.print(k+" ");
}
}
答案 0 :(得分:0)
如果我们尝试包含Integer class
,parseInt()
,while loop
方法在O(s.length^2)+O((s.length)*O(F(q))
内运行,并且取决于通过String,它会给出不同的时间复杂度,但是如果我们假设它将需要O(F(q)),那么代码的时间复杂度将为>>> URL = 'http://www.professorpaddle.com/rivers/riverdetails.asp?riverid=350'
>>> from lxml import html
>>> import requests
>>> page = requests.get(URL)
>>> tree = html.fromstring(page.content)
>>> tableRows = tree.xpath('..//table[@class="tableBorder" or @class="tableborder"][2]/tr')
>>> len(tableRows)
2
>>> for row in tableRows:
... for child in row.iterchildren():
... if child.text:
... child.text.strip()
...
'Pinned Forum Threads'
''
''