06 2017 档案

摘要:思路: 先翻转,然后相加。(需要多了解一些操作字符串的函数,能方便好多)。 class Solution { public: string addBinary(string a, string b) { string res = ""; int carry = 0; int valA; int va 阅读全文
posted @ 2017-06-17 10:11 UniMilky 阅读(102) 评论(0) 推荐(0)
摘要:思路: 细节题,应该不断和面试官交流确定一些特殊情况。 阅读全文
posted @ 2017-06-17 09:26 UniMilky 阅读(102) 评论(0) 推荐(0)
摘要:思路: 暴力: class Solution { public: int strStr(string haystack, string needle) { if(needle.length() == 0) return 0; bool res = true; int len1 = haystack. 阅读全文
posted @ 2017-06-16 09:31 UniMilky 阅读(115) 评论(0) 推荐(0)
摘要:思路: 开始想的是先把特殊符号去掉,但是超时了。事实上直接在遍历中处理就行了。 "参考" class Solution { public: bool isPalindrome(string s) { int left = 0, right = s.length() 1; while(left 阅读全文
posted @ 2017-06-16 08:42 UniMilky 阅读(98) 评论(0) 推荐(0)
摘要:思路: 栈的应用。如果是'(','{','[',则只需入栈即可,如果是'}',']',')',则要看栈顶的字符是否和其匹配。 class Solution { public: bool isValid(string s) { stack res; for(int i = 0; i 阅读全文
posted @ 2017-06-16 07:16 UniMilky 阅读(95) 评论(0) 推荐(0)
摘要:思路: 递归,利用level表示第几层。 迭代,利用队列,一层一层进行遍历。 阅读全文
posted @ 2017-06-15 16:34 UniMilky 阅读(107) 评论(0) 推荐(0)
摘要:思路: 递归。 利用栈进行遍历。对于中序遍历,首先遍历左子树, 然后是根节点,最后才是右子树,所以我们需要用stack记录每次遍历的根节点, 当左子树遍历完成之后,从stack弹出根节点,得到其右子树,开始新的遍历。(参考的一个文档,链接找不到了。。) 直接遍历,以后写。。 阅读全文
posted @ 2017-06-15 14:57 UniMilky 阅读(82) 评论(0) 推荐(0)
摘要:思路: 递归 利用栈进行遍历。 直接遍历,以后写。。 阅读全文
posted @ 2017-06-15 11:06 UniMilky 阅读(106) 评论(0) 推荐(0)
摘要:思路 递归 阅读全文
posted @ 2017-06-15 10:42 UniMilky 阅读(109) 评论(0) 推荐(0)
摘要:思路: 递归。 / Definition for a binary tree node. struct TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) : val(x), left(NULL), right(NUL 阅读全文
posted @ 2017-06-15 09:06 UniMilky 阅读(131) 评论(0) 推荐(0)
摘要:思路: 和Subsets类似,因为数组中有重复元素,所以用set存储结果,相当于正常遍历然后去掉了重复。 class Solution { public: vector subsetsWithDup(vector& nums) { sort(begin(nums),end(nums)); vecto 阅读全文
posted @ 2017-06-14 14:06 UniMilky 阅读(80) 评论(0) 推荐(0)
摘要:思路: dfs,每个元素有两种选择,选或者不选。 class Solution { public: vector subsets(vector& nums) { sort(begin(nums),end(nums)); vector res; vector tmp; dfs(nums,0,res,t 阅读全文
posted @ 2017-06-14 09:34 UniMilky 阅读(135) 评论(0) 推荐(0)
摘要:思路: 从后向前赋值,这样就可以避免移动数组元素。 "参考" 阅读全文
posted @ 2017-06-14 08:19 UniMilky 阅读(124) 评论(0) 推荐(0)
摘要:思路: 两个指针,比Remove Duplicates from Sorted Array多了一个count,记录是否超过两次。直接贴代码。 class Solution { public: int removeDuplicates(vector& nums) { if(nums.size() 阅读全文
posted @ 2017-06-13 22:10 UniMilky 阅读(102) 评论(0) 推荐(0)
摘要:思路: dfs,重点写在注释里面。这道题做了好久,以后不做无意义的尝试,想好了再改,调试。 阅读全文
posted @ 2017-06-13 10:14 UniMilky 阅读(143) 评论(0) 推荐(0)
摘要:思路: 设置两个指针red,blue,遇到0就交换到前面,red++,i++,遇到blue就交换到后面,因为red前面已经遍历过,所以交换red时需要i++,但是交换blue只需要blue++。(描述的好乱啊,还是贴上分析的好的博客吧。。) "参考" class Solution { public: 阅读全文
posted @ 2017-06-12 09:52 UniMilky 阅读(178) 评论(0) 推荐(0)
摘要:思路: 将二维数组看成一个一维数组,数组长度为m n,对于位置x,它映射到matrix数组里面的位置为i/n,i%n,然后利用二分查找即可。 阅读全文
posted @ 2017-06-11 19:29 UniMilky 阅读(100) 评论(0) 推荐(0)
摘要:思路: 设置两个bool数组row[matrix[0].size()],col[matrix.size()]分别记录行和列中的0,如果matrix[i]\[j]为0,那么将row[j]和col[i]赋值为0。先遍历数组matrix,更新bool数组,然后遍历bool数组,更新matrix。(也可以将 阅读全文
posted @ 2017-06-11 19:08 UniMilky 阅读(180) 评论(0) 推荐(0)
摘要:思路: 直接模拟加法 class Solution { public: vector plusOne(vector& digits) { int lead = 0; int in = 1; for(int i = digits.size() 1; i = 0; i ){ if(digits[i] + 阅读全文
posted @ 2017-06-11 08:22 UniMilky 阅读(166) 评论(0) 推荐(0)
摘要:64.Minimum Path Sum 思路: DFS + 备忘录,和Unique Path类似。 "参考文章" class Solution { public: int minPathSum(vector & grid) { int m = grid.size(); int n = grid[0] 阅读全文
posted @ 2017-06-10 23:14 UniMilky 阅读(207) 评论(0) 推荐(0)
摘要:思路: dfs,超时 DP。 "参考文章" class Solution { public: int uniquePathsWithObstacles(vector & obstacleGrid) { int m = obstacleGrid.size(); int n = obstacleGrid 阅读全文
posted @ 2017-06-10 14:47 UniMilky 阅读(121) 评论(0) 推荐(0)
摘要:思路: dfs,超时 dfs + 备忘录即可过大数据集。 "参考文章" 组合数学方法。$m /times n的网格,从$(0,0)$走到$(m 1,n 1)$共需要$m+n 2$步,其中 $m 1$ 步向下,$n 1$ 步向右。如果把向下走记为 $0$ ,向右走记为 $1$ ,相当于$m 1$个 $ 阅读全文
posted @ 2017-06-10 13:33 UniMilky 阅读(132) 评论(0) 推荐(0)
摘要:思路 直接模拟程序运行,设置$rowbegin,rowend,colbegin,colend$变量。注意:要判断是否遍历过。 "参考" dfs:待实现 阅读全文
posted @ 2017-06-09 12:03 UniMilky 阅读(159) 评论(0) 推荐(0)
摘要:思路 类似Spiral Matrix,直接模拟 dfs:等做几道dfs后再来实现 阅读全文
posted @ 2017-06-09 12:02 UniMilky 阅读(127) 评论(0) 推荐(0)
摘要:思路: dfs,超时 class Solution { public: bool canJump(vector& nums) { return dfs(nums,0); } bool dfs(vector& nums,int i){ if(i == nums.size() 1) return tru 阅读全文
posted @ 2017-06-09 10:41 UniMilky 阅读(537) 评论(0) 推荐(0)
摘要:上网时看到的,虽然方向不同,但是有些建议很中肯。 链接: http://blog.sina.com.cn/s/blog_50c1545101008spw.html梁大伟 序 我刚刚开始做实验的时候,别人怎么说我就怎么做,每天在实验台旁干到深 夜,以为这就是科研了。两个月过去,突然发现自己还在原地踏步 阅读全文
posted @ 2017-06-08 18:42 UniMilky 阅读(676) 评论(0) 推荐(0)
摘要:思路: DP,设置数组res,存储以i结尾的连续数组和的最大值。如果i之前子数组最大和小于等于0,那么res[i] = nums[i],否则res[i] = nums[i] + res[i 1]。复杂度为$O(n)$。 阅读全文
posted @ 2017-06-08 09:55 UniMilky 阅读(117) 评论(0) 推荐(0)
摘要:思路: 先转置,然后对称。复杂度为$O(n^2)$。每次用swap交换,额外占用一个空间,所以空间复杂度为$O(1)$。 class Solution { public: void rotate(vector & matrix) { for(int i = 0; i 阅读全文
posted @ 2017-06-08 09:19 UniMilky 阅读(143) 评论(0) 推荐(0)
摘要:思路: 类似Combination Sum,但是数组有重复元素,同时要求元素不能重复使用。复杂度和Combination Sum类似。 阅读全文
posted @ 2017-06-08 08:53 UniMilky 阅读(115) 评论(0) 推荐(0)
摘要:思路: 利用回溯,其运行过程可以看成遍历一颗4叉树(因为要求不重复,所以中间剪掉了部分树枝),复杂度为$O(n^h)$,$h$为树的高度,这里$h$相当于只用数组里最小的数加起来大于等于target时所用的数目。比如[2,3,6,7],target = 7。2+2+2+2 7,所以h为4。 clas 阅读全文
posted @ 2017-06-07 23:14 UniMilky 阅读(122) 评论(0) 推荐(0)
摘要:思路: 直接遍历数组。复杂度为$O(n)$。 利用二分查找,注意最后利用mid找插入位置。复杂度为$O(log(n))$。 阅读全文
posted @ 2017-06-07 22:28 UniMilky 阅读(154) 评论(0) 推荐(0)
摘要:思路: 二分查找,找到后分别向左向右遍历有多少重复的,然后记录左右端点即可,但是这样在某些情况下复杂度为$O(n)$,比如{8,8,8,8,8,8,8,8,8}。竟然过了。。。 总结: 1. 每次二分要判断是否找到 2. 测试程序要考虑到[],[a]两种特殊情况 阅读全文
posted @ 2017-06-07 15:56 UniMilky 阅读(121) 评论(0) 推荐(0)
摘要:思路: 参考 http://blog.csdn.net/abcjennifer/article/details/40152323, 复杂度为 $O(nlog(n))$ class Solution { public: void nextPermutation(vector& nums) { int 阅读全文
posted @ 2017-06-07 09:17 UniMilky 阅读(142) 评论(0) 推荐(0)
摘要:思路: 复杂度 $O(n)$。 class Solution { public: int removeElement(vector& nums, int val) { int res,k=0; for(int i = 0; i 阅读全文
posted @ 2017-06-07 08:32 UniMilky 阅读(154) 评论(0) 推荐(0)
摘要:思路: 记录每个元素前面有几个重复的数字,但是写的时候感觉容易出错,还是第二种好一点。复杂度$O(n)$。 class Solution { public: int removeDuplicates(vector& nums) { int i = 0,k = 0; if(nums.size() & 阅读全文
posted @ 2017-06-06 22:59 UniMilky 阅读(238) 评论(0) 推荐(0)
摘要:思路: 类似3Sum,利用map存储可以省去判断重复数组,复杂度为 $O(n^4)$,因为set判断元素是否重复也需要 $O(n)$. class Solution { public: vector fourSum(vector& nums, int target) { sort(nums.begi 阅读全文
posted @ 2017-06-06 21:20 UniMilky 阅读(153) 评论(0) 推荐(0)
摘要:思路: 暴力,复杂度为 $O(n^3)$,超时 类似3Sum。这几道题相对暴力能够减小复杂度的主要原因是暴力里面的两层循环都执行了,而后面的这种解法通过比较使内部的两层循环减少到了一层,所以时间复杂度由 $O(n^3)$ 减小到了 $O(n^2)$。 阅读全文
posted @ 2017-06-06 20:50 UniMilky 阅读(195) 评论(0) 推荐(0)
摘要:思路 直接暴力,$O(n^4 log(n))$,不出意外的超时了。。 class Solution { public: vector threeSum(vector& nums) { vector res; int i,j,k; int len = nums.size(); for(i = 0; i 阅读全文
posted @ 2017-06-06 15:03 UniMilky 阅读(181) 评论(0) 推荐(0)
摘要:思路: 首先假设选择两侧,如果想让体积变大,应该让小的一侧移动,因为想要使小的一侧遇到比原来大的高度。每次都移动小的,直到左右两侧相遇,记录移动过程中产生的最大值。复杂度为$O(n)$ 阅读全文
posted @ 2017-06-05 22:52 UniMilky 阅读(124) 评论(0) 推荐(0)
摘要:两种方法: 直接遍历,复杂度为 $O(n^2)$ class Solution { public: vector twoSum(vector& nums, int target) { vector a; for(int i = 0; i twoSum(vector& nums, int target 阅读全文
posted @ 2017-06-05 22:40 UniMilky 阅读(120) 评论(0) 推荐(0)