自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(103)
  • 收藏
  • 关注

原创 【LeetCode146】LRU缓存

思路及启发思路:HashMap + 双向链表双向链表可以在O(1)的时间复杂度内,得到最近访问过的节点,和最久访问过的节点。如果使用单链表的话,只可能在一个方向达到O(1)的时间复杂度。【版本1】class DNode{ int key; int val; DNode pre; DNode next; public DNode() { } public DNode(int k, int v) { key = k; val = v; } public D

2022-05-01 13:30:36 418

原创 【LeetCode 128】最长连续序列

public int longestConsecutive(int[] nums) { int ret = 0; int n = nums.length; Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < n; i++) map.put(nums[i], i); ...

2022-04-17 21:04:14 455

原创 【LeetCode 819】最常见的单词

思路难点1:如何将非空格、非字母区分出来→全部替换为空格,利用正则表达式难点2:如何找到出现次数最多,且不在banned中→先找到不在set中的单词,然后再判断其出现的次数是否是最多的 public static String mostCommonWord(String paragraph, String[] banned) { Set<String> set = new HashSet<String>(); Map<String, Integer> map

2022-04-17 16:58:01 285

原创 【华为软件实习机考真题】热词选择

写在前面满分600分,题目分为100,200,300分,该题目是100分题;没有现场AC,后续花了2个小时完成,涉及到很多东西还不是特别熟悉。题目描述给出多篇文章,根据下列标准选择出topN个热词。标准1:出现次数越多,权值越大,排名越靠前;出现在题目中权值+3,出现在文本中权值+1;标准2:权值相同的词,出现在题目中,排名优先于出现在文本中;标准3:权值相同且均出现在题目或者文本中,出现越早,排名越靠前。下面给出M篇文章,每一篇文章题目占一行,文本占一行,即一篇文章2*M行,输出前topN

2022-04-07 17:11:40 863

原创 【LeetCode 560】和为 K 的子数组

思路前缀和 public static int subarraySum(int[] nums, int k) { int ret = 0; Map<Integer, Integer> map = new HashMap<Integer, Integer>(); // 和 : 出现次数 int sum = 0; map.put(0, 1); for (int i = 0; i < nums.length; i++)

2022-04-06 14:45:05 425

原创 【LeetCode 310】最小高度树

思路深度优先搜索LeetCode没有全部AC,卡在了第53个测试用例,原因是超时。 public static List<Integer> findMinHeightTrees(int n, int[][] edges) { int[][] tree = graph(n, edges); Map<Integer, List<Integer>> deepList = new HashMap<Integer, List<Integer&g

2022-04-06 11:08:22 291

原创 【LeetCode 1004】最大连续1的个数 III

思路滑动窗口,一次通过! public int longestOnes(int[] nums, int k) { int maxL = 0; int start = 0; int end = 0; int cnt = 0; while (end < nums.length) { if (nums[end] == 0) cnt++; while (cnt > k) {

2022-04-05 18:34:43 343

原创 【LeetCode 200】岛屿数量

思路深度优先搜索(一次做出来的!) public static int numIslands(char[][] grid) { int ret = 0; // grid是一个m行n列的矩阵 int m = grid.length; int n = grid[0].length; boolean[][] visited = new boolean[m][n]; int[] directions = {-1, 0, 1};

2022-04-05 15:52:55 305

原创 【LeetCode 547】省份数量

思路1深度优先搜索 public int findCircleNum1(int[][] isConnected) { int provinces = isConnected.length; boolean[] visited = new boolean[provinces]; int circles = 0; for (int i = 0; i < provinces; i++) { if (!visit

2022-04-05 14:23:37 325

原创 【LeetCode 204】计数质数

思路参考题解中的埃氏筛方法。 public int countPrimes(int n) { int[] isPrime = new int[n]; // 最多是n个质数 Arrays.fill(isPrime, 1); int ans = 0; for (int i = 2; i < n; ++i) { if (isPrime[i] == 1) { // 1表示是质数 a

2022-04-05 12:03:10 306

原创 【LeetCode 191】二进制位1的个数

public int hammingWeight(int n) { int cnt = 0;// 方法1: 从低到高依次判断n的二进制的每一位是否为1 while (n != 0) { cnt += (n & 1); n >>= 1; }// 方法2: 从低到高依次将n的二进制的每一位上从1变成0 while (n != 0) ...

2022-04-05 11:12:29 218

原创 【LeetCode 100】相同的树

深度优先搜索 public boolean isSameTree(TreeNode p, TreeNode q) { if (p == null && q == null) return true; if (p == null || q == null) return false; if (p.val != q.val) return false; return isSameTree(p.left, q.left)

2022-03-31 15:16:55 452

原创 【LeetCode 94】二叉树的中序遍历

迭代 public static List<Integer> inorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<Integer>(); Deque<TreeNode> stk = new LinkedList<TreeNode>(); TreeNode t = root; stk.push(new TreeNode());

2022-03-31 13:14:39 832

原创 Java Scanner中的next(), nextLine(), nextInt()

JavaScanner中的next()Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.【注1】这里的delimiter pattern默认是正则表达式:\p{javaWhitespace}+,表示匹配任何空白字符,因此next()读取的内容默认去掉前后的空白符号

2022-03-30 20:13:54 1192

原创 【LeetCode 583】两个字符串的删除操作

字符串,动态规划

2022-03-29 14:30:16 1100

原创 【LeetCode 718】最长重复子数组

字符串,动态规划

2022-03-28 22:22:26 415

原创 【LeetCode 658】找到 K 个最接近的元素

参考题解,使用双指针,由于数组本身是有序的,最后的答案也需要有序,因此从数组的头和尾一起收缩,最后的区间长度为k。 public static List<Integer> findClosestElements2(int[] arr, int k, int x) { int size = arr.length; int left = 0; int right = size - 1; int

2022-03-08 16:27:55 251 3

原创 【LeetCode 611】有效三角形的个数

public static int triangleNumber(int[] nums) { int ret = 0; Arrays.sort(nums); int n = nums.length; for (int i = 0; i < n-1; i++) for (int j = i + 1; j < n; j++) { // range (nums[j] - nums[i], nums[i] + nums[j])

2022-02-27 15:56:37 66

原创 【LeetCode 275】H 指数 II

public static int hIndex(int[] citations) { int n = citations.length; if (citations[n - 1] == 0) return 0; int l = 0; int r = n - 1; while (l < r) { // find the citations[mid] <= n - mid int mid = l + (...

2022-02-25 15:42:17 159

原创 【LeetCode 69】x 的平方根

思路:二分查找算法参考题解,这里mid的取法值得注意。 public static int mySqrt(int x) { int left = 0; int right = x / 2 + 1 ; // 否则会超时 // 在区间 [left..right] 查找目标元素 while (left < right) { int mid = left + (right - left) / 2; // mid取

2022-02-15 12:23:22 80

原创 【LeetCode 66】加一

public static int[] plusOne(int[] digits) { int l = digits.length; int[] ret = new int[l+1]; int remain = (digits[l-1] + 1) / 10; digits[l-1] = (digits[l-1] + 1) % 10; if (remain == 0) return digits; // 最后一位<9 ...

2022-02-08 17:45:49 278

原创 【LeetCode 65】有效数字

参考答案,学习了自动机的相关知识。 public int make(char c) { switch(c) { case ' ': return 0; case '+': case '-': return 1; case '.': return 3; case 'E': case 'e': return 4; defa

2022-01-20 18:09:02 150

原创 【LeetCode 64】最小路径和

有点简单… public int minPathSum(int[][] grid) { int row = grid.length; int col = grid[0].length; for (int i = 1; i < row; i++) grid[i][0] += grid[i-1][0]; for (int j = 1; j < col; j++) grid[0][j] += grid[0][j-1];

2022-01-18 18:19:33 123

原创 【LeetCode 63】不同路径 II

基础解法 public static int uniquePathsWithObstacles(int[][] obstacleGrid) { int m = obstacleGrid.length; int n = obstacleGrid[0].length; if (obstacleGrid[0][0] == 1 || obstacleGrid[m-1][n-1] == 1) return 0; int[][] retMatrix = new

2022-01-18 16:53:02 49

原创 【LeetCode 62】不同路径

参考题目解析给出阶乘计算的方法class Solution { public int uniquePaths(int m, int n) { //只跟第几行第几列有关,从m+n-2步中抽出m-1步 long ans=1; for(int i=0;i<Math.min(m-1,n-1);i++){ ans*=m+n-2-i; ans/=i+1;

2022-01-18 14:26:28 4098

原创 【LeetCode 61】旋转链表

稍微有点问题…public static ListNode rotateRight(ListNode head, int k) { if (k == 0) return head; int count = 0; ListNode tmp = head; while (tmp != null) { count++; tmp = tmp.next; } if (count == 0) retu

2022-01-13 18:45:24 77

原创 【LeetCode 60】排列序列

LeetCode 60

2022-01-13 15:08:19 103

原创 【LeetCode 58】最后一个单词的长度

LeetCode 58

2022-01-06 09:45:10 45

原创 【LeetCode 57】插入区间

思路1 public int[][] insert(int[][] intervals, int[] newInterval) { if (intervals.length == 0) return new int[][]{newInterval}; // find start position 以logn的时间查找 int start = newInterval[0]; // 复制一个新的数组 List<int[]> ret

2022-01-05 10:38:24 126

原创 【LeetCode 56】合并区间

LeetCode 56

2022-01-04 20:30:14 222

原创 【LeetCode 54,59】螺旋矩阵

LeetCode 54, 59

2022-01-04 15:28:50 215

原创 【LeetCode 53】最大子数组和

【LeetCode 53】

2022-01-04 10:30:31 355

原创 【LeetCode 49】字母异位词分组

比较复杂的方法。 public static List<List<String>> groupAnagrams(String[] strs) { List<List<String>> list = new ArrayList<List<String>>(); int n = strs.length; if (n == 0) return null; int[] pointers

2021-12-31 14:27:17 182

原创 【LeetCode 48】旋转图像

public void rotate(int[][] matrix) { // put upside down int rows = matrix.length; int cols = matrix[0].length; for (int r = 0; r < rows / 2; r++) { for (int c = 0; c < cols; c++) { int tmp = matrix[r][c...

2021-12-31 09:05:32 115

原创 【LeetCode 46】【LeetCode 47】全排列

public static List<List<Integer>> permute(int[] nums) { List<Integer> ret = new ArrayList<Integer>(); List<List<Integer>> list = new ArrayList<List<Integer>>(); boolean[] read = new boolean...

2021-12-30 16:19:00 346

原创 【LeetCode 45】跳跃游戏 II

public static int jump2(int[] nums) { int end = 0; int maxPosition = 0; int steps = 0; for(int i = 0; i < nums.length - 1; i++){ //找能跳的最远的 maxPosition = Math.max(maxPosition, nums[i] + i); ...

2021-12-30 15:55:08 1678

原创 【LeetCode 43】字符串相乘

public static String multiply(String num1, String num2) { String ret = ""; int n1 = num1.length(); int n2 = num2.length(); // use num2 to multiply num1 if (n1 < n2) { String tmp = num1; num1 = num2; ...

2021-12-26 15:09:48 186

原创 【LeetCode41】缺失的第一个正数

 根据参考答案给出的一点提示,完成代码。 public static int firstMissingPositive(int[] nums) { int ret = 1; boolean flag = false; int posCount = 0; for (int i = 0; i < nums.length; i++) if (nums[i] > 0) posCount++; for (int i = 0

2021-12-23 20:43:21 163

原创 【LeetCode 40】组合总和

public static List<List<Integer>> combinationSum2(int[] candidates, int target) { List<List<Integer>> list = new ArrayList<List<Integer>>(); Arrays.sort(candidates); List<Integer> ret = new Array...

2021-12-23 15:42:48 196

原创 【LeetCode 39】组合总和

 参考题解 public List<List<Integer>> combinationSum(int[] candidates, int target) { int len = candidates.length; List<List<Integer>> res = new ArrayList<>(); if (len == 0) { return res;

2021-12-22 10:01:34 49

空空如也

空空如也

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

TA关注的人

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