随笔分类 - leetcode
摘要:问题: 求给定数组中,连续k个数的最大平均值。 Input: [1,12,-5,-6,50,3], k = 4 Output: 12.75 Explanation: Maximum average is (12-5-6+50)/4 = 51/4 = 12.75 Note: 1 <= k <= n <
阅读全文
posted @ 2020-03-15 12:18
habibah_chang
摘要:问题:求一个数列里三个数相乘的最大值 Input: [1,2,3] Output: 6 Input: [1,2,3,4] Output: 24 Note: 1.The length of the given array will be in range [3,104] and all element
阅读全文
posted @ 2020-03-15 12:08
habibah_chang
摘要:问题: 种花问题,在给出的花圃中(可能已经种植了一些花),如果存在空位与其他花相隔1个空位,则可在此处种植。 求:给出的花圃中,能否种植完给定数量的花。 Input: flowerbed = [1,0,0,0,1], n = 1 Output: True Input: flowerbed = [1,
阅读全文
posted @ 2020-03-15 11:45
habibah_chang
摘要:问题: 求数列中最小的未排序子数列(即,如果将此子数列排序后,整个数列则成为有序数列) Input: [2, 6, 4, 8, 10, 9, 15] Output: 5 Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order
阅读全文
posted @ 2020-03-08 13:51
habibah_chang
摘要:Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Input: [2,-5,-2,-4,3] Output: 24 Explanation: [-2,-4,3] has the largest prod
阅读全文
posted @ 2020-03-07 11:53
habibah_chang
摘要:题目: 从1~9个数中,取k个数,使其和为n,求所有的组合 例子: Input: k = 3, n = 9 Output: [[1,2,6], [1,3,5], [2,3,4]] 方法: keyword:栈,递归 1. 对于1~9的每一个数, 用掉一个,则在剩下的数里面寻找满足的数。 2.结束条件:
阅读全文
posted @ 2020-03-01 16:25
habibah_chang
摘要:题目:含有从1~n(数组size)数值的数组,里面存在重复元素,求缺掉的数字。 要求:不使用多余辅助空间,时间复杂度为(n)的算法 方法: 利用 原数组index 一一对应 所求连续数列的数值 的特点, 顺次遍历数组的同时,遇到某个值,使用正负号来标记已遇到。 最后在遍历一次,没遇到过的(即所求缺掉
阅读全文
posted @ 2020-03-01 15:20
habibah_chang
摘要:题目:未排序的数组从0~n,依次增大的数列中缺少了一个数,是哪个? 方法一:高斯算法 等差数列求和,0~size()+1个数列的和 - 实际给定的数列的和 = 要求数字 参考代码: 1 class Solution { 2 public: 3 int missingNumber(vector<int
阅读全文
posted @ 2020-03-01 12:09
habibah_chang
摘要:c++ STL set容器 insert后的set是从小到大排序的。 set.begin() < .. < set.rbegin() 参考代码: 1 class Solution { 2 public: 3 int thirdMax(vector<int>& nums) { 4 set<int> M
阅读全文
posted @ 2020-03-01 12:01
habibah_chang
摘要:核心:按照状态: 一开始持有金额:0 第一次买入:buy1 = 0-price[i] 第一次卖出:sol1 = buy1+price[i] 第二次买入:buy2 = sol1-price[i] 第二次卖出:sol2 = buy2+price[i] 由于从4->1向前依赖,所以对于每个i,赋值顺序应为
阅读全文
posted @ 2020-02-23 13:33
habibah_chang
摘要:问题:三角形,从顶到底最小路径 解法类型:DP动态规划 思路: 最原始思路:穷举每一行每个点的最小路径值,下一行=上一行累计,形成2维数组(n*n),再从数组最后一行选取最小值。 进化思路:每行计算,只需要上一行的结果,则只需要2维数组(2*n)两行保存数据即可。 再进化思路:如何化为只需要1维数组
阅读全文
posted @ 2020-02-16 14:34
habibah_chang
摘要:rowIndex=0 -> len = 1 所以 例如 rowIndex=3 1.初始化 res=[0,0,0,0],res[0]=1 res = [1,0,0,0] 2.从后往前加,循环 rowIndex-1 次,当前位=当前位+前一位 [1,0,0,0] [1(不变),1(=1+0),0,0]
阅读全文
posted @ 2020-02-16 13:52
habibah_chang
摘要:题目:前序+中序 or 中序+后序 -> 构建二叉树 前提: 1.前序:root->left->right 2.中序:left->root->right 3.后序:left->right->root e.g. 有以下的二叉树: 前序(preorder): [1, 2, 4, 8, 5, 9, 10,
阅读全文
posted @ 2020-02-16 12:57
habibah_chang
摘要:subsets([1,2,3,4]) = [] // push(1) [1, subsets([2,3,4])] // if push N times in subsets([2,3,4]), the pop times is also N, so vec is also [1] after bac
阅读全文
posted @ 2020-02-13 13:11
habibah_chang
摘要:方针: sort数组 固定前三个(i1,i2,i3初始化,逐次偏移+1),从后往前移动最后一个(i4)。 判断4数之和==target break; 问题: 1. 数组含有重复数字 解决:if(ix==ix-1) then continue; 2.优化:执行时间长 解决: 过滤1,if(i1+..+
阅读全文
posted @ 2020-02-13 11:22
habibah_chang

浙公网安备 33010602011771号