当前位置:   article > 正文

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

js88591013

偶然间在脉脉上看到了一道头条的算法面试题

clipboard.png

按照题目的理解,简单的写了一个html网页

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <title>pool</title>
  7. <meta name="viewport" content="width=device-width, initial-scale=1">
  8. </head>
  9. <body>
  10. <div id="vue_det">
  11. <div @click="SmartChange(0)" style="width:40px;height:40px;background:#fff">开始</div>
  12. <div class="pool" v-for="(item, index) in list">
  13. <div @click="change(index)" v-if="item" style="width:40px;height:40px;background:#999;float:left">{{index}}</div>
  14. <div @click="change(index)" v-if="!item" style="width:40px;height:40px;background:#fff;float:left">{{index}}</div>
  15. </div>
  16. </div>
  17. <script src="https://cdn.bootcss.com/vue/2.6.4/vue.js"></script>
  18. <script>
  19. var vm = new Vue({
  20. el: '#vue_det',
  21. data: {
  22. list: [],
  23. i: 0
  24. },
  25. methods: {
  26. details: function () {
  27. return this.site + " - 学的不仅是技术,更是梦想!";
  28. },
  29. change (index) {
  30. // console.log(index)
  31. if (index === 99) {
  32. this.list[0] = !this.list[0]
  33. this.list[98] = !this.list[98]
  34. this.list[99] = !this.list[99]
  35. } else if (index === 0) {
  36. this.list[0] = !this.list[0]
  37. this.list[1] = !this.list[1]
  38. this.list[99] = !this.list[99]
  39. } else {
  40. // console.log('222')
  41. this.list[index] = !this.list[index]
  42. this.list[index - 1] = !this.list[index - 1]
  43. this.list[index + 1] = !this.list[index + 1]
  44. }
  45. // console.log(this.list)
  46. this.list = JSON.parse(JSON.stringify(this.list))
  47. },
  48. SmartChange (index) {
  49. if (this.i === 99) {
  50. return false
  51. }
  52. if (this.list[this.i] === true) {
  53. this.i = this.i + 1
  54. setTimeout(() => {
  55. this.SmartChange()
  56. }, 10);
  57. } else {
  58. this.change(this.i + 1)
  59. setTimeout(() => {
  60. this.SmartChange()
  61. }, 10);
  62. }
  63. // console.log('222222222',this.i)
  64. },
  65. go () {
  66. for (let index = 1; index < 101; index++) {
  67. this.list.push(Math.random() > 0.5 ? true : false)
  68. }
  69. console.log(this.list)
  70. let a = 0, b = 0
  71. this.list.map(val => {
  72. if (val) {
  73. a++
  74. } else {
  75. b++
  76. }
  77. })
  78. console.log(a, b)
  79. }
  80. },
  81. created () {
  82. this.go()
  83. }
  84. })
  85. </script>
  86. </body>
  87. </html>

得到了如下效果图

clipboard.png

图片描述

得到如题可以进行开关的示例
在最后一个灯特殊处理,链接第一个灯,形成环

经过测试发现
只要从序号0开始,如果打开则跳过,如果是灭灯,则点击i+1
得到如下效果

图片描述

敲黑板

现在得出的部分结论是
只有随机亮灭灯是一定比例的时候才有可能全部点亮

现在可行的比例为
亮-灭 50-50
亮-灭 51-49
亮-灭 47-53
亮-灭 44-56
亮-灭 42-58
亮-灭 53-47
亮-灭 54-46

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

大家有什么好想法,可以留下见解讨论下
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/940373
推荐阅读
相关标签
  

闽ICP备14008679号