当前的一个项目想使用 Electron 做个 Demo 来进行测试,在调用 C++ 时遇到了一些坑,踩坑的过程中发现网上踩这些坑的人还不少,踩完坑在这里顺手记录一下。
x86 OR x64
lib 的位数与 node.js 的位数必须一致,而和操作系统的位数无关,当然,32位的操作系统是无法运行64位的应用程序 的。
ps: 如何查文件是32位还是64位
- 记得自己下载安装的是什么版本(废话)
- Windows 下:可以使用 vs 自带的 SDK Tools, 执行命令:
当前的一个项目想使用 Electron 做个 Demo 来进行测试,在调用 C++ 时遇到了一些坑,踩坑的过程中发现网上踩这些坑的人还不少,踩完坑在这里顺手记录一下。
lib 的位数与 node.js 的位数必须一致,而和操作系统的位数无关,当然,32位的操作系统是无法运行64位的应用程序 的。
1. 安装 node.js 参见这里
2. 安装 less for node.js
1 |
npm install -g less |
3. 安装 file watchers 插件:
File->Settings->Plugins->
4. 配置 File Wathcers:
File->Settings->Tools->
插件会自动寻找配置 lessc 。
至此,改动 less 并保存时会自动生成对应的 css文件
链表倒置是链表的基本操作之一。
LeetCode 206 Reverse Linked List
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Follow up:
A linked list can be reversed either iteratively or recursively. Could you implement both?
题目提示可以用 迭代 或 递归 两种方法来解。
head
p
分别指向 表头,欲倒置的元素. 为了使下一个欲倒置的元素不会 野 掉, 还需要一个指针 tmp
来保护它
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Solution { public: ListNode* reverseList(ListNode* head) { if(!head) return NULL; if(!head->next) return head; ListNode *p,*tmp; p = head->next; tmp = p->next; head->next = NULL; while(p1){ p->next = head; head = p; p = tmp; if(tmp) tmp = tmp->next; } return head; } }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Solution { public: ListNode* reverseList(ListNode* head) { if(!head || !head->next){ return head; } ListNode* p = reverseList(head->next); head->next->next = head; head->next = NULL; return p; } }; |
递归的方法代码简洁高效,在很多链表题目中都会用到它,所以特别重要。例如下一题
LeetCode 120. Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
For example, given the following triangle[
[2],
[3,4],
[6,5,7],
[4,1,8,3]
]The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
分析:
设三角形共有 $N$ 行, $r$ 为三角形的行, $c$ 为三角形的列。从点 $P(r,c)$ 出发,每向下走一步有两个点$P_1(r+1,c), P_2(r+1, c+1)$ 可以选择 ,如果每次都选值小的点$min(P1, P2)$,则最后得到的点的值之和即是最优解。令 $M(r,c)$ 为从 $P(r,c)$ 开始到下面的列的各条路径中,最佳路径的数字之和。
这是一个典型的递归问题。
$$M(r,c) = \begin{cases} P(r,c), & \text{if r = N }\\ min(M(r+1,c), M(r+1,c+1)) + P(r,c),& \text{others}\\ \end{cases}$$
由此写出代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Solution { public: int getMinPathSum(vector<vector<int>>& triangle, int r, int c){ if(r == triangle.size() - 1) return triangle[r][c]; int x = getMinPathSum(triangle, r+1, c); int y = getMinPathSum(triangle, r+1, c+1); x = x<y ? x: y; return triangle[r][c] + x; } int minimumTotal(vector<vector<int>>& triangle) { return getMinPathSum(triangle, 0,0); } }; |
代码没有问题,在 "Run Code" 时可以得出正确的结果。但是 "Submit" 却会给出 "Time Limit Exceeded",超时!
如果我们推算这个解法的时间复杂度的话,可以得到 $O(2^n)$ .这不超时就有鬼了。我们需要改进这个算法。