算法——交错01串

题目:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>交错01串</title>
        <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
        <style>
            input{
                width: 300px;
                height: 30px;
                border: 1px cadetblue solid;
                border-radius: 10px;
            }
            button{
                width: 100px;
                height: 30px;
                color: aliceblue;
                border: 1px cadetblue solid;
                border-radius: 5px;
                background: cornflowerblue;
            }
            h2{
                color: darkslategray;
                font-family: "楷体";
            }
            
        </style>
    </head>
    <body>
        
            <h2>交错01串</h2>
            <input type="text" onkeyup="value=value.replace(/[^\[01]$]/g,'')"/>
            <button>判断</button>
        
        <script>
            var re = /[^01]/;
            
            var a = [],b = [];
            var j = 1;
                
                    $('button').on('click',function  () {
                        if ($('input').val().length < 1 || $('input').val().length >= 50) {
                            alert("长度必须在1-50之间")
                        } else{
                                if (re.test($('input').val())) {
                                    alert("只能输入0,1")
                                } else{
                                    a = $('input').val().split('');
                                    for (var i=0 ;i<a.length-1;i++) {
                                        if (a[i] == a[i+1]) {
                                            b.push(j);
                                            j=1;
                                        } else{
                                            j++;
                                        }
                                    }
                                    b.push(j);
                                    alert(Math.max.apply(null, b));
                                    j=1;
                                    b=[];
                                }
                                }
                            }) 
                    
        </script>
    </body>
</html>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容