03 2018 档案
摘要:问题: 字符串s="ABBCABCDABDADSBC",p="ABCDABD",问p在s中第一次出现的索引,未找到则返回-1 思路: 暴力求解:时间复杂度O(m*n),其中m、n分别为s、p的长度 KMP算法:时间复杂度O(m+n) 利用next[],存储字符串p中的前后缀相同的长度。 即next[
阅读全文
摘要:思路: 维持一个数组arr[i][j],表示前i个物品中的若干个,放入体积为j的背包中的最大价值。 每次放入第i个物品,就更新这个数组 动态规划递推关系式: if (j < vol[i]) //当前物品的体积比当前背包体积大,放不进背包 arr[i][j] = arr[i - 1][j]; else
阅读全文
摘要:https://leetcode.com/problems/can-i-win/ 思路: 1. 用checked的二进制形式表示第i个数被选取 2. 对每个数字进行dfs,dfs每次返回当前checked状态下是否能win 3. dfs会产生重复的结果,对每次dfs的结果用c[checked]记录
阅读全文
摘要:要求把n*n的矩阵顺时针旋转90° 空间复杂度为O(n) 思路: 1. 每次找4个元素,确定通用坐标 2. 3次交换操作就可以旋转这4个元素 3. 逐层缩小遍历范围
阅读全文
摘要:Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and inorder traversal sequences, you are supposed to out
阅读全文
摘要:In the big cities, the subway systems always look so complex to the visitors. To give you some sense, the following figure shows the map of Beijing su
阅读全文
摘要:There is a kind of balanced binary search tree named red-black tree in the data structure. It has the following 5 properties: (1) Every node is either
阅读全文
摘要:Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle in the early years. When a boy A had a crush on a g
阅读全文
摘要:题意是这样的:有N个方块,可以被涂成K种颜色。输入N*K矩阵,costs[n][k]代表第n个方块,涂成第k种颜色的花费。要求每相邻两个方块的颜色不同。求最小花费。 思路:没找到动态规划的更好方法,只好用递归来穷举了
阅读全文

浙公网安备 33010602011771号