leetcode

【LEETCODE】69、动态规划,easy,medium级别,题目:198、139、221

醉酒当歌 提交于 2019-11-27 12:12:46
package y2019.Algorithm.dynamicprogramming.easy; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.dynamicprogramming.easy * @ClassName: Rob * @Author: xiaof * @Description: 198. House Robber * You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night. * * Given a list of non-negative integers representing the

LeetCode: 2. 两数相加

送分小仙女□ 提交于 2019-11-27 12:05:19
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。 您可以假设除了数字 0 之外,这两个数都不会以 0 开头。 示例: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807 思路: 将当前结点初始化为返回列表的哑结点。 将进位 carry 初始化为 0。 将 p 和 q 分别初始化为列表 l1 和 l2 的头部。 遍历列表 l1 和 l2 直至到达它们的尾端。 将 x 设为结点 p 的值。如果 p 已经到达 l1 的末尾,则将其值设置为 0。 将 y 设为结点 q 的值。如果 q 已经到达 l2 的末尾,则将其值设置为 0。 设定 sum = x + y + carry。 更新进位的值,carry = sum / 10。 创建一个数值为 (sum mod 10) 的新结点,并将其设置为当前结点的下一个结点,然后将当前结点前进到下一个结点。 同时,将 p 和 q 前进到下一个结点。 检查 carry=1 是否成立,如果成立,则向返回列表追加一个含有数字 1 的新结点。 返回哑结点的下一个结点。 请注意,我们使用哑结点来简化代码。如果没有哑结点

BFS小结

我们两清 提交于 2019-11-27 09:35:38
BFS 通过一些leetcode上面的题目, 总结出以下类型的题目: 树的层序遍历类型。 搜索联通块。 拓扑排序。 最短路径。 无边权重 有边权重 边界扩展。 增广 BFS 是基于 向量 的, 一般都需要一个 状态数组 , 一个 增广队列 。状态数组是为了防止 相同状态 的节点再次进后重复加入队列当中。 层序遍历 从根节点开始, 距离的根节点最近的顶点优先遍历。 因为同一个顶点不存在会被访问两次的情况, 所以不需要状态数组。 模板 queue<TreeNode*> nodes; /* 增广队列。*/ nodes.push(root); /* 压入树的根节点。*/ while(!nodes.empty()) { TreeNode *node = nodes.front(); nodes.pop(); /* 针对该顶点进行操作的代..... */ // 增广 if(node->left) nodes.push(node->left); if(node->right) nodes.push(node->right); } 题目 leetcode 103. 二叉树的锯齿形层次遍历 技巧: 节点状态中包含节点层数即可。 leetcode 199. 二叉树的右视图 节点状态中添加层数这一状态, 当当前节点 和 前一个节点的层数不一致时, 将前一个节点的值保存到答案当中, 最后一个顶点单独处理。

leetcode_11_盛水最多的容器(JavaScript)

喜你入骨 提交于 2019-11-27 07:25:29
文章目录 leetcode —— 11 : 盛水最多的容器 题目要求 题解思路 实现代码 代码效率 问题 leetcode —— 11 : 盛水最多的容器 leetcode原题 题目要求 题解思路 官方双指针法题解思路 参考官方双指针法: 容器的容积为bottom*height height为最小的边长 相对于边长的大小来说,底部的长度bottom的变化很小,看作不变 初始时以首尾两个高度作为边长。 由于容积只受限于较短边长,依数组元素顺序循环将较短变长替换。 最初我们考虑由最外围两条线段构成的区域。现在,为了使面积最大化,我们需要考虑更长的两条线段之间的区域。如果我们试图将指向较长线段的指针向内侧移动,矩形区域的面积将受限于较短的线段而不会获得任何增加。但是,在同样的条件下,移动指向较短线段的指针尽管造成了矩形宽度的减小,但却可能会有助于面积的增大。因为移动较短线段的指针会得到一条相对较长的线段,这可以克服由宽度减小而引起的面积减小。 实现代码 /** * @param {number[]} height * @return {number} */ var maxArea = function(height) { let head = 0, tail = height.length-1, altitude = 0, bottom = 0, capacity = 0; let

Leetcode日记7

别来无恙 提交于 2019-11-27 06:05:20
(2015/1/2) LeetCode 318 Maximum Product of Word Lengths 题目: 1)求一个字符串数组中,两个不同的字符串的长度乘积的最大值。 2)这两个字符串不能共同拥有同一个字符。(两个字符串的长度可以不相同) 3)所有字符串只由小写字符组成。 4)若不存在这样的两个字符串则返回0。 使用的数据结构: 1)定义一个结构体,第一个成员表示字符串的长度;第二个成员表示这个字符串拥有哪些小写字母。 2)第二个成员使用unsigned int,若有某个字母则相应位置1。(使用逻辑或和移位操作) 3)使用vector存放结构体。 若使用multimap数据类型则会出错, 待研究。 (2015/1/3) LeetCode 319 Bulb Switcher 1)使用模拟开关灯的方法不太现实。 2)一个灯被 toggle 的次数与它的因子个数相同。若它有偶数个因子则最后是关闭的,奇数个则是打开的。 3)一个数只有其开根号后是整数时,它才有奇数个因子。 LeetCode 141 Linked List Cycle 解法1:使用set。 解法2:参考 https://leetcode.com/discuss/32906/o-1-space-solution 1)使用两个结点指针。 2)一个指针一次走一步,一个指针一次走两步。 3)若有循环

LeetCode:Contains Duplicate

风格不统一 提交于 2019-11-27 06:04:17
1、题目名称 Contains Duplicate(判断数组内是否有重复元素) 2、题目地址 https://leetcode.com/problems/contains-duplicate/ 3、题目内容 英文:Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 中文:给出一个整数数组,判断该数组内是否有两个元素值是相同的,是则返回true,否则返回false 4、解题方法1 先将传入的数组进行排序,排序后查看相邻元素,如果存在相邻元素相同的情况,则说明原数组内有至少两个元素相同。 Java代码如下: import java.util.Arrays; /** * @功能说明:LeetCode 217 - Contains Duplicate * @开发人员:Tsybius2014 * @开发时间:2015年10月12日 */ public class Solution { /** * 查看数组内是否有重复元素 *

【LeetCode】475. Heaters (java实现)

强颜欢笑 提交于 2019-11-27 05:16:39
原题链接 https://leetcode.com/problems/heaters/ 原题 Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses. Now, you are given positions of houses and heaters on a horizontal line, find out minimum radius of heaters so that all houses could be covered by those heaters. So, your input will be the positions of houses and heaters seperately, and your expected output will be the minimum radius standard of heaters. Note: Numbers of houses and heaters you are given are non-negative and will not exceed 25000. Positions of houses and

Leetcode 78. Subsets

别说谁变了你拦得住时间么 提交于 2019-11-27 03:53:36
https://leetcode.com/problems/subsets/ Medium Given a set of distinct integers, nums , return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Example: Input: nums = [1,2,3] Output: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ] 三种解法:回溯,迭代,位运算。搞清楚迭代和位运算的算法逻辑。 迭代就是在现有的结果集上扩展添加新元素,作为新的结果集。 位运算需要把所有结果集放在一起看,其实是判断二进制中第几位上为1,则该位所对应的数字在该结果集中。 Python easy to understand solutions (DFS recursively, Bit Manipulation, Iteratively). - LeetCode Discuss https://leetcode.com/problems/subsets/discuss/27301/Python-easy-to-understand-solutions-(DFS

LeetCode 146. LRU缓存机制(LRU Cache)

主宰稳场 提交于 2019-11-27 00:22:37
LeetCode.jpg 146. LRU缓存机制 运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 。 获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。 写入数据 put(key, value) - 如果密钥不存在,则写入其数据值。当缓存容量达到上限时,它应该在写入新数据之前删除最近最少使用的数据值,从而为新的数据值留出空间。 进阶: 你是否可以在 O(1) 时间复杂度内完成这两种操作? 示例: LRUCache cache = new LRUCache( 2 /* 缓存容量 */ ); cache.put(1, 1); cache.put(2, 2); cache.get(1); // 返回 1 cache.put(3, 3); // 该操作会使得密钥 2 作废 cache.get(2); // 返回 -1 (未找到) cache.put(4, 4); // 该操作会使得密钥 1 作废 cache.get(1); // 返回 -1 (未找到) cache.get(3); // 返回 3 cache.get(4); // 返回 4 Python3 实现 LRU(最近最少使用) 缓存机制 更多可参见: https://en

Leetcode题目复习

ε祈祈猫儿з 提交于 2019-11-26 23:51:35
Leetcode 题目复习 题目 Leetcode 1 两数之和 Leetcode 2 两数相加 来源: CSDN 作者: 高高石头小花菇 链接: https://blog.csdn.net/qq_30163473/article/details/89011804