Daily Archives: April 8, 2015


[leetcode] Maximum Subarray

Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. click to show more practice. More practice:If you have figured out the O(n) solution, try […]


[leetcode] Permutations II

Permutations II Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. 跟PermutionsI类似,不过需要去重。 思路是先对原数组排序,然后在循环开始时,跳过重复元素。 class Solution { public: vector<vector<int> > permuteUnique(vector<int> &num) { vector<vector<int>> re; vector<int> thisRe; sort(num.begin(), num.end()); DFS(num, re, thisRe); return re; […]


[leetcode] Permutations

Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. 深搜DFS。注意参数需要传引用。 class Solution { public: vector<vector<int> > permute(vector<int> &num) { vector<vector<int>> re; vector<int> thisRe; DFS(num, re, thisRe); return re; } void DFS(vector<int> &num, vector<vector<int>> &re, vector<int> […]


如何用PHP进行异步处理

项目中遇到这样的问题: 浏览器访问某一页面,服务器相应地查询数据库,生成json给前端,并且在数据库中更新该页面的浏览量。 查看日志发现,“更新数据库中该页面的浏览量”字段非常耗时,整个过程400ms,它占了一半。 想到我们没必要在等数据库更新完毕后再给用户返回数据,这两步完全可以异步执行。考虑PHP的异步操作。 PHP异步操作的解决方案有很多: 1. http://www.360doc.com/content/11/0123/17/397482_88527668.shtml Gearman的php扩展,通过建立一个一直运行的守护进程(Deamon)来实现异步处理。 2. http://www.dewen.io/q/3970/ 使用fscokopen,或消息队列。 其实它的本质是用多线程甚至是多进程来解决异步问题。 待补充