
ez
Rudy Chan
这个作者很懒,什么都没留下…
展开
-
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 · 339 阅读 · 0 评论 -
leetcode1051. 高度检查器
学校在拍年度纪念照时,一般要求学生按照 非递减 的高度顺序排列。请你返回至少有多少个学生没有站在正确位置数量。该人数指的是:能让所有学生以 非递减 高度排列的必要移动人数。示例:输入:[1,1,4,2,1,3]输出:3解释:高度为 4、3 和最后一个 1 的学生,没有站在正确的位置。排序后比较不一样的位有多少个class Solution { public int heig...原创 2019-07-22 15:16:15 · 79 阅读 · 0 评论 -
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 · 88 阅读 · 0 评论 -
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 · 86 阅读 · 0 评论 -
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 · 138 阅读 · 0 评论 -
leetcode682. 棒球比赛(stack)
你现在是棒球比赛记录员。给定一个字符串列表,每个字符串可以是以下四种类型之一:1.整数(一轮的得分):直接表示您在本轮中获得的积分数。2. “+”(一轮的得分):表示本轮获得的得分是前两轮有效 回合得分的总和。3. “D”(一轮的得分):表示本轮获得的得分是前一轮有效 回合得分的两倍。4. “C”(一个操作,这不是一个回合的分数):表示您获得的最后一个有效 回合的分数是无效的,应该被移除...原创 2019-07-29 09:23:20 · 79 阅读 · 0 评论 -
leetcode543. 二叉树的直径(递归)
给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过根结点。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * Tre...原创 2019-07-16 10:36:42 · 98 阅读 · 0 评论 -
leetcode448. 找到所有数组中消失的数字(下标)
给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次。找到所有在 [1, n] 范围之间没有出现在数组中的数字。您能在不使用额外空间且时间复杂度为O(n)的情况下完成这个任务吗? 你可以假定返回的数组不算在额外空间内。示例:输入:[4,3,2,7,8,2,3,1]输出:[5,6]利用下标进行markcla...原创 2019-07-11 17:10:46 · 78 阅读 · 0 评论 -
leetcode226. 翻转二叉树(递归)
翻转一棵二叉树。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution {...原创 2019-07-11 14:07:08 · 75 阅读 · 0 评论 -
leetcode461. 汉明距离hamming-distance(位运算)
两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。给出两个整数 x 和 y,计算它们之间的汉明距离。注意:0 ≤ x, y < 231.示例:输入: x = 1, y = 4输出: 2解释:1 (0 0 0 1)4 (0 1 0 0)class Solution { public int hammingDistance(int x, in...原创 2019-07-11 13:45:26 · 114 阅读 · 0 评论 -
leetcode234. 回文链表(stack)
请判断一个链表是否为回文链表。示例 1:输入: 1->2输出: false示例 2:输入: 1->2->2->1输出: true先全存在stack里面然后把list和stack 的peek比较 count/2 次/** * Definition for singly-linked list. * public class ListNode { * ...原创 2019-07-01 10:24:44 · 112 阅读 · 0 评论 -
leetcode538. 把二叉搜索树转换为累加树
给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和。逆序遍历 右中左/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left;...原创 2019-07-15 15:21:30 · 84 阅读 · 0 评论 -
leetcode437. 路径总和 III(二叉树)
给定一个二叉树,它的每个结点都存放着一个整数值。找出路径和等于给定数值的路径总数。路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点)。二叉树不超过1000个节点,且节点数值范围是 [-1000000,1000000] 的整数。双遍历/** * Definition for a binary tree node. * public clas...原创 2019-07-15 13:29:40 · 178 阅读 · 0 评论 -
leetcode38. 报数
报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数。其前五项如下:1112112111112211 被读作 “one 1” (“一个一”) , 即 11。11 被读作 “two 1s” (“两个一”), 即 21。21 被读作 “one 2”, “one 1” (“一个二” , “一个一”) , 即 1211。给定一个正整...转载 2019-06-30 15:44:45 · 88 阅读 · 0 评论 -
leetcode771. 宝石与石头(hashmap)
给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。示例 1:输入: J = “aA”, S = “aAAbbbb”输出: 3示例 2:输入: J = “z”, S = “ZZ”输出: ...原创 2019-07-10 17:16:26 · 120 阅读 · 0 评论 -
leetcode438. 找到字符串中所有字母异位词(滑块)
给定一个字符串 s 和一个非空字符串 p,找到 s 中所有是 p 的字母异位词的子串,返回这些子串的起始索引。字符串只包含小写英文字母,并且字符串 s 和 p 的长度都不超过 20100。说明:字母异位词指字母相同,但排列不同的字符串。不考虑答案输出的顺序。示例 1:输入:s: “cbaebabacd” p: “abc”输出:[0, 6]解释:起始索引等于 0 的子串是 “c...原创 2019-08-08 04:36:03 · 92 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 · 136 阅读 · 0 评论 -
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 · 99 阅读 · 0 评论 -
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 · 185 阅读 · 0 评论 -
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 · 135 阅读 · 0 评论 -
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 · 134 阅读 · 0 评论 -
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 · 133 阅读 · 0 评论 -
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 · 138 阅读 · 0 评论 -
leetcode819. Most Common Word(HashMap)
给定一个段落 (paragraph) 和一个禁用单词列表 (banned)。返回出现次数最多,同时不在禁用列表中的单词。题目保证至少有一个词不在禁用列表中,而且答案唯一。禁用列表中的单词用小写字母表示,不含标点符号。段落中的单词不区分大小写。答案都是小写字母。class Solution { public String mostCommonWord(String paragraph, ...原创 2019-09-06 07:46:44 · 232 阅读 · 0 评论 -
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 · 354 阅读 · 0 评论 -
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 · 92 阅读 · 0 评论 -
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 · 160 阅读 · 1 评论 -
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 · 273 阅读 · 0 评论 -
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 · 82 阅读 · 0 评论 -
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 · 203 阅读 · 0 评论 -
leetcode617. 合并二叉树(递归)
给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠。你需要将他们合并为一个新的二叉树。合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 NULL 的节点将直接作为新二叉树的节点。/** * Definition for a binary tree node. * public class TreeNode { * i...原创 2019-07-10 16:43:08 · 77 阅读 · 0 评论 -
leetcode509. 斐波那契数(递归+备忘录)
斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是:F(0) = 0, F(1) = 1F(N) = F(N - 1) + F(N - 2), 其中 N > 1.给定 N,计算 F(N)。示例 1:输入:2输出:1解释:F(2) = F(1) + F(0) = 1 + 0 = 1.示例 ...原创 2019-07-09 10:18:32 · 279 阅读 · 0 评论 -
leetcode387. 字符串中的第一个唯一字符
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。案例:s = “leetcode”返回 0.s = “loveleetcode”,返回 2.class Solution { public int firstUniqChar(String s) { int length=s.length(); int[] te...原创 2019-06-27 20:38:16 · 83 阅读 · 0 评论 -
leetcode412. Fizz Buzz
写一个程序,输出从 1 到 n 数字的字符串表示。如果 n 是3的倍数,输出“Fizz”;如果 n 是5的倍数,输出“Buzz”;3.如果 n 同时是3和5的倍数,输出 “FizzBuzz”。class Solution { public List<String> fizzBuzz(int n) { List<String> list ...原创 2019-06-09 06:34:24 · 169 阅读 · 0 评论 -
171. Excel表列序号 excel-sheet-column-number
给定一个Excel表格中的列名称,返回其相应的列序号。例如,A -> 1B -> 2C -> 3...Z -> 26AA -> 27AB -> 28 ...示例 1:输入: “A”输出: 1示例 2:输入: “AB”输出: 28示例 3:输入: “ZY”输出: 70126进制class Solution { pu...原创 2019-06-08 07:53:40 · 113 阅读 · 0 评论 -
leetcode118. 杨辉三角pascals-triangle
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。在杨辉三角中,每个数是它左上方和右上方的数的和。示例:输入: 5输出:[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]class Solution { public List<List<Integer>> generate(int num...原创 2019-06-08 07:20:35 · 100 阅读 · 0 评论 -
leetcode160. 相交链表
编写一个程序,找到两个单链表相交的起始节点。如果两个链表没有交点,返回 null.在返回结果后,两个链表仍须保持原有的结构。可假定整个链表结构中没有循环。程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。/**定义两个指针, 第一轮让两个到达末尾的节点指向另一个链表的头部, 最后如果相遇则为交点(在第一轮移动中恰好抹除了长度差)两个指针等于移动了相同的距离, 有交点就返回,...原创 2019-05-28 08:08:17 · 71 阅读 · 0 评论 -
155. 最小栈
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。push(x) – 将元素 x 推入栈中。pop() – 删除栈顶的元素。top() – 获取栈顶元素。getMin() – 检索栈中的最小元素。用两个stack实现,一个存数,一个存最小值class MinStack { Deque<Integer> stack1; ...原创 2019-05-28 06:22:53 · 129 阅读 · 0 评论 -
121. 买卖股票的最佳时机
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。注意你不能在买入股票前卖出股票。一次遍历,遇到最小值就存起来,遇到最大利益就存起来。class Solution { public int maxProfit(int[] prices) { int min= Int...原创 2019-05-27 08:50:46 · 89 阅读 · 0 评论