自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

汋灼的博客

小白取经路

  • 博客(178)
  • 收藏
  • 关注

原创 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 169

原创 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 151

原创 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 155

原创 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 146

原创 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 90

原创 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 69

原创 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 87

原创 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 55

原创 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 79

原创 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 81

原创 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 65

原创 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 137

原创 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 101

原创 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 58

原创 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 101

原创 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 142

原创 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 79

原创 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 83

原创 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 63

原创 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 50

原创 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 57

原创 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 64

原创 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 76

原创 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 90

原创 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 70

原创 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 217

原创 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 168

原创 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 75

原创 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 84

原创 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 66

原创 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 62

原创 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 65

原创 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 109

原创 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 85

原创 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 77

原创 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 84

原创 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 76

原创 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 79

原创 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 91

原创 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 119

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除