![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
文章平均质量分 68
TTLeoH
这个作者很懒,什么都没留下…
展开
-
微软 Leetcode
文章目录1. Two Sum3. Longest Substring Without Repeating Characters5. Longest Palindromic Substring23. Merge k Sorted Lists25. Reverse Nodes in k-Group41. First Missing Positive43 Multiply Strings49 Group Anagrams53 Maximum Subarray54 Spiral Matrix62 Unique Pa原创 2021-05-11 14:12:03 · 446 阅读 · 0 评论 -
微软 面试题
Kth Largest Element 第k大元素题目描述:在数组中找到第 k 大的元素。你可以交换数组中的元素的位置。Example样例 1:输入:k = 1nums = [1,3,4,2]输出:4解释:第一大的元素是4。样例 2:输入:k = 3nums = [9,3,2,4,8]输出:4解释:第三大的元素是4。Challenge要求时间复杂度为O(n),空间复杂度为O(1)。public class Solution { /**原创 2021-05-06 14:13:22 · 956 阅读 · 0 评论 -
Leetcode LinkedIn
464: 我能赢吗状压DP(DFS+记忆)T: O(NlogK)O(NlogK)O(NlogK)S:O(N)O(N)O(N)状态 int state: 可以用一个二进制位来标记1是否被用过了,即0表示没用过,1表示用过了。 比如00,表示没有一个数字被用过,01表示1被用过了,10表示2被用过了,11表示1和2都被用过了。我们使用f [] 布尔数组来记录对应状态下第一个人先选的情况下是否能赢的情况class Solution { Boolean[] f; int n, m ;原创 2021-04-22 18:05:24 · 900 阅读 · 0 评论 -
Leetcode 150: 逆波兰表达式求值
java 8 labmda 和 BiFunction 构建 map 表示 4个运算符对应的操作。stack 存储数字,遍历到运算符时把之前的两个数字 pop 出来运算得到的结果在 push 到stack 中。最终stack剩余的一个数字即为最终结果。class Solution { private final static Map<String, BiFunction<Integer, Integer, Integer>> map = new HashMap<>原创 2021-04-22 15:51:59 · 75 阅读 · 0 评论 -
Leetcode 1650: 二叉树的最近公共祖先 III
与160题一样的思路 1 ------o--- 1 + 2: ------o-----o--- 2 --o--- 2 + 1: --o---------o--- 公共祖先为root, Node p 到root 的 距离:x; Node q 到root 的 距离:y。让两Node先往上 找到 root,找到root后在各自指向以对方Node 再走一遍。最终 p 和 q 各自都走了 x+y 的 长度,且第二次到达 root 时相遇。对于 Node p 整个path:原创 2021-04-22 15:42:00 · 448 阅读 · 0 评论 -
Leetcode 1790: 仅执行一次字符串交换能否使两个字符串相等
s1 和 s2 只能有两个字符不同两个不同的字符位置交换后,s1 == s2class Solution { public boolean areAlmostEqual(String s1, String s2) { char[] sa = new char[2]; char[] ta = new char[2]; int cnt = 0; // 记录不同的次数 if(s1.length() .原创 2021-04-22 15:30:31 · 138 阅读 · 0 评论 -
KMP 总结
求next数组private int[] kmpNext(String str){ int[] next = new int[str.length()]; int j = 0; for(int i = 1; i < str.length(); i++){ while(j > 0 && str.charAt(i) != str.charAt(j)){ j = next[j-1原创 2021-04-17 03:25:36 · 85 阅读 · 0 评论 -
Java leetcode 常用代码模板
基础算法快速排序算法模板public void quickSort(int[] nums, int start,int end){ if(start >= end) return; int l = start; int r = end; int mid = l + (r-l>>1); int pivot = nums[mid]; while(l <= r){ w原创 2021-04-17 02:57:40 · 892 阅读 · 0 评论 -
Leetcode 755: 倒水 Pour Water
题目描述:We are given an elevation map, heights[i] representing the height of the terrain at that index. The width at each index is 1. After V units of water fall at index K, how much water is at each index?Water first drops at index K and rests on top of t原创 2021-04-14 13:18:08 · 262 阅读 · 0 评论 -
Leetcode 68:情侣牵手 Text Justification
中文描述:给定一个单词数组和一个长度 maxWidth,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本。你应该使用“贪心算法”来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格 ’ ’ 填充,使得每行恰好有 maxWidth 个字符。要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。文本的最后一行应为左对齐,且单词之间不插入额外的空格。说明:单词是指由非空格字符组成的字符序列。每原创 2021-04-13 07:19:55 · 90 阅读 · 0 评论 -
Leetcode 786:第 K 个最小的素数分数 K-th Smallest Prime Fraction
中文描述:给你一个按递增顺序排序的数组 arr 和一个整数 k 。数组 arr 由 1 和若干 素数组成,且其中所有整数互不相同。对于每对满足 0 < i < j < arr.length 的 i 和 j ,可以得到分数 arr[i] / arr[j] 。那么第 k 个最小的分数是多少呢? 以长度为 2 的整数数组返回你的答案, 这里 answer[0] == arr[i] 且 answer[1] == arr[j] 。题目描述:You are given a sorte原创 2021-04-13 05:39:28 · 267 阅读 · 0 评论 -
Union Find 并查集总结
转载并总结:并查集并查集(Disjoint set,Union-Find):一种用来解决集合查询合并的数据结构 支持 O(1) find 查找 O(1) union 集合初始化(Init):将每个元素所在集合初始化为其自身。合并(Union):将两个元素所属的集合合并为一个集合。查找(Find):查找元素所在的集合,即根节点。应用最小生成树:Kruskal 算法图的连通分量静态连通性动态连通性:判断图的最早连通时间局限:不支持拆分(split)操作:任何节点一旦成为其他节点的原创 2021-03-25 15:21:25 · 129 阅读 · 0 评论 -
Leetcode 765:情侣牵手 Couples Holding Hands
中文描述:N 对情侣坐在连续排列的 2N 个座位上,想要牵到对方的手。 计算最少交换座位的次数,以便每对情侣可以并肩坐在一起。 一次交换可选择任意两人,让他们站起来交换座位。人和座位用 0 到 2N-1 的整数表示,情侣们按顺序编号,第一对是 (0, 1),第二对是 (2, 3),以此类推,最后一对是 (2N-2, 2N-1)。这些情侣的初始座位 row[i] 是由最初始坐在第 i 个座位上的人决定的。题目描述:N couples sit in 2N seats arranged in a原创 2021-03-25 14:57:31 · 129 阅读 · 0 评论 -
Leetcode 685. 冗余连接 II Redundant Connection II
中文描述:在本问题中,有根树指满足以下条件的 有向 图。该树只有一个根节点,所有其他节点都是该根节点的后继。该树除了根节点之外的每一个节点都有且只有一个父节点,而根节点没有父节点。输入一个有向图,该图由一个有着 n 个节点(节点值不重复,从 1 到 n)的树及一条附加的有向边构成。附加的边包含在 1 到 n 中的两个不同顶点间,这条附加的边不属于树中已存在的边。结果图是一个以边组成的二维数组 edges 。 每个元素是一对 [ui, vi],用以表示 有向 图中连接顶点 ui 和顶点 vi 的边,其原创 2021-03-25 14:04:08 · 129 阅读 · 0 评论 -
Leetcode 684: 冗余连接 Redundant Connection
中文描述:在本问题中, 树指的是一个连通且无环的无向图。输入一个图,该图由一个有着N个节点 (节点值不重复1, 2, …, N) 的树及一条附加的边构成。附加的边的两个顶点包含在1到N中间,这条附加的边不属于树中已存在的边。结果图是一个以边组成的二维数组。每一个边的元素是一对[u, v] ,满足 u < v,表示连接顶点u 和v的无向图的边。返回一条可以删去的边,使得结果图是一个有着N个节点的树。如果有多个答案,则返回二维数组中最后出现的边。答案边 [u, v] 应满足相同的格式 u <原创 2021-03-25 13:22:51 · 86 阅读 · 0 评论 -
Leetcode 547: 省份数量 Number of Provinces
中文描述:有 n 个城市,其中一些彼此相连,另一些没有相连。如果城市 a 与城市 b 直接相连,且城市 b 与城市 c 直接相连,那么城市 a 与城市 c 间接相连。省份 是一组直接或间接相连的城市,组内不含其他没有相连的城市。给你一个 n x n 的矩阵 isConnected ,其中 isConnected[i][j] = 1 表示第 i 个城市和第 j 个城市直接相连,而 isConnected[i][j] = 0 表示二者不直接相连。返回矩阵中 省份 的数量。题目描述:There原创 2021-03-25 13:10:10 · 393 阅读 · 0 评论 -
Leetcode 399: 除法求值
中文描述:给你一个变量对数组 equations 和一个实数值数组 values 作为已知条件,其中 equations[i] = [Ai, Bi] 和 values[i] 共同表示等式 Ai / Bi = values[i] 。每个 Ai 或 Bi 是一个表示单个变量的字符串。另有一些以数组 queries 表示的问题,其中 queries[j] = [Cj, Dj] 表示第 j 个问题,请你根据已知条件找出 Cj / Dj = ? 的结果作为答案。返回 所有问题的答案 。如果存在某个无法原创 2021-03-25 13:02:10 · 113 阅读 · 0 评论 -
Leetcode 130:被围绕的区域 Surrounded Regions
中文描述:给你一个 m x n 的矩阵 board ,由若干字符 ‘X’ 和 ‘O’ ,找到所有被 ‘X’ 围绕的区域,并将这些区域里所有的 ‘O’ 用 ‘X’ 填充。题目描述:Given an m x n matrix board containing ‘X’ and ‘O’, capture all regions surrounded by ‘X’.A region is captured by flipping all 'O’s into 'X’s in that surrounded原创 2021-03-25 12:13:39 · 126 阅读 · 0 评论 -
Leetcode 128. 最长连续序列 Longest Consecutive Sequence
中文描述:给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。题目描述:Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.Example 1:Input: nums = [100,4,200,1,3,2]Output: 4Explanation: The longest cons原创 2021-03-25 11:39:43 · 126 阅读 · 0 评论 -
Leetcode 200: 岛屿数量 Number of Islands
中文描述:给你一个由 ‘1’(陆地)和 ‘0’(水)组成的的二维网格,请你计算网格中岛屿的数量。岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。此外,你可以假设该网格的四条边均被水包围。题目描述:Given an m x n 2D binary grid grid which represents a map of '1’s (land) and '0’s (water), return the number of islands.An island is原创 2021-03-25 12:36:15 · 156 阅读 · 0 评论 -
Leetcode 1802: 有界数组中指定下标处的最大值 Maximum Value at a Given Index in a Bounded Array
中文描述:给你三个正整数 n、index 和 maxSum 。你需要构造一个同时满足下述所有条件的数组 nums(下标 从 0 开始 计数):nums.length == nnums[i] 是 正整数 ,其中 0 <= i < nabs(nums[i] - nums[i+1]) <= 1 ,其中 0 <= i < n-1nums 中所有元素之和不超过 maxSumnums[index] 的值被 最大化返回你所构造的数组中的 nums[index] 。注意:ab原创 2021-03-23 09:02:59 · 267 阅读 · 0 评论 -
Leetcode 1539: 第 k 个缺失的正整数 Kth Missing Positive Number
中文描述:给你一个 严格升序排列 的正整数数组 arr 和一个整数 k 。请你找到这个数组里第 k 个缺失的正整数。题目描述:Given an array arr of positive integers sorted in a strictly increasing order, and an integer k.Find the kth positive integer that is missing from this array.Example 1:Input: arr =原创 2021-03-23 08:36:47 · 121 阅读 · 0 评论 -
Leetcode 1482: 制作 m 束花所需的最少天数 Minimum Number of Days to Make m Bouquets
中文描述:给你一个整数数组 bloomDay,以及两个整数 m 和 k 。现需要制作 m 束花。制作花束时,需要使用花园中 相邻的 k 朵花 。花园中有 n 朵花,第 i 朵花会在 bloomDay[i] 时盛开,恰好 可以用于 一束 花中。请你返回从花园中摘 m 束花需要等待的最少的天数。如果不能摘到 m 束花则返回 -1 。题目描述:Given an integer array bloomDay, an integer m and an integer k.We need to ma原创 2021-03-23 07:13:31 · 121 阅读 · 0 评论 -
Leetcode 1283:使结果不超过阈值的最小除数 Find the Smallest Divisor Given a Threshold
中文描述:给你一个整数数组 nums 和一个正整数 threshold ,你需要选择一个正整数作为除数,然后将数组里每个数都除以它,并对除法结果求和。请你找出能够使上述结果小于等于阈值 threshold 的除数中 最小 的那个。每个数除以除数后都向上取整,比方说 7/3 = 3 , 10/2 = 5 。题目保证一定有解。题目描述:Given an array of integers nums and an integer threshold, we will choose a posi原创 2021-03-23 06:58:12 · 119 阅读 · 0 评论 -
Leetcode 1231: 分享巧克力 Divide Chocolate
中文描述:你有一个由一些小块组成的巧克力棒。每一小块都有它自己的甜度,由数组sweetness给出。你想要和你的K个朋友分享巧克力,所以你开始切K次把巧克力切成K+1大块,每一大块都由一些连续的小块组成。孔融让梨,你会吃掉那块甜度最少的,然后把其他的分给你的朋友。设计算法优化切巧克力的方法,找出你能得到的最大甜度。题目描述:You have one chocolate bar that consists of some chunks. Each chunk has its own sweetne原创 2021-03-23 06:44:55 · 778 阅读 · 0 评论 -
Leetcode 1011:在 D 天内送达包裹的能力 Capacity To Ship Packages Within D Days
中文描述:传送带上的包裹必须在 D 天内从一个港口运送到另一个港口。传送带上的第 i 个包裹的重量为 weights[i]。每一天,我们都会按给出重量的顺序往传送带上装载包裹。我们装载的重量不会超过船的最大运载重量。返回能在 D 天内将传送带上的所有包裹送达的船的最低运载能力。题目描述:A conveyor belt has packages that must be shipped from one port to another within D days.The ith packag原创 2021-03-23 06:02:10 · 149 阅读 · 0 评论 -
Leetcode 875:爱吃香蕉的珂珂 Koko Eating Bananas
中文描述:珂珂喜欢吃香蕉。这里有 N 堆香蕉,第 i 堆中有 piles[i] 根香蕉。警卫已经离开了,将在 H 小时后回来。珂珂可以决定她吃香蕉的速度 K (单位:根/小时)。每个小时,她将会选择一堆香蕉,从中吃掉 K 根。如果这堆香蕉少于 K 根,她将吃掉这堆的所有香蕉,然后这一小时内不会再吃更多的香蕉。珂珂喜欢慢慢吃,但仍然想在警卫回来前吃掉所有的香蕉。返回她可以在 H 小时内吃掉所有香蕉的最小速度 K(K 为整数)。题目描述:Koko loves to eat bananas. T原创 2021-03-23 05:43:08 · 230 阅读 · 0 评论 -
Leetcode 774: 最小化去加油站的最大距离 Minimize Max Distance to Gas Station
中文描述:给定一个整数阵列加油站,表示加油站在x轴上的位置。给定一个整数k,要求增加k个新的加油站。您可以在x轴上的任何位置添加站点,而不必在整数位置上添加。设penalty()为增加k个新加油站后,相邻加油站之间的最大距离。返回penalty()的最小值。在实际答案的10-6以内的答案将被接受。题目描述:You are given an integer array stations that represents the positions of the gas stations on the原创 2021-03-23 05:26:11 · 454 阅读 · 0 评论 -
Leetcode 410: 分割数组的最大值 Split Array Largest Sum
中文描述:给定一个非负整数数组 nums 和一个整数 m ,你需要将这个数组分成 m 个非空的连续子数组。设计一个算法使得这 m 个子数组各自和的最大值最小。题目描述:Given an array nums which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays.Write an algorithm to mi原创 2021-03-23 04:59:01 · 222 阅读 · 0 评论 -
二分搜索总结
基础标准模版:Example 1:Input: nums = [-1,0,3,5,9,12], target = 9Output: 4Explanation: 9 exists in nums and its index is 4Example 2:Input: nums = [-1,0,3,5,9,12], target = 2Output: -1Explanation: 2 does not exist in nums so return -1class Solution {.原创 2021-03-23 04:36:59 · 235 阅读 · 0 评论 -
Leetcode 1734: 解码异或后的排列 Decode XORed Permutation
中文描述:给你一个整数数组 perm ,它是前 n 个正整数的排列,且 n 是个 奇数 。它被加密成另一个长度为 n - 1 的整数数组 encoded ,满足 encoded[i] = perm[i] XOR perm[i + 1] 。比方说,如果 perm = [1,3,2] ,那么 encoded = [2,1] 。给你 encoded 数组,请你返回原始数组 perm 。题目保证答案存在且唯一。题目描述:There is an integer array perm that is a原创 2021-03-19 14:31:37 · 133 阅读 · 0 评论 -
Leetcode 259: 较小的三数之和 3Sum Smaller
题目描述:Given an array of n integers nums and an integer target, find the number of index triplets i, j, k with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target.Follow up: Could you solve it in O(n2) runti原创 2021-03-10 15:32:00 · 146 阅读 · 0 评论 -
Leetcode 255: 验证前序遍历序列二叉搜索树 Verify Preorder Sequence in Binary Search Tree
题目描述:Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree.You may assume each number in the sequence is unique.Consider the following binary search tree: 5 / \ 2 6 / \1 3Exampl原创 2021-03-10 15:00:30 · 280 阅读 · 0 评论 -
Leetcode 0274: H 指数 H-Index
题目描述:Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher’s h-index.According to the definition of h-index on Wikipedia: “A scientist has index h if h of his/her N papers have原创 2021-02-26 13:16:04 · 287 阅读 · 0 评论 -
Leetcode 0273: 整数转换英文表示 Integer to English Words
题目描述:Write a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.中文描述:编写一个程序,找出第 n 个丑数。丑数就是质因数只包含 2, 3, 5 的正整数。Example:Input: n = 10Output: 12Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10,原创 2021-02-26 07:13:27 · 147 阅读 · 0 评论 -
Leetcode 0264: 丑数 II Ugly Number II
题目描述:Write a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.中文描述:编写一个程序,找出第 n 个丑数。丑数就是质因数只包含 2, 3, 5 的正整数。Example:Input: n = 10Output: 12Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10,原创 2021-02-25 14:46:54 · 62 阅读 · 0 评论 -
Leetcode 0241: 为运算表达式设计优先级 Different Ways to Add Parentheses
题目描述:Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *.中文描述:给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果。你需要给出所有可能的组合的结原创 2021-02-24 15:52:17 · 80 阅读 · 0 评论 -
Leetcode 0240: 搜索二维矩阵 II Search a 2D Matrix II
题目描述:Write an efficient algorithm that searches for a target value in an m x n integer matrix. The matrix has the following properties:Integers in each row are sorted in ascending from left to right.Integers in each column are sorted in ascending from原创 2021-02-24 15:32:19 · 72 阅读 · 0 评论 -
Leetcode 0239: Sliding Window Maximum
题目描述:You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.R原创 2021-02-23 14:37:00 · 91 阅读 · 0 评论 -
Leetcode 0238: Product of Array Except Self
题目描述:Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Example :Input: [1,2,3,4]Output: [24,12,8,6]Constraint:It’s guaranteed that th原创 2021-02-23 07:20:33 · 94 阅读 · 0 评论