浏览 166 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
|
发表时间:2023-12-18
学习地址2:https://pan.baidu.com/s/1schn_jcgf03syblfyankca 提取码:c862 前端高手特训 从0到1带你手写一个微信小程序底层框架,今天就带着大家从0到深入学习小程序框架底层原理,无论你是一位新手,还是一位有经验的开发者,能够自研一套小程序底层框架,都是你突破技术瓶颈有效途径。 今天就由我带领大家从架构设计 ,原理剖析,再到源码的实现,一步步地实战构建一个完整的微信小程序底层框架,让你深度掌握小程序双线程原理,助力你具备把握最佳机会的能力和提升获取心仪offer的成功率,成为一个真正有实力的技术人才 。 小程序使用的是exparser组件模型,exparser组件模型与web components中的shadow dom高度相似,微信为什么使用自定义组件框架,而不使用web components呢?主要还是出于安全考虑,并且方便管控。既然exparser组件框架与shadow dom高度相似,那么我们首先来了解一下shadow dom。 本题比较特殊,我们要删除目前所在的节点,而没有它的前趋节点。题目已经告诉我们删除的不是尾节点,我们可以拷贝它的后继节点内容到它本身的位置。这样链表里有两个内容相同的节点——即它的后继节点出现了两次,我们把它的后继节点删除即可。 /** * definition for singly-linked list. * public class listnode { * int val; * listnode next; * listnode(int x) { val = x; } * } */ class solution { public void deletenode(listnode node) { node.val = node.next.val; node.next = node.next.next; } } } 思路分析:滑动窗口——和我们之前讲的链表找环问题类似。快指针每次走两步,慢指针每次走一步,如此快指针移动到链表末尾的时候,慢指针刚好移动到中间。注意细节,链表长度的奇偶性,以及如何判断末尾等。 代码 /** * definition for singly-linked list. * public class listnode { * int val; * listnode next; * listnode() {} * listnode(int val) { this.val = val; } * listnode(int val, listnode next) { this.val = val; this.next = next; } * } */ class solution { public listnode middlenode(listnode head) { for (listnode temp = head; temp != null && temp.next != null; temp = temp.next.next, head = head.next) ; return head; } } 思路分析: 可以在push时进行反向操作。 class mystack { private queue private int getind() { return q[0].isempty() ? 1 : 0; } /** initialize your data structure here. */ public mystack() { q = new queue[2]; for (int i = 0; i < 2; i) { q[i] = new linkedlist<>(); } } /** push element x onto stack. */ public void push(int x) { q[getind()].add(x); } /** removes the element on top of the stack and returns that element. */ public int pop() { final int ind = getind(); for (; q[ind].size() > 1; q[ind ^ 1].add(q[ind].poll())) ; return q[ind].poll(); } /** get the top element. */ public int top() { int r = pop(); push(r); return r; } /** returns whether the stack is empty. */ public boolean empty() { return q[0].isempty() && q[1].isempty(); } } /** * your mystack object will be instantiated and called as such: * mystack obj = new mystack(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.top(); * boolean param_4 = obj.empty(); */ 思路分析:把0和n加到端点里,得到数组c,排序后任意一段木棍可以用c中两个元素表示。定义dp[i][j]表示从端点c[i]到c[j]的木棍都切好后的最小代价。 dp[i][j] = 0 对于j <= i 1 dp[i][j] = min(dp[i][k] dp[k][j]) c[j] - c[i] i < k < j 枚举切点k,分别切两段。 class solution { public int mincost(int n, int[] cuts) { int[] c = new int[cuts.length 2]; c[1] = n; system.arraycopy(cuts, 0, c, 2, cuts.length); arrays.sort(c); final int m = c.length; int[][] dp = new int[m][m]; for (int d = 2; d < m; d) { for (int i = 0; i d < m; i) { final int j = i d; dp[i][j] = integer.max_value; for (int k = i 1; k < j; k) { dp[i][j] = math.min(dp[i][j], dp[i][k] dp[k][j]); } dp[i][j] = c[j] - c[i]; } } return dp[0][m - 1]; } } 思路分析:设dp[i][j]表示从s的长度为i的前缀中删掉不超过k个字符最终得到的最短压缩串长度。 dp[0][j] = 0, 空串。 dp[i][j] = min(dp[i - 1][j - 1], dp[p][j - x] (y个s.charat(i - 1)的压缩长度)) 其中p < i, 设c = s.charat(i - 1), x是s.charat(p…i)中非c出现的次数,要删掉它们, 而y是其中c出现的次数,要全部保留。实际上我们枚举了目前压缩串最后一部分中删掉c和保留y个c的全部可能性。 class solution { private int getlen(int x) { if (x == 1) { return 1; } if (x < 10) { return 2; } if (x < 100) { return 3; } return 4; } public int getlengthofoptimalcompression(string s, int k) { final int n = s.length(); int[][] dp = new int[n 1][k 1]; for (int i = 1; i <= n; i) { final char c = s.charat(i - 1); for (int j = 0; j <= k; j) { dp[i][j] = j > 0 ? dp[i - 1][j - 1] : integer.max_value; for (int p = i - 1, num = 0, t = j; p >= 0; --p) { if (s.charat(p) == c) { num; } else if (--t < 0) { break; } dp[i][j] = math.min(dp[i][j], getlen(num) dp[p][t]); } } } return dp[n][k]; } } 思路分析: 方法1 经典算法 dp[i]表示以nums[i]结尾的最长单调子序列长度。 dp[i] = max(dp[j]) 1 (0 <= j < i, nums[j] < nums[i]) 解是max(dp[i]) (0 <= i < nums.length) 代码: class solution { public int lengthoflis(int[] nums) { final int n = nums.length; int[] dp = new int[n]; int r = 0; for (int i = 0; i < n; i) { for (int j = 0; j < i; j) { if (nums[j] < nums[i]) { dp[i] = math.max(dp[i], dp[j]); } } r = math.max(r, dp[i]); } return r; } } 结语 介绍到这里,小程序的底层框架原理基本已经介绍完了,想跟大家分享的是,小程序确实和h5非常类似,其实它相当于一个借助了native强大功能的加强版h5,小程序并不神秘,除了微信小程序之外,现在各大超级app都已经推出了自己的小程序,原理应该都大差不差。 声明:iteye文章欧洲杯足彩官网的版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|