對一道【脈脈】上 頭條 算法面試題的思考

偶然間在脈脈上看到了一道頭條的算法面試題css

clipboard.png

按照題目的理解,簡單的寫了一個html網頁html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>pool</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
    <div id="vue_det">
        <div @click="SmartChange(0)" style="width:40px;height:40px;background:#fff">開始</div>
        <div class="pool" v-for="(item, index) in list">
            <div @click="change(index)" v-if="item" style="width:40px;height:40px;background:#999;float:left">{{index}}</div>
            <div @click="change(index)" v-if="!item" style="width:40px;height:40px;background:#fff;float:left">{{index}}</div>
        </div>
    </div>
 
    <script src="https://cdn.bootcss.com/vue/2.6.4/vue.js"></script>
    <script>
        var vm = new Vue({
                el: '#vue_det',
                data: {
                    list: [],
                    i: 0
                },
                methods: {
                    details: function () {
                        return this.site + " - 學的不只是技術,更是夢想!";
                    },
                    change (index) {
                        // console.log(index)
                        if (index === 99) {
                            this.list[0] = !this.list[0]
                            this.list[98] = !this.list[98]
                            this.list[99] = !this.list[99]
                        } else if (index === 0) {
                            this.list[0] = !this.list[0]
                            this.list[1] = !this.list[1]
                            this.list[99] = !this.list[99]
                        } else {
                            // console.log('222')
                            this.list[index] = !this.list[index]
                            this.list[index - 1] = !this.list[index - 1]
                            this.list[index + 1] = !this.list[index + 1]
                        }
                        // console.log(this.list)
                        this.list = JSON.parse(JSON.stringify(this.list))
                    },
                    SmartChange (index) {
                      if (this.i === 99) {
                          return false
                      }
                      if (this.list[this.i] === true) {
                          this.i = this.i + 1
                          setTimeout(() => {
                            this.SmartChange()  
                          }, 10);
                      } else {
                          this.change(this.i + 1)
                          setTimeout(() => {
                              this.SmartChange()
                          }, 10);
                      }
                    //   console.log('222222222',this.i)
                    },
                    go () {
                        for (let index = 1; index < 101; index++) {
                            this.list.push(Math.random() > 0.5 ? true : false)
                        }
                        console.log(this.list)
                        let a = 0, b = 0
                        this.list.map(val => {
                            if (val) {
                                a++
                            } else {
                                b++
                            }
                        })
                        console.log(a, b)
                    }
                },
                created () {
                    this.go()
                }
            })
    </script>
</body>
</html>

獲得了以下效果圖vue

clipboard.png

圖片描述

獲得如題能夠進行開關的示例
在最後一個燈特殊處理,連接第一個燈,造成環

通過測試發現
只要從序號0開始,若是打開則跳過,若是是滅燈,則點擊i+1
獲得以下效果面試

圖片描述

敲黑板

如今得出的部分結論是
只有隨機亮滅燈是必定比例的時候纔有可能所有點亮算法

如今可行的比例爲
亮-滅 50-50
亮-滅 51-49
亮-滅 47-53
亮-滅 44-56
亮-滅 42-58
亮-滅 53-47
亮-滅 54-46dom

並且,還決定於最後一個燈和相鄰燈的亮滅測試

你們有什麼好想法,能夠留下看法討論下
相關文章
相關標籤/搜索