刷题
汋灼
这个作者很懒,什么都没留下…
展开
-
1761. Minimum Degree of a Connected Trio in a Graph [Hard]
/** * construct adjacency matrix and degree array instead of adjacency table * traverse edges instead of vertices to reduce one level of loop * Runtime: 45 ms, faster than 87.20% * Memory Usage: 69.8 MB, less than 33.74% */class Solution { publ.原创 2021-10-16 04:12:58 · 144 阅读 · 0 评论 -
128. Longest Consecutive Sequence [Medium]
/** * Runtime: 24 ms, faster than 64.64% * Memory Usage: 54.5 MB, less than 46.79% */class Solution { public int longestConsecutive(int[] nums) { int max = 0; HashMap<Integer, Integer> map = new HashMap<>(); // .原创 2021-10-16 03:03:22 · 121 阅读 · 0 评论 -
684. Redundant Connection [Medium]
/** * Runtime: 3 ms, faster than 29.76% * Memory Usage: 39.5 MB, less than 53.75% */class Solution { public int[] findRedundantConnection(int[][] edges) { HashMap<Integer, Integer> parents = new HashMap<>(); for (int[] .原创 2021-10-16 01:53:10 · 132 阅读 · 0 评论 -
340. Longest Substring with At Most K Distinct Characters [Medium]
/** * Runtime: 19 ms, faster than 31.40% * Memory Usage: 41.2 MB, less than 30.53% */class Solution { public int lengthOfLongestSubstringKDistinct(String s, int k) { HashMap<Character, Integer> map = new HashMap<>(); in.原创 2021-10-14 09:22:12 · 124 阅读 · 0 评论 -
1004. Max Consecutive Ones III [Medium]
/** * Runtime: 15 ms, faster than 5.99% * Memory Usage: 77.2 MB, less than 6.37% */class Solution { public int longestOnes(int[] nums, int k) { int left = 0, res = 0; LinkedList<Integer> queue = new LinkedList<>(); .原创 2021-10-14 06:00:55 · 86 阅读 · 0 评论 -
547. Number of Provinces [Medium]
/** * dfs * Runtime: 0 ms, faster than 100.00% * Memory Usage: 39.9 MB, less than 68.63% */class Solution { public int findCircleNum(int[][] isConnected) { int groupCount = 0, numsCount = isConnected.length; boolean[] grouped = ne.原创 2021-10-13 23:41:53 · 64 阅读 · 0 评论 -
776. Split BST [Medium]
感觉这道题很难想,看discuss才做出来/** * Runtime: 0 ms, faster than 100.00% * Memory Usage: 37.8 MB, less than 34.15% */class Solution { public TreeNode[] splitBST(TreeNode root, int target) { if (root == null) { return new TreeNode[2];原创 2021-10-10 08:42:39 · 80 阅读 · 0 评论 -
259. 3Sum Smaller [Medium]
/** * Runtime: 3 ms, faster than 99.71% * Memory Usage: 38.5 MB, less than 67.37% */class Solution { public int threeSumSmaller(int[] nums, int target) { Arrays.sort(nums); int res = 0; for (int i = 0; i < nums.length .原创 2021-10-10 00:25:21 · 51 阅读 · 0 评论 -
347. Top K Frequent Elements [Medium]
/** * 自己的代码,用了比较复杂的数据结构:优先队列保存Map.Entry<Integer, Integer> * Runtime: 9 ms, faster than 87.62% * Memory Usage: 41.7 MB, less than 54.73% */class Solution { public int[] topKFrequent(int[] nums, int k) { int[] res = new int[k]; .原创 2021-10-08 10:32:48 · 75 阅读 · 0 评论 -
239. Sliding Window Maximum [Hard]
/** * monotonic queue */class Solution { public int[] maxSlidingWindow(int[] nums, int k) { int[] res = new int[nums.length - k + 1]; LinkedList<Integer> queue = new LinkedList<>(); // to sotre and update the index of po.原创 2021-10-08 10:00:10 · 78 阅读 · 0 评论 -
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts [Medium]
/** * Runtime: 13 ms, faster than 91.31% * Memory Usage: 48.6 MB, less than 93.62% */class Solution { public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) { int preH = 0, preV = 0, maxH = 1, maxV = 1; Arrays.s.原创 2021-10-08 08:53:58 · 61 阅读 · 0 评论 -
1636. Sort Array by Increasing Frequency [Easy]
这道题最重要的是善用Collections比较器!/** * Runtime: 6 ms, faster than 63.89% * Memory Usage: 39.4 MB, less than 42.07% */class Solution { public int[] frequencySort(int[] nums) { HashMap<Integer, Integer> map = new HashMap<>();原创 2021-10-05 21:40:42 · 134 阅读 · 0 评论 -
1836. Remove Duplicates From an Unsorted Linked List [Medium]
/** * Two Pass Solution * 自己的代码,看discussion也都是这样做的 * 不知道为什么时间空间效率却显示没有超过多少人 * Runtime: 144 ms, faster than 21.47% * Memory Usage: 164.9 MB, less than 21.17% */class Solution { public ListNode deleteDuplicatesUnsorted(ListNode head) { Ha.原创 2021-10-05 13:05:42 · 96 阅读 · 0 评论 -
238. Product of Array Except Self [Medium]
/** * 自己的代码 * Runtime: 1 ms, faster than 100.00% * Memory Usage: 49.4 MB, less than 90.80% */class Solution { public int[] productExceptSelf(int[] nums) { int len = nums.length; int[] res = new int[len], mul2 = new int[len]; // mu.原创 2021-10-05 12:05:13 · 55 阅读 · 0 评论 -
1041. Robot Bounded In Circle [Medium]
/** * Runtime: 1 ms, faster than 40.00% * Memory Usage: 38.9 MB, less than 8.63% */class Solution { public boolean isRobotBounded(String instructions) { char dir = 'N'; int x = 0, y = 0; for (char c : instructions.toCharArr.原创 2021-10-05 10:50:51 · 92 阅读 · 0 评论 -
532. K-diff Pairs in an Array [Medium]
/** * one pass with HashMap * Runtime: 7 ms, faster than 80.92% * Memory Usage: 39.3 MB, less than 66.53% */class Solution { public int findPairs(int[] nums, int k) { Map<Integer, Integer> m = new HashMap<>(); int cnt.原创 2021-09-27 01:32:23 · 139 阅读 · 0 评论 -
109. Convert Sorted List to Binary Search Tree [Medium]
/** * 自己的代码 * Runtime: 0 ms, faster than 100.00% * Memory Usage: 39.6 MB, less than 88.37% */class Solution { public TreeNode sortedListToBST(ListNode head) { int cnt = 0; // count the number of nodes in the list ListNode p = hea.原创 2021-09-26 02:28:53 · 79 阅读 · 0 评论 -
108. Convert Sorted Array to Binary Search Tree [Easy]
/** * Runtime: 0 ms, faster than 100.00% * Memory Usage: 38.5 MB, less than 90.28% */class Solution { public TreeNode sortedArrayToBST(int[] nums) { return constructor(nums, 0, nums.length - 1); } private TreeNode constructor(int[].原创 2021-09-26 02:29:41 · 74 阅读 · 0 评论 -
729. My Calendar I [Medium]
/** * 最简单但效率很低的做法 * Runtime: 109 ms, faster than 14.10% * Memory Usage: 51.8 MB, less than 26.79% */class MyCalendar { List<int[]> intervals; public MyCalendar() { intervals = new ArrayList(); } public boolean boo.原创 2021-09-26 01:07:53 · 61 阅读 · 0 评论 -
542. 01 Matrix [Medium]
/** * discuss看的,这题必须从左上到右下一次,再从右下到左上一次,否则无法让每个点获得全部的access * Runtime: 7 ms, faster than 82.69% * Memory Usage: 42.3 MB, less than 59.65% */class Solution { public int[][] updateMatrix(int[][] mat) { int m = mat.length, n = mat[0].length;.原创 2021-09-25 13:30:56 · 46 阅读 · 0 评论 -
299. Bulls and Cows [Medium]
/** * Runtime: 18 ms, faster than 11.23% * Memory Usage: 39 MB, less than 67.38% */class Solution { public String getHint(String secret, String guess) { int a = 0, b = 0; char[] sec = secret.toCharArray(); char[] gue = gues.原创 2021-09-24 22:57:27 · 54 阅读 · 0 评论 -
17. Letter Combinations of a Phone Number [Medium]
/** * Runtime: 5 ms, faster than 31.46% * Memory Usage: 39.3 MB, less than 35.17% */class Solution { public List<String> letterCombinations(String digits) { List<String> res = new ArrayList(); if (digits.length() == 0) .原创 2021-09-24 13:01:19 · 60 阅读 · 0 评论 -
543. Diameter of Binary Tree [Easy]
/** * Runtime: 0 ms, faster than 100.00% * Memory Usage: 39.1 MB, less than 52.72% */class Solution { public int diameterOfBinaryTree(TreeNode root) { int[] max = new int[1]; depth(root, max); return max[0]; } priva.原创 2021-09-24 12:27:10 · 69 阅读 · 0 评论 -
616. Add Bold Tag in String [Medium]
class Solution { public String addBoldTag(String s, String[] words) { // find all intervals List<int[]> intervals = new ArrayList(); for (int i = 0; i < words.length; i++) { int idx = s.indexOf(words[i]), l.原创 2021-09-22 20:53:35 · 85 阅读 · 0 评论 -
978. Longest Turbulent Subarray [Medium]
/** * 自己的做法,用downFlg标志当前的判断是否需要是下降or上升来保持和前面的连续 * Runtime: 5 ms, faster than 49.88% * Memory Usage: 42.2 MB, less than 84.99% */class Solution { public int maxTurbulenceSize(int[] arr) { int len = arr.length, curr = 1, max = 1; Bo.原创 2021-09-15 23:17:34 · 67 阅读 · 0 评论 -
83. Remove Duplicates from Sorted List [Easy]
/** * Runtime: 1 ms, faster than 28.08% * Memory Usage: 41.2 MB, less than 5.44% */class Solution { public ListNode deleteDuplicates(ListNode head) { if (head == null || head.next == null) { return head; } ListN.原创 2021-09-09 03:19:42 · 201 阅读 · 0 评论 -
82. Remove Duplicates from Sorted List II [Medium]
/** * Runtime: 0 ms, faster than 100.00% * Memory Usage: 38 MB, less than 93.09% */class Solution { public ListNode deleteDuplicates(ListNode head) { ListNode sentinel = new ListNode(-1), pre = sentinel, curr = head; while (curr !=.原创 2021-09-09 03:01:54 · 165 阅读 · 0 评论 -
57. Insert Interval [Medium]
System.arraycopy()方法,“目标下标+copy长度 == 目标数组长度”时不会报错,但“目标下标+copy长度 > 目标数组长度” 或 “copy长度 < 0”时,会报错)/** * Runtime: 1 ms, faster than 98.53% * Memory Usage: 41.3 MB, less than 65.40% */class Solution { public int[][] insert(int[][] intervals, i.原创 2021-08-15 01:17:22 · 73 阅读 · 0 评论 -
54. Spiral Matrix [Medium]
/** * 自己的代码 * 每次都读矩阵第一行,然后构造一个新的去掉第一行、逆时针旋转的矩阵,直到矩阵元素全部被去掉 * Runtime: 0 ms, faster than 100.00% * Memory Usage: 38.8 MB, less than 5.21% */class Solution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> res .原创 2021-08-14 19:48:22 · 80 阅读 · 0 评论 -
50. Pow(x, n) [Medium]
自己没做出来,想用一个循环累乘x得到结果,但是会exceed time limitdiscuss中的解答:递归调用myPow()方法,每一层判断自身的n还剩多少如果剩余0直接返回1(任何数的0次方为1)否则先判断n是否为负数,如果为负数将其转换为正数,将x转换为自身的倒数(本题testcase中不会出现0的negative power),为了防止Integer.MIN_VALUE转为正数溢出,转换时应该先变为(n + 1)再转换为正数:1/x * myPow(1/x, -(n + 1));原创 2021-08-14 13:51:03 · 64 阅读 · 0 评论 -
49. Group Anagrams [Medium]
/** * 自己的代码1 * 用map记录每个字符集合对应的strs中字符串的list * 将每个字符串中的字符sort,得到的array转换为String,作为map的key * 时间复杂度O(nlogn) * Runtime: 11 ms, faster than 35.81% * Memory Usage: 53.3 MB, less than 5.68% */class Solution { public List<List<String>> gr.原创 2021-08-14 01:19:47 · 59 阅读 · 0 评论 -
35. Search Insert Position [Easy]
/** * 二分查找,注意target可能不存在,要返回应该插入的位置 * Runtime: 0 ms, faster than 100.00% * Memory Usage: 40.4 MB, less than 7.46% */class Solution { public int searchInsert(int[] nums, int target) { return binarySearch(nums, 0, nums.length - 1, target);.原创 2021-08-13 23:39:09 · 62 阅读 · 0 评论 -
159. Longest Substring with At Most Two Distinct Characters [Medium]
plus会员题,和904. Fruit Into Baskets一模一样/** * 滑动窗口法 * 认为'-'不会出现在s中 * 5 ms, 72.82% * 36.8 MB, 94.42% */class Solution { public int lengthOfLongestSubstringTwoDistinct(String s) { int max = 0, curr = 0, countC2 = 0; // countC2代表两个字符中,最近出现的字符原创 2021-08-08 00:21:22 · 94 阅读 · 0 评论 -
904. Fruit Into Baskets [Medium]
/** * 自己的代码 * 遍历寻找只包含两个不同值的substring的长度,并不断更新长度最大值得到结果 * 用nextStart代表下一个substring的开头位置,nextStart应该等于当前substring包含的两个值(也可能是一个)中,最后出现的那个值中,连续部分的第一个的位置 * Runtime: 9 ms, faster than 75.03% * Memory Usage: 81 MB, less than 8.75% */class Solution { p.原创 2021-08-07 22:08:11 · 79 阅读 · 0 评论 -
929. Unique Email Addresses [Easy]
可以锻炼使用java String的split()、replacAll()等方法和正则表达式的题思路很简单,但实现时有坑:用'.'字符分割字符串,不能用s.split("."),因为'.'是特殊字符,而split()是用正则表达式匹配参数这里需要用"\\."或"[.]"作为参数。/** * Runtime: 7 ms, faster than 87.20% * Memory Usage: 39.3 MB, less than 62.20% */class Solution {原创 2021-08-07 01:02:55 · 72 阅读 · 0 评论 -
388. Longest Absolute File Path [Medium]
自己不会做用stack回溯路径本题特别要注意的是,'\t'和'\n'这样的是一个字符,不是两个字符,length、index都只占一位'\t'和'\n'是java中的特殊字符,'\t'代表tab,'\n'代表换行/** * Runtime: 1 ms, faster than 82.93% * Memory Usage: 36.7 MB, less than 91.67% */class Solution { public int lengthLongestPath(.原创 2021-08-05 15:32:13 · 76 阅读 · 0 评论 -
973. K Closest Points to Origin [Medium]
和苏微二面的题有点像,也是按照一个不常规的值来给一组元素做快速排序,找到pivot位置为指定的k时这道题更简单,不需要构造多个容器/** * 对points快速排序,按照每个point(x, y)的 x^2 + y^2的大小 * 找到pivotIdx == k的地方,左边的所有点就是答案 * Runtime: 8 ms, faster than 86.92% * Memory Usage: 47.5 MB, less than 72.21% */class Solution {原创 2021-08-03 00:58:37 · 72 阅读 · 0 评论 -
953. Verifying an Alien Dictionary [Easy]
/** * 自己的代码 * 用map存下order字符串中每个字符和下标的对应关系,方便后面判断两个字符的先后 * 其实没必要,直接用order.indexOf()来找所需字符的下标判断先后即可 * Runtime: 1 ms, faster than 53.19% * Memory Usage: 39.1 MB, less than 24.42% */class Solution { public boolean isAlienSorted(String[] words, Stri.原创 2021-08-02 23:28:07 · 76 阅读 · 0 评论 -
421. Maximum XOR of Two Numbers in an Array [Medium]
自己不会做,连解答都看的比较吃力对位运算还是不太熟悉,常见技巧在位运算场景中容易用不出来一姐讲解视频:https://www.bilibili.com/video/BV1pr4y1c7B8?from=search&seid=8501163236722907377/** * Runtime: 248 ms, faster than 35.69% * Memory Usage: 62.3 MB, less than 48.77% */class Solution { .原创 2021-08-02 01:33:06 · 87 阅读 · 0 评论 -
412. Fizz Buzz [Easy]
/** * 自己的代码 * Runtime: 1 ms, faster than 99.72% * Memory Usage: 40.7 MB, less than 20.87% */class Solution { public List<String> fizzBuzz(int n) { List<String> res = new LinkedList(); for (int i = 1; i <= n; i++) .原创 2021-08-01 19:08:13 · 117 阅读 · 0 评论