leetcode

LeetCode(111&104):二叉树的最小深度+最大深度 Minimum or Maximum Depth of Binary Tree(Java)

假装没事ソ 提交于 2019-11-26 21:06:06
2019.4.3 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新) 本期开始更LeetCode,因为博客速度已经远远赶不上刷题速度,因此考虑只把比较有价值的题目放上博客作为记录。 深度就是按层计数的结点数,一个结点的深度是1。 二叉树的最小深度可以依靠剪枝层序遍历,也就是从根节点开始进行BFS,当遇到第一个叶子结点的时候停止,此时的深度对应着最小深度。 二叉树的最大深度只需要进行简单的递归,每个结点的最大深度等于左子树的最大深度与右子树的最大深度的较大值+1。 传送门:二叉树的最小深度 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Note: A leaf is a node with no children. 给定一个二叉树,找出其最小深度。 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 说明: 叶子节点是指没有子节点的节点。 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回它的最小深度 2. 传送门

LeetCode实战:LRU缓存机制

我们两清 提交于 2019-11-26 19:34:13
背景 为什么你要加入一个技术团队? 如何加入 LSGO 软件技术团队? 我是如何组织“算法刻意练习活动”的? 为什么要求团队的学生们写技术Blog 题目英文 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. put(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item. The cache is initialized with a positive capacity. Follow up : Could you do both

LeetCode 146. LRU缓存机制

我们两清 提交于 2019-11-26 19:29:37
LeetCode 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 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems

LeetCode146——LRU缓存机制

为君一笑 提交于 2019-11-26 19:28:29
我的LeetCode代码仓: https://github.com/617076674/LeetCode 原题链接: https://leetcode-cn.com/problems/lru-cache/description/ 题目描述: 知识点:哈希表、双向链表、LRU缓存机制 思路一:用哈希表记录键值对,用双向链表处理LRU缓存机制 用示例来说明我们算法的流程,为了防止对null节点的讨论,初始化双向链表时设置虚拟头节点和虚拟尾节点,初始化的双向链表如下图所示: (1)put(1, 1) 由于此时我们的链表容量为0,可以新增节点,且无需删除节点,这步操作后我们的链表如下图所示: (2)put(2, 2) 由于此时我们的链表容量为1,可以新增节点,且无需删除节点,这步操作后我们的链表如下图所示: (3)get(1) 此时我们将(1, 1)节点从链表尾部删除,并将其新增至链表头部,这步操作后我们的链表如下图所示: (4)put(3, 3) 由于此时我们的链表容量为2,如果新增节点,就需要先删除节点,因此我们先删除(2, 2)节点,再新增(3, 3)节点。这步操作后我们的链表如下图所示: (5)get(2) 如上图所示,链表中并没有2这个键,返回-1,无需对链表做任何操作。 (6)put(4, 4) 由于此时我们的链表容量为2,如果新增节点,就需要先删除节点,因此我们先删除(1,

leetcode (Find Smallest Letter Greater Than Target)

旧城冷巷雨未停 提交于 2019-11-26 18:47:57
Title:Find Smallest Letter Greater Than Target 744 Difficulty:Easy 原题leetcode地址: https://leetcode.com/problems/find-smallest-letter-greater-than-target/ 1. 见代码注释 时间复杂度:O(n),一层while循环。 空间复杂度:O(n),申请长度为26的数组。 /** * 采用数组将letter出现的字母标记为1,在循环中对target做加加操作,只要找到某处存的是1,就找到对应的数,注意z的位置 * @param letters * @param target * @return */ public static char nextGreatestLetter(char[] letters, char target) { int tmp[] = new int[26]; for (int i = 0; i < letters.length; i++) { tmp[letters[i] - 'a'] = 1; } while (true) { target++; if (target > 'z') { return letters[0]; } if (tmp[target - 'a'] == 1) { return target;

Leetcode日记6

北战南征 提交于 2019-11-26 18:10:26
(2015/11/28) LeetCode 303 Range Sum Query - Immutable :(Easy) 1)超时的算法:每次调用sumRange函数进行一次累加运算。 2)不超时的算法:改变数组的内容,存储从0下标到当前下标所有元素的和。每次调用 sumRange函数进行一次减法运算。 LeetCode 290 Word Pattern :(Easy) 1)建立两个map,使字符和字符串建立一一对应的关系。 2)2种情况的判断:pattern中的字符比string多,string比pattern中的字符多。 LeetCode 283 Move Zeroes :(Easy) 1)i:指向非零元素要插入的位置;j:寻找下一个非零元素;zeronums:记录0的个数。 LeetCode 263 Ugly Number :(Easy)(别人有更快的方法( 待看 )) 1)循环除2,除3,除5,判断最后的结果。 LeetCode 258 Add Digits :(Easy) LeetCode 242 Valid Anagram :(Easy) LeetCode 299 Bulls and Cows :(Easy) LeetCode 292 Nim Game :(Easy) LeetCode 237 Delete Node in a Linked List :(Easy)

LeetCode:Contains Duplicate II

大憨熊 提交于 2019-11-26 18:09:44
1、题目名称 Contains Duplicate II(判断数组内是否有重复元素2) 2、题目地址 https://leetcode.com/problems/contains-duplicate-ii/ 3、题目内容 英文:Given an array of integers and an integer k , find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k . 中文:给出一个整数数组,判断该数组内是否有两个元素值是相同的,且他们的索引值相差不大于k,是则返回true,否则返回false 4、一个TLE的方法 本题如果直接使用暴力方法解决会运行超时。一段TLE的Java代码如下: /** * @功能说明:LeetCode 219 - Contains Duplicate II * @开发人员:Tsybius2014 * @开发时间:2015年10月15日 */ public class Solution { /** * 查看数组内是否有重复元素且相邻重复元素索引间隔不大于K * @param nums * @return */

[LeetCode] 857. Minimum Cost to Hire K Workers 雇佣K名工人的最低成本

霸气de小男生 提交于 2019-11-26 17:41:59
There are N workers. The i -th worker has a quality[i] and a minimum wage expectation wage[i] . Now we want to hire exactly K workers to form a paid group . When hiring a group of K workers, we must pay them according to the following rules: Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group. Every worker in the paid group must be paid at least their minimum wage expectation. Return the least amount of money needed to form a paid group satisfying the above conditions. Example 1: Input: quality = [10,20,5], wage = [70,50,30],

[Leetcode算法学习]:Leetcode模块Queue & Stack部分总结

╄→尐↘猪︶ㄣ 提交于 2019-11-26 17:10:10
Leetcode中的Queue & Stack部分学习总结 Stack部分:733. Flood fill 以下代码可以作为DFS的新范式进行参考,代码较为简洁(毕竟头条写DFS模板时候被喷冗余的那一幕仍然历历在目= =)。 class Solution(): def floodFill(self, image, sr, sc, newColor): if len(image) == 0: return image elif image[sr][sc] == newColor: return image self.fill(image, sr, sc, newColor, image[sr][sc]) return image def fill(self, image, sr, sc, newColor, color): # 进入递归之前,先判断传入坐标合不合法,否则直接返回。 if (sr < 0) or (sr > len(image) - 1) or (sc < 0) or (sc > len(image[0]) - 1) or (image[sr][sc] != color): return image[sr][sc] = newColor self.fill(image, sr-1, sc, newColor, color) self.fill(image, sc+1,

一些不错的题

旧街凉风 提交于 2019-11-26 16:37:57
https://leetcode.com/problems/bricks-falling-when-hit/ https://leetcode.com/problems/remove-boxes/ https://leetcode.com/problems/cherry-pickup/ https://leetcode.com/problems/cat-and-mouse/ https://leetcode.com/problems/poor-pigs/ https://leetcode.com/problems/course-schedule-iii/ https://leetcode.com/problems/valid-permutations-for-di-sequence/ https://leetcode.com/problems/find-the-shortest-superstring/ 来源: https://blog.csdn.net/yaoct/article/details/98954487