对一道【脉脉】上 头条 算法面试题的思考

栏目: 编程工具 · 发布时间: 6年前

内容简介:偶然间在脉脉上看到了一道头条的算法面试题按照题目的理解,简单的写了一个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>

得到了如下效果图

对一道【脉脉】上 头条 算法面试题的思考

对一道【脉脉】上 头条 算法面试题的思考

得到如题可以进行开关的示例

在最后一个灯特殊处理,链接第一个灯,形成环

经过测试发现

只要从序号0开始,如果打开则跳过,如果是灭灯,则点击i+1

得到如下效果

对一道【脉脉】上 头条 算法面试题的思考

敲黑板

现在得出的部分结论是

只有随机亮灭灯是一定比例的时候才有可能全部点亮

现在可行的比例为

亮-灭 50-50

亮-灭 51-49

亮-灭 47-53

亮-灭 44-56

亮-灭 42-58

亮-灭 53-47

亮-灭 54-46

而且,还决定于最后一个灯和相邻灯的亮灭

大家有什么好想法,可以留下见解讨论下


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

创意,未来的工作方式

创意,未来的工作方式

方军 / 中信出版社 / 2016-11-20 / 58.00元

知识工作者已成过去,创意工作者才是未来 工作的本质是创意 纵观我们身处的世界,除了自然美景,世间或伟大或平凡的事物,几乎都是人观念革新的产物,它们多数是我们在工作过程中群体创意的产物。 从工业时代到知识时代,大多数人通过掌握新知、持续学习,获得社会的认可和回报;但进入以大数据、人工智能、机器人为标志的新时代,单纯的学习已经不能满足社会对人的要求。算法和机器人正在取代人类很多重复性......一起来看看 《创意,未来的工作方式》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具