《微软必应·豪杰会第三届在线编程大赛:几个bing?》挑战失败,由于程序运行总时间大于3s导致超时

   阅读
《微软必应·英雄会第三届在线编程大赛:几个bing?》挑战失败,由于程序运行总时间大于3s导致超时
刚把字符串加到5000左右,确实很慢...这是我的代码:各位大神能帮我简化下吗...
  public static  int howmany(String s)
    {
         int mark=0;
        int x = (int) (Math.pow(10, 9)+7);
    for (int i = 0; i <=s.lastIndexOf("b"); i++) {
         
            int b1 = s.indexOf("b",i);//b
            if(b1!=-1){//有b
                i=b1;//
                for(int j = b1+1;j<=s.lastIndexOf("i");j++){
                    int i1 = s.indexOf("i",j);//i
                    if(i1!=-1){//
                        j=i1;//
                        for(int m=i1+1;m<=s.lastIndexOf("n");m++){
                            int n1 = s.indexOf("n",m);//n
                            if(n1!=-1){//
                                m=n1;//
                                for(int n=n1+1;n<=s.lastIndexOf("g");n++){
                                    int g1 = s.indexOf("g",n);//g
                                    if(g1!=-1){
                                        n=g1;
                                        mark++;
                                    }
                                }
                            }
                            
                        }
                    }
                
                }
            }
        
            
        }
        return mark%x;
    }
------解决方案--------------------
这是我的代码,
O(n)
级别的,
http://bbs.csdn.net/topics/390685437
另外,把我里面用的int改成long型的,int会长度溢出
------解决方案--------------------
你这个算法复杂度是O(n3次方),能不慢吗,换个算法。
阅读
上一篇:servlet面试题解决方法 下一篇:返回列表