![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
数据结构与算法
文章平均质量分 59
NJU_ChopinXBP
矜持放荡,明媚忧伤,技术不宅,文艺俗咖
展开
-
LeetCode(459):重复的子字符串 Repeated Substring Pattern(Java)
2021.1.22 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel如果s中包含重复的子字符串,那么说明s中至少包含两个子字符串,s+s至少包含4个字串,前后各去掉一位,查找s是否在新构建的字符串中。传送门:重复的子字符串Given a non-empty string check if it can be constructed by taking a substring of it and原创 2021-01-22 12:40:52 · 310 阅读 · 1 评论 -
LeetCode(402):移掉K位数字 Remove K Digits(Java)
2021.1.21 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel最小递增栈,每次替换栈顶比当前元素大的元素,直至k次。若替换完成之后k>0,从尾部移除。传送门:移掉K位数字Given a non-negative integer num represented as a string, remove k digits from the number so that the new nu原创 2021-01-21 10:32:49 · 344 阅读 · 3 评论 -
LeetCode(453):最小移动次数使数组元素相等 Minimum Moves to Equal Array Elements(Java)
2021.1.18 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel1.先对数组排序2.排序后,第1次更新除最大值nums[len-1]外的其余值,更新nums[i]-nums[0]次,使得nums[0]=nums[len-1]同为最小值3.而nums[len-2]为新的最大值,第二次更新nums[len-2]-nums[0]次,使得nums[0]=nums[len-1]=nums[len-2]同原创 2021-01-18 13:46:12 · 273 阅读 · 1 评论 -
LeetCode(1361):验证二叉树 Validate Binary Tree Nodes(Java)
2021.1.17 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel直接的思路是借助哈希表+DFS验证二叉树的正确性。1.同一结点不能有两个父节点2.有且仅有一个根节点3.结点中不存在环高级的一点的方法可以借助图论:叶子结点个数 = 非叶子结点个数 + 1将所有-1看成叶子结点,也即:num(-1) = n + 1传送门:验证二叉树You have n binary tree原创 2021-01-17 11:27:03 · 201 阅读 · 0 评论 -
LeetCode(1360):日期之间隔几天 Number of Days Between Two Dates(Java)
2021.1.16 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel如果能依靠库函数Date和SimpleDateFormat可以很快解决问题。在不能依赖库函数的情况下,直接让两个日期做差需要考虑的因素太多。需要换个思路,分别求出两个日期距离1970年1月1日的天数,再将两个天数做差即可。传送门:日期之间隔几天Write a program to count the number of day原创 2021-01-16 13:01:58 · 238 阅读 · 0 评论 -
LeetCode(401):二进制手表 Binary Watch(Java)
2021.1.11 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel可以有两种思路:1.遍历所有符合要求的时间,统计二进制中1的个数,把个数等于num的时间筛选出来。2.回溯法遍历所有LED,记录符合条件的时间。传送门:二进制手表A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 L原创 2021-01-11 08:39:11 · 152 阅读 · 0 评论 -
LeetCode(1319):连通网络的操作次数 Number of Operations to Make Network Connected(Java)
2021.1.8 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel这是一道经典的并查集问题。关键点在于:每一个多余的连通集都需要一次操作进行消除,每一次操作需要一条多余的边。因此,可以先建立一个并查集,初始连通集个数为n。根据给定的connection来合并连通集,每一次失败的合并意味着有一条多余的边。计算多余的连通集个数result。如果result=0,说明已经全连通,直接返回0;如果l原创 2021-01-08 09:42:37 · 161 阅读 · 1 评论 -
LeetCode(342):4的幂 Power of Four(Java)
2021.1.6 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel不能用循环或者递归,意味着就得依靠奇技淫巧了。位运算方法11.该数是2的幂(仅有一位为1),用n&(n-1)可以验证(去掉最右边的1)2.这个1出现在奇数位上,用0x[5] = 0b[0101]可以验证位运算方法21.该数是2的幂(仅有一位为1),用n&(n-1)可以验证(去掉最右边的1)2.保证上一条的前提原创 2021-01-06 16:54:18 · 105 阅读 · 0 评论 -
LeetCode(1297):子串的最大出现次数 Maximum Number of Occurrences of a Substring(Java)
2021.1.4 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel利用滑动窗口的思路可以做,但是本题有一个思维技巧在于:只需要创建minSize的滑动窗口即可,如果长度为maxSize的子串出现了N次,那么长度为minSize的子串也会出现N次。而题目只要求最大数量的子串即可,没有长度要求。传送门:子串的最大出现次数Given a string s, return the maximum num原创 2021-01-04 11:13:26 · 252 阅读 · 0 评论 -
LeetCode(1296):划分数组为连续数字的集合 Divide Array in Sets of K Consecutive Numbers(Java)
2021.1.2 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel可以用TreeMap对数组中的元素进行排序并对个数进行记录,再依次检查。传送门:划分数组为连续数字的集合Given an array of integers nums and a positive integer k, find whether it’s possible to divide this array into set原创 2021-01-02 15:24:36 · 206 阅读 · 0 评论 -
LeetCode(601):体育馆的人流量 Human Traffic of Stadium(SQL)
2021.1.1 第一天,元旦快乐LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel括号题一般可以考虑用递归/回溯、栈等等,子串问题一般可以考虑用动态规划、滑动窗口等等。这道题结果要求输出多种组合,一般也就考虑用递归/回溯或者动态规划了。这道题的回溯思想如下:1.先计算不匹配的左右括号数leftError、rightError。2.开始递归回溯,返回条件是判断当前递归位置idx是否到达尾部,如果此时原创 2021-01-01 16:43:27 · 1153 阅读 · 1 评论 -
LeetCode(263 & 264):丑数 I II Ugly Number I II(Java)
2020.12.30 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel丑数I只需要验证是否为丑数即可,直接将其除净即可。丑数II要求返回指定顺位的丑数,可以采用归并的思想。用三指针指向下一个需要乘n(n=2/3/5)进行拓展的数字,相当于做一个三路归并。找出三个指针的最小元素,将指针后移。如果有多组指针的当前值都是min,指针都需要后移,保证 ugly 数组中不会加入重复元素。传送门:丑数W原创 2020-12-30 17:48:35 · 121 阅读 · 0 评论 -
LeetCode(262):行程和用户 Trips and Users(SQL)
2020.12.29 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel传送门:行程和用户Trips 表中存所有出租车的行程信息。每段行程有唯一键 Id,Client_Id 和 Driver_Id 是 Users 表中 Users_Id 的外键。Status 是枚举类型,枚举成员为 (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’)。原创 2020-12-29 17:41:31 · 170 阅读 · 0 评论 -
LeetCode(258):各位相加 Add Digits(Java)
2020.12.27 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel智力题规律很简单:数字为0时,结果为0;数字为9的倍数时,结果为9;其他情况下为除以9的余数。假设输入的数字是一个5位数字num,则num的各位分别为a、b、c、d、e。有如下关系:num = a * 10000 + b * 1000 + c * 100 + d * 10 + e即:num = (a + b + c + d原创 2020-12-27 20:49:53 · 115 阅读 · 0 评论 -
LeetCode(1292):元素和小于等于阈值的正方形的最大边长 Maximum Side Length of a Square(Java)
2020.12.24 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel原地动态规划 + 前缀和。遍历每一个位置ij时,将该位的数值置为从该位起左边的1的总个数,例如对于矩阵 [1,1,3,2], [1,1,4,3], [1,2,4,2]遍历结束后效果为: [1,2,5,7], [1,2,6,9], [1,3,7,9]为了统计正方形矩阵的面积,可以假定当前位置ij为正方原创 2020-12-24 16:12:52 · 162 阅读 · 1 评论 -
LeetCode(185):部门工资前三高的所有员工 Department Top Three Salaries(SQL)
2020.12.23 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel传送门:部门工资前三高的所有员工Employee 表包含所有员工信息,每个员工有其对应的工号 Id,姓名 Name,工资 Salary 和部门编号 DepartmentId 。+----+-------+--------+--------------+| Id | Name | Salary | DepartmentId |原创 2020-12-23 19:32:37 · 206 阅读 · 0 评论 -
LeetCode(168):Excel表列名称 Excel Sheet Column Title(Java)
2020.12.21 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel这题实质上是移位进制转换问题。xi*26^(i-1) + … + x3*26^2 + x2*26^1 + x1*26^0 = n每次num=n%26可以取出最低位的xi=num,n/=26可以去掉最低位的xi.但题目每个位的范围是1-26,而不是0-25。因此当num==26时需要进行修正,将n-1是使n不满足26的倍数,再原创 2020-12-21 16:17:20 · 149 阅读 · 0 评论 -
LeetCode(626):换座位 Exchange Seats(SQL)
2020.12.20 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel传送门:换座位小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id。其中纵列的 id 是连续递增的小美想改变相邻俩学生的座位。你能不能帮她写一个 SQL query 来输出小美想要的结果呢?示例:+---------+---------+| id | stu原创 2020-12-20 19:40:56 · 155 阅读 · 0 评论 -
LeetCode(1226):哲学家进餐 The Dining Philosophers(JUC)
2020.12.19 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel传送门:哲学家进餐Five silent philosophers sit at a round table with bowls of spaghetti. Forks are placed between each pair of adjacent philosophers.Each philosopher must alt原创 2020-12-19 20:35:46 · 415 阅读 · 1 评论 -
LeetCode(184):部门工资最高的员工 Reformat Department Table(SQL)
2020.7.8 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel传送门:部门工资最高的员工Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id。+----+-------+--------+--------------+| Id | Name | Salary | DepartmentId |+----+-------+------原创 2020-07-08 20:34:47 · 194 阅读 · 0 评论 -
LeetCode(1195):交替打印字符串 Fizz Buzz Multithreaded(JUC)
2020.7.7 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel传送门:交替打印字符串Write a program that outputs the string representation of numbers from 1 to n, however:If the number is divisible by 3, output “fizz”.If the number is divi原创 2020-07-07 14:10:25 · 335 阅读 · 1 评论 -
LeetCode(432):全 O(1) 的数据结构 All O`one Data Structure(Java)
2020.2.22 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel这道题要求在常数的复杂度内对这个数据结构进行查找、增删、获取最值操作。而且相同count值还可能会同时存在多个key,因此需要结合多种数据结构进行。依靠一个HashMap、一个排序双向链表DoubleLinkedList,...原创 2020-02-22 15:42:12 · 406 阅读 · 0 评论 -
LeetCode(1117):H2O 生成 Building H2O(JUC)
2020.1.30 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel传送门:H2O 生成There are two kinds of threads, oxygen and hydrogen. Your goal is to group these threads to form wate...原创 2020-01-30 17:50:25 · 384 阅读 · 0 评论 -
LeetCode(354):俄罗斯套娃信封问题 Russian Doll Envelopes(Java)
2020.1.10 . LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel这道题是之前 LeetCode(300):最长上升子序列 Longest Increasing Subsequence(Java) 的拓展问题。对所有信封排序之后,原题相当于是二维的最长上升子序列。思路主要有动态规划和动...原创 2020-01-10 22:07:46 · 426 阅读 · 0 评论 -
LeetCode(1277):统计全为 1 的正方形子矩阵 Count Square Submatrices with All Ones(Java)
2019.12.29 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel原地动态规划。遍历每一个位置ij时,将该位的数值置为从该位起左边的1的总个数,例如对于矩阵 [0,1,1,1], [1,1,1,1], [0,1,1,1]遍历结束后效果为: [0,1,2,3], ...原创 2019-12-29 12:34:41 · 644 阅读 · 0 评论 -
LeetCode(120):三角形最小路径和 Triangle(Java)
2019.12.22 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel自底向上的动态规划。建立dp数组,dp[j]代表当前层j位置的最小路径和,对于每一层i,自底向上有动态转移方程:dp[j] = triangle.get(i - 1).get(j) + Math.min(dp[j], d...原创 2019-12-22 15:16:11 · 238 阅读 · 0 评论 -
LeetCode(60):第k个排列 Permutation Sequence(Java)
2019.12.19 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel之前有做过两道排列相关的题目:LeetCode(46):全排列 Permutations(Java)LeetCode(31):下一个排列 Next Permutation(Java)但是,单纯依靠之前的全排列方法做出...原创 2019-12-19 09:47:00 · 249 阅读 · 0 评论 -
LeetCode(1114):按序打印 Print in Order(JUC)
2019.12.17 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel传送门:按序打印Suppose we have a class:public class Foo { public void first() { print("first"); } public void se...原创 2019-12-17 19:55:44 · 252 阅读 · 0 评论 -
LeetCode(1268):搜索推荐系统 Search Suggestions System(Java)
2019.12.9 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel这是参加的第二场周赛。本题很直接可以想到前缀树+DFS的思路,不过要注意一点神坑:测试用例的字典中可能包含重复的单词。应对这点,只需要在字典树结点中加上条件count用于统计相同单词的出现次数即可。也可以不直接建树,先将...原创 2019-12-09 20:35:19 · 750 阅读 · 0 评论 -
LeetCode(93):复原IP地址 Restore IP Addresses(Java)
2019.12.6 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel递归方法在本题上比较高效,也很容易想到。还可以依靠动态规划的思路。建立dp数组,dp[i]代表截至第i位字符串能够组成的地址集合。对于第i位上的地址集合,可以遍历前三位j,状态转移方程为:dp[i] += dp[j] +...原创 2019-12-06 20:18:25 · 176 阅读 · 0 评论 -
LeetCode(77):组合 Combinations(Java)
2019.12.1 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel这题可以很容易想到用剪枝回溯法求解,不过还有一种字典序的思路:1.将数字序列nums初始化为[1…k, n+1],一共k+1个元素,最后一位n+1用于作为哨兵。2.循环将nums从最小排列[1…k, n+1]更新至最大排...原创 2019-12-01 12:22:20 · 226 阅读 · 0 评论 -
LeetCode(37):解数独 Sudoku Solver(Java)
2019.11.30 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel智力题。经典的回溯法,需要保存行列块数组,进行约束检查。传送门:解数独Write a program to solve a Sudoku puzzle by filling the empty cells.A su...原创 2019-11-30 19:49:43 · 222 阅读 · 0 评论 -
LeetCode(51 & 52):N皇后 N-Queens(Java)
2019.11.29 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel智力题。可以通过回溯法来依次放置皇后,得出最终能够放置的所有结果。传送门:N皇后The n-queens puzzle is the problem of placing n queens on an n×n che...原创 2019-11-29 17:44:33 · 177 阅读 · 0 评论 -
LeetCode(887):鸡蛋掉落 Super Egg Drop(Java)
2019.11.27 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel这是一道经典的智力题。由于有鸡蛋摔碎的限定条件,因此不能依靠单纯的二分查找来解决,可以从递归开始,不断优化和升级方法。1.递归(超时)在手握K个鸡蛋时从第i层楼[0,N]向下丢鸡蛋,会有两种可能:1.鸡蛋摔碎,将剩余...原创 2019-11-27 11:44:46 · 390 阅读 · 0 评论 -
LeetCode(1262):可被三整除的最大和 Greatest Sum Divisible by Three(Java)
2019.11.24 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel这是第一次参加周赛,最后没完全做出这道01背包问题,其关键在于初始值的设定。建立dp数组,dp[i][j]代表到第i位数字(num[i-1])模3的余数为j的最大累加和。i=0时的初始值为{0,-INT,-INT},若...原创 2019-11-24 14:24:41 · 644 阅读 · 1 评论 -
LeetCode(336):回文对 Palindrome Pairs(Java)
2019.11.22 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel这道题本质上是要切分字符串,使得字典中的两个单词word1[0,end] + word2[0,j-1] + word2[j,end]或者word1[0,j-1] + word1[j,end] + word2[0,en...原创 2019-11-22 11:39:47 · 237 阅读 · 0 评论 -
LeetCode(421):数组中两个数的最大异或值 Maximum XOR of Two Numbers in an Array(Java)
2019.11.20 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel如果想用o(n)的方法找到最大的异或值,根本思路是将n^2的遍历计算转换成32n的按位匹配。这题可以用两种方法:1.异或性质+贪心算法异或的性质:如果 a ^ b = c 成立,那么a ^ c = b 与 b ^ c ...原创 2019-11-20 21:38:42 · 255 阅读 · 0 评论 -
LeetCode(211 & 648):添加与搜索单词 Add and Search Word & 单词替换 Replace Words(Java)
LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel两道经典的手撕字典树。用字典树结点值end代表是否是一个字典词的结尾,根据需求返回对应结果即可。传送门:添加与搜索单词 - 数据结构设计Design a data structure that supports the following ...原创 2019-11-19 20:56:21 · 209 阅读 · 0 评论 -
LeetCode(677):键值映射 Map Sum Pairs(Java)
2019.11.18 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel这题需要用道前缀树+哈希表,前缀树每个结点存放以其为前缀的键值总和,哈希表每个结点存放插入的元素及其最新插入值。插入插入可能有两种情况:1.key首次插入,map中不存在,前缀路径的所有结点value更新为val值...原创 2019-11-18 19:53:20 · 246 阅读 · 0 评论 -
LeetCode(220):存在重复元素 III Contains Duplicate III(Java)
2019.11.17 LeetCode 从零单刷个人笔记整理(持续更新)github:https://github.com/ChopinXBP/LeetCode-Babel之前其实通过HashSet做过一道 传送门:存在重复元素II,但是这道升级版很不一样,从寻找重复元素(target = num)变成了寻找在差在t范围内的元素(num-t <= target <= num+t)...原创 2019-11-17 15:22:06 · 203 阅读 · 0 评论