资讯专栏INFORMATION COLUMN

some demos

Mr_houzi / 2208人阅读

摘要:数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。求所有子数组的和的最大值,要求时间复杂度为。输出结果类数组转成数组直接遍历这种方法是借用了数组原型中的方法,返回一个数组方法从一个类似数组或可迭代对象中创建一个新的数组实例。

</>复制代码

  1. import "../css/detail.css";
  2. // 找到字符串中重复次数最多的字符
  3. function findMax(str) {
  4. let maxChar = "";
  5. let maxValue = 1;
  6. if (!str.length) return;
  7. let arr = str.replace(/s/g, "").split("");
  8. let obj = {};
  9. for (let i = 0; i < arr.length; i++) {
  10. if (!obj[arr[i]]) {
  11. obj[arr[i]] = 1;
  12. } else {
  13. obj[arr[i]]++;
  14. }
  15. }
  16. let keys = Object.keys(obj);
  17. for (let j = 0; j < keys.length; j++) {
  18. if (obj[keys[j]] > maxValue) {
  19. maxValue = obj[keys[j]];
  20. maxChar = keys[j];
  21. }
  22. }
  23. return {
  24. maxChar, maxValue
  25. }
  26. }
  27. function findMax1() {
  28. let maxChar = "";
  29. let maxValue = 1;
  30. let h = {};
  31. if (!str.length) return;
  32. let arr = str.replace(/s/g, "").split("");
  33. for (let i = 0; i < arr.length; i++) {
  34. let a = arr[i];
  35. h[a] === undefined ? h[a] = 1 : h[a]++;
  36. if (h[a] > maxValue) {
  37. maxChar = a;
  38. maxValue = h[a];
  39. }
  40. }
  41. return {
  42. maxChar, maxValue
  43. }
  44. }
  45. function findMax2() {
  46. let maxChar = "";
  47. let maxValue = 1;
  48. if (!str.length) return;
  49. let arr = str.replace(/s/g, "").split("");
  50. let obj = arr.reduce((acc, curVal) => {
  51. acc[curVal] ? acc[curVal]++ : acc[curVal] = 1;
  52. if (acc[curVal] > maxValue) {
  53. maxChar = curVal;
  54. maxValue = acc[curVal];
  55. }
  56. return acc;
  57. }, {})
  58. return {
  59. maxChar, maxValue
  60. }
  61. }

</>复制代码

  1. /*
  2. d 任意一个数字,0~9 中的任意一个
  3. w 任意一个字母或数字或下划线,也就是 A~Z,a~z,0~9,_ 中任意一个
  4. s 包括空格、制表符、换页符等空白字符的其中任意一个
  5. . 小数点可以匹配除了换行符(
  6. )以外的任意一个字符
  7. */
  8. function findMax3(str) {
  9. let maxChar = "";
  10. let maxValue = 1;
  11. if (!str.length) return;
  12. let arr = str.replace(/s/g, "").split("");
  13. let obj = {};
  14. str.replace(/s/g, "").replace(/(w)/g, (word, p) => {
  15. obj[p] ? obj[p]++ : obj[p] = 1;
  16. if (obj[p] > maxValue) {
  17. maxValue = obj[p];
  18. maxChar = p;
  19. }
  20. });
  21. return {
  22. maxChar, maxValue
  23. }
  24. }
  25. function findMax4(str) {
  26. let maxChar = "";
  27. let maxValue = 1;
  28. if (!str.length) return;
  29. let arr = str.replace(/s/g, "").split("");
  30. Array.prototype.getMost = function() {
  31. let obj = this.reduce((acc, cur) => {
  32. acc[cur] ? acc[cur]++ : acc[cur] = 1;
  33. acc.max = acc[cur] > acc.max ? acc[cur] : acc.max;
  34. acc.key = acc[cur] > acc.max ? cur : acc.key;
  35. return acc;
  36. }, {});
  37. return obj;
  38. }
  39. // return arr.getMost();
  40. }
  41. const str = "this is a test 222222 ts project. skajdf; 222sldjfwel p"

</>复制代码

  1. const reducer = (accumulator, currentValue, b, c) => {
  2. let obj = {};
  3. obj[b] = currentValue;
  4. return obj;
  5. }
  6. const array1 = [1, 2, 3, 4];
  7. // console.log(array1.reduce(reducer));
  8. console.log(findMax(str));
  9. console.log("findMax1", findMax4(str));

</>复制代码

  1. /**
  2. * 输入一个整形数组,数组里有正数也有负数。数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。 求所有子数组的和的最大值,要求时间复杂度为O(n)。
  3. */
  4. function findMaxSubArr(arr) {
  5. console.log(arr)
  6. let max = arr[0];
  7. let currSum = 0;
  8. for (let i = 0; i < arr.length; i++) {
  9. if (currSum < 0) {
  10. currSum = arr[i];
  11. } else {
  12. currSum += arr[i];
  13. }
  14. if (currSum > max) {
  15. max = currSum;
  16. }
  17. }
  18. return max;
  19. }
  20. // console.log("maxsubarr", findMaxSubArr([1, -2, 3, 10, -4, 7, 2, -5]));

</>复制代码

  1. // 输出结果
  2. console.log("begin");
  3. setTimeout(() => {
  4. console.log("settimeout 1")
  5. Promise.resolve().then( () => {
  6. console.log("promise 1")
  7. setTimeout(() => {
  8. console.log("settimeout 2");
  9. });
  10. }).then(() => {
  11. console.log("promise 2");
  12. })
  13. }, 0);
  14. console.log("end");

</>复制代码

  1. /**
  2. * 类数组转成数组
  3. */
  4. const nodes = document.querySelectorAll("div");
  5. // nodes.map(item => {});
  6. // 1.for直接遍历
  7. // 2.这种方法是借用了数组原型中的slice方法,返回一个数组
  8. Array.prototype.slice.call(nodes).map(item => {});
  9. [].slice.call(nodes).map(item => {console.log()});
  10. // 3.Array.from() 方法从一个类似数组或可迭代对象中创建一个新的数组实例。
  11. Array.from(nodes).map(item => {})
  12. // 4.同样是ES6中新增的内容,扩展运算符(…)也可以将某些数据结构转为数组
  13. const nodeList = [...nodes];
  14. function getlist(a,b,c,d) {
  15. // console.log(arguments);
  16. // console.log([...arguments][0])
  17. }
  18. getlist(11, 2, 3, 4);

</>复制代码

  1. // 使用reduce方法实现forEach、map、filter
  2. const arr1 = [12, 21, 3];
  3. const arr2 = arr1.map(function(item) {
  4. // console.log(this, item)
  5. return item*2
  6. }, { msg: "mapping" })
  7. // console.log(arr1, arr2)

</>复制代码

  1. // github上的
  2. Array.prototype.selfMap = function () {
  3. const ary = this
  4. const result = []
  5. const [ fn, thisArg ] = [].slice.call(arguments)
  6. if (typeof fn !== "function") {
  7. throw new TypeError(fn + "is not a function")
  8. }
  9. for (let i = 0; i < ary.length; i++) {
  10. result.push(fn.call(thisArg, ary[i], i, ary))
  11. }
  12. return result
  13. }
  14. Array.prototype.reduceMap = function (fn, thisArg) {
  15. // return (list) => {
  16. // 不怎么愿意写下面这两个判断条件
  17. const list = this
  18. if (typeof fn !== "function") {
  19. throw new TypeError(fn + "is not a function")
  20. }
  21. if (!Array.isArray(list)) {
  22. throw new TypeError("list must be a Array")
  23. }
  24. if (list.length === 0) return []
  25. return list.reduce((acc, value, index) => {
  26. return acc.concat([ fn.call(thisArg, value, index, list) ])
  27. }, [])
  28. // }
  29. }

</>复制代码

  1. // mine
  2. Array.prototype.imitateMap = function () {
  3. const list = this;
  4. const result = []
  5. const [fn, thisArg] = [].slice.call(arguments)
  6. if (typeof fn !== "function") {
  7. throw new TypeError(fn + "is not a function");
  8. }
  9. for (let i = 0; i < list.length; i++) {
  10. result.push(fn.call(thisArg, list[i], i, list));
  11. }
  12. return result;
  13. }
  14. Array.prototype.imitateReduceMap = function () {
  15. const list = this;
  16. const result = []
  17. const [fn, thisArg] = [].slice.call(arguments)
  18. if (typeof fn !== "function") {
  19. throw new TypeError(fn + "is not a function");
  20. }
  21. if (!Array.isArray(list)) {
  22. throw new TypeError(list + "is not a Array");
  23. }
  24. return list.reduce((acc, curValue, index) => {
  25. return acc.concat([fn.call(thisArg, curValue, index, list)]);
  26. }, [])
  27. }

</>复制代码

  1. const imitateReduceMap1 = function (fn, thisArg) {
  2. return (list) => {
  3. const result = []
  4. if (typeof fn !== "function") {
  5. throw new TypeError(fn + "is not a function");
  6. }
  7. if (!Array.isArray(list)) {
  8. throw new TypeError(list + "is not a Array");
  9. }
  10. return list.reduce((acc, curValue, index) => {
  11. return acc.concat([fn.call(thisArg, curValue, index, list)]);
  12. }, [])
  13. }
  14. }
  15. console.log("imitateMap", arr1, arr1.imitateMap(function(item) {
  16. console.log("imitateMap this", this)
  17. return item + 1
  18. }, { msg: "mapping" }) )
  19. console.log("imitateReduceMap", [ 1, 2, 3 ].imitateReduceMap(x => x + 1));
  20. console.log("imitateReduceMap1", imitateReduceMap1(x => x + 1)([ 1, 2, 3 ]));

</>复制代码

  1. /**
  2. * 实现repeat方法
  3. */
  4. function repeat(func, times, wait) {
  5. return (str) => {
  6. let count = 0;
  7. const timer = setInterval(() => {
  8. if (count < times) {
  9. // func.call(this, str);
  10. count++;
  11. } else {
  12. clearInterval(timer);
  13. }
  14. }, wait);
  15. }
  16. }
  17. const repeatFunc = repeat(alert, 3, 2000)
  18. repeatFunc("helloworld");

</>复制代码

  1. /**
  2. * 实现一个简单的双向绑定
  3. * 1.发布-订阅模式
  4. * 2.脏值检测
  5. * 3.数据劫持
  6. * Vue.js 采用的是 数据劫持+发布/订阅模式 的方式,通过 Object.defineProperty() 来劫持各个属性的 setter/getter, 在数据变动时发布消息给订阅者(Wacther), 触发相应的监听回调
  7. */
  8. // 基于Object.defineProperty 实现数据劫持,利用了对Vue.js实现双向绑定的思想
  9. const obj = {}
  10. Object.defineProperty(obj, "txt",{
  11. get:function(){
  12. return obj
  13. },
  14. set:function(newValue){
  15. document.getElementById("txt").value = newValue
  16. document.getElementById("show-txt").innerHTML = newValue
  17. }
  18. })
  19. document.addEventListener("keyup", function(e){
  20. obj.txt = e.target.value
  21. })

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/101889.html

相关文章

  • socket.io+express多房间聊天应用

    摘要:简介是一个开源的库,它通过实现服务端,同时也提供客户端库。支持以事件为基础的实时双向通讯,它可以工作在任何平台浏览器或移动设备。 socket.io简介 Socket.IO是一个开源的WebSocket库,它通过Node.js实现WebSocket服务端,同时也提供客户端JS库。Socket.IO支持以事件为基础的实时双向通讯,它可以工作在任何平台、浏览器或移动设备。Socket.IO...

    roadtogeek 评论0 收藏0
  • 使用 Flask-Docs 自动生成 Api 文档

    摘要:影响我写文档的原因可能是代码和文档分离,有时候写完代码会忘记补文档,而且不能及时查看,使用可以解决我的问题,这个插件可以根据代码注释生成文档页面,代码注释改动文档可以及时更新,而且支持离线文档下载。 影响我写文档的原因可能是代码和文档分离,有时候写完代码会忘记补文档,而且不能及时查看,使用 Flask-Docs 可以解决我的问题,这个插件可以根据代码注释生成文档页面,代码注释改动文档可...

    邹强 评论0 收藏0
  • js数组的方法

    摘要:向数组的末尾添加一个或多个元素,并返回新的长度。删除并返回数组的最后一个元素。遍历数组方法用于连接两个或多个数组。该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本。 arr.push() //向数组的末尾添加一个或多个元素,并返回新的长度。 arr.pop() //删除并返回数组的最后一个元素。 arr.unshift() //向数组的开头添加一个或更多元素,并返回新的...

    selfimpr 评论0 收藏0
  • js数组详解

    摘要:将对原来的数组进行反转,并返回改变后的数组,其会改变原数组的值。一个参数时返回该参数指定的位置到当前数组末尾的所有项。对数组的每一项运行给定的函数,没有返回值。测试有过滤筛选的含义,接收一个有返回值为弱的函数,最后返回一个过滤后的新数组。 数组初认识 Array是js中的引用数据类型,除了Object外,Array几乎是ECMAScript中最常用的数据类型了。 js中的数组与其他语言...

    dmlllll 评论0 收藏0
  • 深入理解行内元素的布局

    摘要:看上面的例子我们也能看出来,实际上一个内联元素是有两个高度的高度实际渲染的那个高度和高度实际区域占空间的高度也就是。 前言 总括: 本文通过实例讲解CSS中最大的难点之一,行内元素的布局,主要是挖掘line-height和vertical-align两个属性在布局方面的使用。 原文博客地址:深入理解行内元素的布局 知乎专栏&&简书专题:前端进击者(知乎)&&前端进击者(简书) 博...

    heartFollower 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<