- 博客(122)
- 收藏
- 关注
原创 leetcode739. Daily Temperatures(数组从后往前遍历)
Given a list of daily temperatures T, return a list such that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which...
2019-10-01 12:50:50
178
原创 leetcode253. Meeting Rooms II(greedy)
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],…] (si < ei), find the minimum number of conference rooms required.Example 1:Input: [[0, 30],[5, 10],[1...
2019-10-01 09:28:09
222
原创 leetcode366. Find Leaves of Binary Tree(递归)
Given a binary tree, collect a tree’s nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty.Example:Input: [1,2,3,4,5] 1 / \ 2 3 / \ ...
2019-09-13 16:59:50
273
原创 leetcode364. Nested List Weight Sum II
Example 1:Input: [[1,1],2,[1,1]]Output: 8Explanation: Four 1’s at depth 1, one 2 at depth 2.Example 2:Input: [1,[4,[6]]]Output: 17Explanation: One 1 at depth 3, one 4 at depth 2, and one 6 at d...
2019-09-10 12:41:33
151
原创 leetcode362. Design Hit Counter(队列)
Design a hit counter which counts the number of hits received in the past 5 minutes.Each function accepts a timestamp parameter (in seconds granularity) and you may assume that calls are being made t...
2019-09-09 14:04:49
244
原创 leetcode706. Design HashMap(两个数组实现hashmap)
一个数组储存key另一个数组储存valueclass MyHashMap { private int [] map=new int[100000]; private int [] mark=new int[100000]; /** Initialize your data structure here. */ public MyHashMap() { ...
2019-09-09 13:22:11
336
原创 leetcode205. Isomorphic Strings(HashMap)
Example 1:Input: s = “egg”, t = “add”Output: trueExample 2:Input: s = “foo”, t = “bar”Output: falseExample 3:Input: s = “paper”, t = “title”Output: trueclass Solution { public boolean isI...
2019-09-09 13:09:21
134
原创 leetcode671. Second Minimum Node In a Binary Tree(dfs)
找Binary Tree第二小元素/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class S...
2019-09-09 05:57:50
96
原创 leetcode744. Find Smallest Letter Greater Than Target(一次遍历)
Given a list of sorted characters letters containing only lowercase letters, and given a target letter target, find the smallest element in the list that is larger than the given target.Letters also ...
2019-09-09 05:37:05
182
原创 leetcode605种花问题(一次遍历)
Example 1:Input: flowerbed = [1,0,0,0,1], n = 1Output: TrueExample 2:Input: flowerbed = [1,0,0,0,1], n = 2Output: False判断条件:1)i位置为02)前一个花盆为空 或者 边界3)后一个花盆为空 或者 边界class Solution { public b...
2019-09-09 02:36:09
134
原创 leetcode256. Paint House(dp)
There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the ho...
2019-09-08 08:22:15
132
原创 leetcode349. 两个数组的交集(hashset)
给定两个数组,编写一个函数来计算它们的交集。示例 1:输入: nums1 = [1,2,2,1], nums2 = [2,2]输出: [2]示例 2:输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]输出: [9,4]class Solution { public int[] intersection(int[] nums1, int[] num...
2019-09-08 07:00:06
132
原创 leetcode339. Nested List Weight Sum(递归)
Given a nested list of integers, return the sum of all integers in the list weighted by their depth.Each element is either an integer, or a list – whose elements may also be integers or other lists....
2019-09-07 08:13:03
137
原创 leetcode819. Most Common Word(HashMap)
给定一个段落 (paragraph) 和一个禁用单词列表 (banned)。返回出现次数最多,同时不在禁用列表中的单词。题目保证至少有一个词不在禁用列表中,而且答案唯一。禁用列表中的单词用小写字母表示,不含标点符号。段落中的单词不区分大小写。答案都是小写字母。class Solution { public String mostCommonWord(String paragraph, ...
2019-09-06 07:46:44
232
原创 leetcode929. Unique Email Addresses(String)
Every email consists of a local name and a domain name, separated by the @ sign.For example, in alice@leetcode.com, alice is the local name, and leetcode.com is the domain name.Besides lowercase let...
2019-09-05 12:03:21
351
原创 leetcode67. Add Binary(字符串)
Given two binary strings, return their sum (also a binary string).The input strings are both non-empty and contains only characters 1 or 0.Example 1:Input: a = “11”, b = “1”Output: “100”Example 2...
2019-09-05 03:44:05
90
原创 leetcode937. Reorder Log Files(重写Comparator)
You have an array of logs. Each log is a space delimited string of words.For each log, the first word in each log is an alphanumeric identifier. Then, either:Each word after the identifier will co...
2019-09-02 02:44:29
158
1
原创 leetcode146. LRU缓存机制(LinkedHashMap)
!!!背下来运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 。获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。写入数据 put(key, value) - 如果密钥不存在,则写入其数据值。当缓存容量达到上限时,它应该在写入新数据之前删除最...
2019-08-31 07:12:11
179
原创 leetcode72. 编辑距离(dp)
给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 。你可以对一个单词进行如下三种操作:插入一个字符删除一个字符替换一个字符示例 1:输入: word1 = “horse”, word2 = “ros”输出: 3解释:horse -> rorse (将 ‘h’ 替换为 ‘r’)rorse -> rose (删除 ‘...
2019-08-31 06:31:29
115
原创 leetcode300. 最长上升子序列(dp)
给定一个无序的整数数组,找到其中最长上升子序列的长度。示例:输入: [10,9,2,5,3,7,101,18]输出: 4解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4。O(n^2)做法:动态规划,dp[i]存到第i个并且选择第i个的最长序列长度;结果是Max(dp[0],dp[1]…dp[n-1],所以用maxans存结果if(nums[i]>nums[...
2019-08-31 04:27:34
115
原创 leetcode 股票系列 121, 122, 123, 309, 118,714(dp)
leetcode121 ez:题目:一次交易(找出最大差值)做法:最小值记录到目前为止的最小值,res记录到目前为止最大利润。遍历一遍找到最大值。class Solution { public int maxProfit(int[] prices) { int min=Integer.MAX_VALUE; int res=0; fo...
2019-08-29 06:33:41
270
原创 leetcode52. N皇后 II(位运算最简单)
上图为 8 皇后问题的一种解法。给定一个整数 n,返回 n 皇后不同的解决方案的数量。示例:输入: 4输出: 2解释: 4 皇后问题存在如下两个不同的解法。[[".Q…", // 解法 1“…Q”,“Q…”,“…Q.”],["…Q.", // 解法 2“Q…”,“…Q”,“.Q…”]]class Solution { int count=0; i...
2019-08-27 04:26:00
428
原创 leetcode22. 括号生成(递归)
给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。例如,给出 n = 3,生成结果为:[“((()))”,“(()())”,“(())()”,“()(())”,“()()()”]class Solution { public List<String> generateParenthesis(int n) { ...
2019-08-19 06:18:57
109
原创 leetcode102. 二叉树的层次遍历(dfs 递归)
给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。例如:给定二叉树: [3,9,20,null,null,15,7],返回其层次遍历结果:[[3],[9,20],[15,7]]添加level标签class Solution { List<List<Integer>> levels = new ArrayList<...
2019-08-18 07:38:15
185
原创 leetcode242有效的字母异位词
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。示例 1:输入: s = “anagram”, t = “nagaram”输出: true示例 2:输入: s = “rat”, t = “car”输出: false法1:排序比较 N logNclass Solution { public boolean isAnagram(String ...
2019-08-13 09:48:36
81
原创 leetcode239. 滑动窗口最大值(双向队列)
给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。返回滑动窗口中的最大值。示例:输入: nums = [1,3,-1,-3,5,3,6,7], 和 k = 3输出: [3,3,5,5,6,7]解释:滑动窗口的位置 最大值[1 3 -1] -3 ...
2019-08-12 08:08:38
203
原创 leetcode703. 数据流中的第K大元素(PriorityQueue 最小堆)
设计一个找到数据流中第K大元素的类(class)。注意是排序后的第K大元素,不是第K个不同的元素。你的 KthLargest 类需要一个同时接收整数 k 和整数数组nums 的构造器,它包含数据流中的初始元素。每次调用 KthLargest.add,返回当前数据流中第K大的元素。示例:int k = 3;int[] arr = [4,5,8,2];KthLargest kthLarges...
2019-08-12 05:53:29
201
原创 leetcode 141环形链表(快慢指针)
给定一个链表,判断链表中是否有环。为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。public class Solution { public boolean hasCycle(ListNode head) { if(head==null||head.next==n...
2019-08-10 13:26:51
214
原创 leetcode24 Swap Nodes in Pairs(递归)
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。示例:给定 1->2->3->4, 你应该返回 2->1->4->3.class Solution { public ListNode swapPairs(ListNode head) { if(head==nu...
2019-08-10 11:29:49
215
原创 leetcode438. 找到字符串中所有字母异位词(滑块)
给定一个字符串 s 和一个非空字符串 p,找到 s 中所有是 p 的字母异位词的子串,返回这些子串的起始索引。字符串只包含小写英文字母,并且字符串 s 和 p 的长度都不超过 20100。说明:字母异位词指字母相同,但排列不同的字符串。不考虑答案输出的顺序。示例 1:输入:s: “cbaebabacd” p: “abc”输出:[0, 6]解释:起始索引等于 0 的子串是 “c...
2019-08-08 04:36:03
90
原创 leetcode682. 棒球比赛(stack)
你现在是棒球比赛记录员。给定一个字符串列表,每个字符串可以是以下四种类型之一:1.整数(一轮的得分):直接表示您在本轮中获得的积分数。2. “+”(一轮的得分):表示本轮获得的得分是前两轮有效 回合得分的总和。3. “D”(一轮的得分):表示本轮获得的得分是前一轮有效 回合得分的两倍。4. “C”(一个操作,这不是一个回合的分数):表示您获得的最后一个有效 回合的分数是无效的,应该被移除...
2019-07-29 09:23:20
77
原创 leetcode832. 翻转图像(一次遍历)
给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果。水平翻转图片就是将图片的每一行都进行翻转,即逆序。例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]。反转图片的意思是图片中的 0 全部被 1 替换, 1 全部被 0 替换。例如,反转 [0, 1, 1] 的结果是 [1, 0, 0]。示例 1:输入: [[1,1,0],[1,0,1],[0,0,0]]输...
2019-07-24 16:51:22
137
转载 leetcode98. 验证二叉搜索树(递归)
给定一个二叉树,判断其是否是一个有效的二叉搜索树。假设一个二叉搜索树具有如下特征:节点的左子树只包含小于当前节点的数。节点的右子树只包含大于当前节点的数。所有左子树和右子树自身必须也是二叉搜索树。首先将结点的值与上界和下界(如果有)比较。然后,对左子树和右子树递归进行该过程。class Solution { public boolean help(TreeNode root,I...
2019-07-24 14:40:17
345
转载 leetcode406. 根据身高重建队列(排序后插入)
假设有打乱顺序的一群人站成一个队列。 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数。 编写一个算法来重建这个队列。注意:总人数少于1100人。示例输入:[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]输出:[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]] ...
2019-07-24 09:53:01
373
转载 leetcode32. 最长有效括号(stack)
给定一个只包含 ‘(’ 和 ‘)’ 的字符串,找出最长的包含有效括号的子串的长度。示例 1:输入: “(()”输出: 2解释: 最长有效括号子串为 “()”示例 2:输入: “)()())”输出: 4解释: 最长有效括号子串为 “()()”与找到每个可能的子字符串后再判断它的有效性不同,我们可以用栈在遍历给定字符串的过程中去判断到目前为止扫描的子字符串的有效性,同时能的都最长有效...
2019-07-23 15:07:52
159
原创 leetcode1051. 高度检查器
学校在拍年度纪念照时,一般要求学生按照 非递减 的高度顺序排列。请你返回至少有多少个学生没有站在正确位置数量。该人数指的是:能让所有学生以 非递减 高度排列的必要移动人数。示例:输入:[1,1,4,2,1,3]输出:3解释:高度为 4、3 和最后一个 1 的学生,没有站在正确的位置。排序后比较不一样的位有多少个class Solution { public int heig...
2019-07-22 15:16:15
78
原创 leetcode100. 相同的树(递归)
给定两个二叉树,编写一个函数来检验它们是否相同。如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。示例 1:输入: 1 1/ \ / 2 3 2 3 [1,2,3], [1,2,3]输出: true示例 2:输入: 1 1/ 2 ...
2019-07-22 15:05:59
87
原创 leetcode496. 下一个更大元素 I( 循环+hashmap)
给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集。找到 nums1 中每个元素在 nums2 中的下一个比其大的值。nums1 中数字 x 的下一个更大元素是指 x 在 nums2 中对应位置的右边的第一个比 x 大的元素。如果不存在,对应位置输出-1。示例 1:输入: nums1 = [4,1,2], nums2 = [1,3,4,2].输...
2019-07-22 11:24:37
83
转载 leetcode64. 最小路径和(dp)
给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。说明:每次只能向下或者向右移动一步。示例:输入:[[1,3,1],[1,5,1],[4,2,1]]输出: 7解释: 因为路径 1→3→1→1→1 的总和最小。从右下往左上扩散public class Solution { public int minPathSum...
2019-07-20 16:14:22
150
转载 leetcode114. 二叉树展开为链表(递归)
给定一个二叉树,原地将它展开为链表。例如,给定二叉树1/ 2 5/ \ 3 4 6将其展开为:123456先右后左/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * ...
2019-07-20 14:42:12
164
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人