摘要: class Solution { public int countCornerRectangles(int[][] grid) { if(grid.length < 2 || grid[0].length < 2) return 0; int m = grid.length, n = grid[0] 阅读全文
posted @ 2020-07-08 21:32 Sexyomaru 阅读(261) 评论(0) 推荐(0)
摘要: class Solution { public int removeDuplicates(int[] nums) { int i = 0; for (int n : nums) if (i < 2 || n > nums[i-2]) // 几个元素重复都可以用相同的方法 nums[i++] = n; 阅读全文
posted @ 2020-07-08 21:30 Sexyomaru 阅读(83) 评论(0) 推荐(0)
摘要: class Solution { // 两种二分模版 public boolean searchMatrix(int[][] matrix, int target) { if(matrix.length == 0 || matrix[0].length == 0) return false; int 阅读全文
posted @ 2020-07-08 21:28 Sexyomaru 阅读(113) 评论(0) 推荐(0)
摘要: class Solution { public void setZeroes(int[][] matrix) { int m = matrix.length; if(m == 0) return; int n = matrix[0].length; boolean row0 = false, col 阅读全文
posted @ 2020-07-08 21:27 Sexyomaru 阅读(159) 评论(0) 推荐(0)
摘要: 思路: 把当前目录压入栈中,遇到..弹出栈顶,最后返回栈中元素. class Solution { public String simplifyPath(String path) { String[] strs = path.split("/"); Stack<String> stack = new 阅读全文
posted @ 2020-07-08 21:25 Sexyomaru 阅读(107) 评论(0) 推荐(0)
摘要: class Solution { public boolean isNumber(String s) { s = s.trim(); int n = s.length(); char[] arr = s.toCharArray(); boolean numSeen = false, dotSeen 阅读全文
posted @ 2020-07-08 21:23 Sexyomaru 阅读(160) 评论(0) 推荐(0)
摘要: class Solution { public int mySqrt(int x) { if(x < 2) return x; int l = 0, r = x; while(l < r) { int mid = (l + r) >> 1; if(x / mid < mid) { r = mid; 阅读全文
posted @ 2020-07-08 21:21 Sexyomaru 阅读(147) 评论(0) 推荐(0)
摘要: 本题也可以使用回溯,但耗时太久,这里介绍数学统计的方法 class Solution { public String getPermutation(int n, int k) { StringBuilder sb = new StringBuilder(); boolean[] st = new b 阅读全文
posted @ 2020-07-08 21:11 Sexyomaru 阅读(90) 评论(0) 推荐(0)
摘要: class Solution { List<String> res = new ArrayList<>(); public List<String> addOperators(String num, int target) { dfs(num,0,"",target,0,0); return res 阅读全文
posted @ 2020-07-08 21:07 Sexyomaru 阅读(169) 评论(0) 推荐(0)
摘要: class Solution { public List<TreeNode> generateTrees(int n) { if(n == 0) return new ArrayList<>(); return build(1,n); } public List<TreeNode> build(in 阅读全文
posted @ 2020-07-08 21:03 Sexyomaru 阅读(101) 评论(0) 推荐(0)
摘要: class Solution { public List<Integer> findSubstring(String s, String[] words) { List<Integer> res = new ArrayList<>(); int n = s.length(), m = words.l 阅读全文
posted @ 2020-07-08 21:01 Sexyomaru 阅读(159) 评论(0) 推荐(0)
摘要: 解法一: 动态规划 class Solution { public int longestValidParentheses(String s) { int n = s.length(); if(n < 2) return 0; char[] arr = s.toCharArray(); int[] 阅读全文
posted @ 2020-07-08 20:59 Sexyomaru 阅读(191) 评论(0) 推荐(0)
摘要: class Solution { //倍增思想 public int divide(int dividend, int divisor) { if(dividend == 0) return 0; if(dividend == Integer.MIN_VALUE && divisor == - 1) 阅读全文
posted @ 2020-07-08 20:51 Sexyomaru 阅读(235) 评论(0) 推荐(0)
摘要: class Solution { public ListNode reverseKGroup(ListNode head, int k) { if(head == null || head.next == null || k == 1) return head; ListNode node = he 阅读全文
posted @ 2020-07-08 20:45 Sexyomaru 阅读(166) 评论(0) 推荐(0)
摘要: class Solution { public int maxArea(int[] height) { int n = height.length; int l = 0, r = n - 1; int res = 0; while(l < r) { // 每次移动较低的指针,这样不会丢失最大值 re 阅读全文
posted @ 2020-07-08 20:39 Sexyomaru 阅读(91) 评论(0) 推荐(0)
摘要: class Solution { public boolean isMatch(String s, String p) { int m = s.length(), n = p.length(); char[] arrs = s.toCharArray(); char[] arrp = p.toCha 阅读全文
posted @ 2020-07-08 20:38 Sexyomaru 阅读(340) 评论(0) 推荐(0)
摘要: class Solution { public int myAtoi(String str) { str = str.trim(); int n = str.length(); if(n == 0) return 0; char[] arr = str.toCharArray(); int atoi 阅读全文
posted @ 2020-07-08 20:36 Sexyomaru 阅读(174) 评论(0) 推荐(0)
摘要: 本题解法很多,可用动态规划 中心扩展法 这里给出一种最快的方法manacher算法 class Solution { public String longestPalindrome(String s) { char[] arr = manacher(s.toCharArray()); int n = 阅读全文
posted @ 2020-07-08 20:33 Sexyomaru 阅读(109) 评论(0) 推荐(0)
摘要: class Solution { // 本题采用折半删除法 public double findMedianSortedArrays(int[] nums1, int[] nums2) { int k = nums1.length + nums2.length; if(k % 2 == 0) { r 阅读全文
posted @ 2020-07-08 20:25 Sexyomaru 阅读(132) 评论(0) 推荐(0)
摘要: 请注意本题有复杂度要求 O(n) 本题有一种很重要的思想,可以将本类统计问题,想象为上下车问题,我们不需要清楚的统计每个时间点有多少个人,只需要统计上车点上了多少人,下车点下了多少人,之后每个点人数可以表示为f(i) = f(i) + f(i-1) class Solution { public i 阅读全文
posted @ 2020-07-08 20:21 Sexyomaru 阅读(123) 评论(0) 推荐(0)