public class ZigZag
{
public void maxZigZag(int[] zigzag)
{
int sign[]=new int[zigzag.length];
int p=0;
int max=1;
for(int j=0;j<zigzag.length-1;j++)
{
if(zigzag[j+1]-zigzag[j] > 0)
sign[p++]=1;
else if(zigzag[j+1]-zigzag[j] < 0)
sign[p++]=-1;
}
/*for(int s:sign)
{
System.out.print(s+",");
}*/
System.out.println();
for(int k=0;k<p;k++)
{
if(sign[k]==sign[k+1])
{
sign[k]=0;
}
}
/*for(int s:sign)
{
System.out.print(s+",");
}*/
System.out.println();
for(int s=0;s<p;s++)
{
if(sign[s]!=0)
{
max++;
/*System.out.print(sign[s]+",");*/
}
}
System.out.println("max length zigzag:"+max);
}
public static void main(String args[])
{
ZigZag z=new ZigZag();
int zigzag[]={374, 40, 854, 203, 203, 156, 362, 279, 812, 955,
600, 947, 978, 46, 100, 953, 670, 862, 568, 188,
67, 669, 810, 704, 52, 861, 49, 640, 370, 908,
477, 245, 413, 109, 659, 401, 483, 308, 609, 120,
249, 22, 176, 279, 23, 22, 617, 462, 459, 244};
z.maxZigZag(zigzag);
}
}
想要的運行時間是多少? –
你究竟想要什麼?如果你只想要一個數組,只需使用一個長度爲2n的數組,其中0-n是zag而n-2n是zig? – sukunrt