自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(47)
  • 问答 (1)
  • 收藏
  • 关注

原创 LeetCode 47. 全排列 II

官方class Solution { boolean[] vis; public List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> ans = new ArrayList<List<Integer>>(); List<Integer> perm = new ArrayList<Int.

2021-02-28 23:44:08 202

原创 LeetCode 40. 组合总和 II

官方// 先排序,记录每个数字的次数,递归调用dfs(pos+1,rest−i×freq[pos][0]),i为使用这个数字多少次class Solution { List<int[]> freq = new ArrayList<int[]>(); List<List<Integer>> ans = new ArrayList<List<Integer>>(); List<Integer> s..

2021-02-24 21:13:33 124

原创 LeetCode 39. 组合总和

//写法一:自己写的 38% 先排序,然后往后顺着往后选class Solution { List<List<Integer>> res; public List<List<Integer>> combinationSum(int[] candidates, int target) { res = new ArrayList<List<Integer>>(); Arrays.sort..

2021-02-24 20:57:02 110

原创 LeetCode 234. 回文链表

//解法一:fast走两格,slow走一个,slow走的时候进行反转前半部分链表/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) {.

2021-02-23 20:22:53 59 1

原创 LeetCode 142. 环形链表 II

官方//fast和slow相遇后 ptr从head出发再与slow相遇就是答案public class Solution { public ListNode detectCycle(ListNode head) { if (head == null) { return null; } ListNode slow = head, fast = head; while (fast != null) { ..

2021-02-23 19:08:12 78

原创 LeetCode 88. 合并两个有序数组

官方题解//从尾部归并排序class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int p1 = m - 1, p2 = n - 1; int tail = m + n - 1; int cur; while (p1 >= 0 || p2 >= 0) { if (p1 == -1) { .

2021-02-22 22:28:19 92

原创 LeetCode 75. 颜色分类

大佬题解//三个区域用两个指针 ,中间为1的地方直接让i++即可,遇到0和2则swapclass Solution { public void sortColors(int[] nums) { int n = nums.length; int p0 = 0; int i = 0; int p2 = n - 1; while(i <= p2){ if(nums[i] == 2){ .

2021-02-22 20:08:16 69

原创 LeetCode 61. 旋转链表

大佬题解//快慢双指针/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next =.

2021-02-22 18:10:05 77

原创 LeetCode 42. 接雨水

官方题解 我拜倒//解法一:暴力枚举寻找某个位置的左边最大和右边最大public int trap(int[] height) { int ans = 0; int size = height.length; for (int i = 1; i < size - 1; i++) { int max_left = 0, max_right = 0; for (int j = i; j >= 0; j--) { //Search the..

2021-02-21 20:39:12 72

原创 LeetCode 30. 串联所有单词的子串

大佬题解//哈希表 + 双指针 + 滑动窗口class Solution { public List<Integer> findSubstring(String s, String[] words) { List<Integer> res = new ArrayList<Integer>(); if(words.length == 0) return res; int n = s.len.

2021-02-21 19:42:44 79

原创 LeetCode 28. 实现 strStr()

//解法一:双指针 55%public int strStr(String haystack, String needle) { int m = haystack.length(), n = needle.length(); if (n == 0) return 0; for (int i = 0; i <= m - n; i++) { for (int j = 0; j < n; j++) { if (haystack.ch.

2021-02-21 19:00:28 107

原创 LeetCode 18. 四数之和

//两个外层循环固定a和b,适当的进行提前判断,里层两个指针往里逼近class Solution { public List<List<Integer>> fourSum(int[] nums, int target) { List<List<Integer>> quadruplets = new ArrayList<List<Integer>>(); if (nums == null || .

2021-02-20 09:46:28 64

原创 LeetCode 16. 最接近的三数之和

//排序。最外层a固定,里层双指针 逼近class Solution { public int threeSumClosest(int[] nums, int target) { int n = nums.length; Arrays.sort(nums); int best = 10000000; for(int first = 0; first < n; first++){ if(first == .

2021-02-20 09:43:46 75

原创 LeetCode 15. 三数之和

//写法一//排序。 第一层循环固定a, 第二层固定b ,c从尾部向左逼近class Solution { public List<List<Integer>> threeSum(int[] nums) { int n = nums.length; Arrays.sort(nums); List<List<Integer>> res = new ArrayList<List<Intege.

2021-02-20 09:41:22 65

原创 LeetCode 11. 盛最多水的容器

//双指针两边逼近,矮的竖线往中间挪class Solution { public int maxArea(int[] height) { int len = height.length; int res = 0; int l = 0; int r = len - 1; while(r != l){ res = Math.max(res, (r - l) * Math.min(height..

2021-02-20 09:32:05 71

原创 LeetCode 421. 数组中两个数的最大异或值

官方class TrieNode { HashMap<Character, TrieNode> children = new HashMap<Character, TrieNode>(); public TrieNode() {}}class Solution { public int findMaximumXOR(int[] nums) { // Compute length L of max number in a binary representa.

2021-02-15 18:24:42 75

原创 LeetCode 336. 回文对

别人的题解class Solution { private TrieNode root; public boolean isPalindrome(String s){ int i=0, j=s.length()-1; while (i < j){ if (s.charAt(i) != s.charAt(j)){ return false; } i+.

2021-02-15 17:47:34 100

原创 LeetCode 212. 单词搜索 II

官方class TrieNode { HashMap<Character, TrieNode> children = new HashMap<Character, TrieNode>(); String word = null; public TrieNode() {}}class Solution { char[][] _board = null; ArrayList<String> _result = new ArrayList<..

2021-02-15 16:49:55 83

原创 LeetCode 211. 添加与搜索单词 - 数据结构设计

class TrieNode{ TrieNode[] child;//记录孩子节点 boolean is_end;//记录当前节点是不是一个单词的结束字母 public TrieNode(){// child = new TrieNode[26];//子节点数组长度26,0:‘a’,1:‘b’..... is_end = false; }}class WordDictionary { TrieNode root;//记录前缀树.

2021-02-15 15:51:48 74

原创 LeetCode 208. 实现 Trie (前缀树)

官方模板class Trie { private TrieNode root; /** Initialize your data structure here. */ public Trie() { root = new TrieNode(); } /** Inserts a word into the trie. */ public void insert(String word) { TrieNode node.

2021-02-15 15:28:29 72

原创 LeetCode 448. 找到所有数组中消失的数字

官方class Solution { public List<Integer> findDisappearedNumbers(int[] nums) { int len = nums.length; for(int i = 0; i < len; i++){ int x = (nums[i] - 1) % len; nums[x] += len; } List<.

2021-02-13 09:44:11 64

原创 通用线段树的写法

class NumArray {public interface Merger<E>{ E merge(E a, E b);}public class SegmentTree<E>{ /* 使用一个数组表示区间. 首先,用户可能要获取区间内某一个的元素,或者获取区间的某一个属性,我们在线段树中创建数组,作为区间数组的的副本,用于给出区间数组的某些属性; 其次,我们想将data数组内的元素(arr数组区间传递进来的)组织成为一个线段树,

2021-02-09 23:35:57 101

原创 LeetCode 307. 区域和检索 - 数组可修改

//解法一:树状数组class NumArray { int n; int[] tree; int[] a; public NumArray(int[] nums) { this.n = nums.length; a = nums; tree = new int[this.n + 1]; for(int i = 1; i <= this.n; i++) add(i, nums[i.

2021-02-09 22:33:13 164

原创 LeetCode 241. 为运算表达式设计优先级

大佬题解class Solution { public List<Integer> diffWaysToCompute(String input) { List<Integer> res = new LinkedList<>();//这里要注意,每一个调用递归函数都会有一个【属于】本次 // 调用的res列表存放数据 // 递归函数传参传进来的是上一层左半部分或者右半部分 for(int i=0;.

2021-02-09 17:29:17 88

原创 LeetCode 282. 给表达式添加运算符

官方写法//解法一:官方解法class Solution { public ArrayList<String> answer; public String digits; public long target; public void recurse( int index, long previousOperand, long currentOperand, long value, ArrayList<String> ops) { Str.

2021-02-09 17:05:55 99

原创 LeetCode 315. 计算右侧小于当前元素的个数

//解法一:分治 用额外的index数组记录下标 根据index归并排序并计算答案 有点慢我日class Solution { int[] res; int[] tmp; int[] index; public List<Integer> countSmaller(int[] nums) { int n = nums.length; res = new int[n]; tmp = new int[n]; .

2021-02-08 21:40:44 93

原创 LeetCode 312. 戳气球

//解法一:dp 左开右开区间写法class Solution { public int maxCoins(int[] nums) { int n = nums.length; int[] arr = new int[n + 2]; arr[0] = arr[n + 1] = 1; for(int i = 1; i <= n; i++) arr[i] = nums[i - 1]; int.

2021-02-08 16:53:32 85

原创 LeetCode 327. 区间和的个数

//解法一:分治 68%class Solution { public int countRangeSum(int[] nums, int lower, int upper) { int n = nums.length; long[] sum = new long[n + 1]; for(int i = 1; i <= n; i++){ sum[i] = sum[i - 1] + nums[i - 1]; .

2021-02-07 21:49:02 92

原创 LeetCode 493. 翻转对

//解法一:分治 真tm慢 才5%class Solution { public int reversePairs(int[] nums) { if(nums.length == 0){ return 0; } return reversePairRecursive(nums, 0, nums.length - 1); } public int reversePairRecursive(int[] nums.

2021-02-07 21:16:25 86

原创 剑指 Offer 36. 二叉搜索树与双向链表

大佬题解/*// Definition for a Node.class Node { public int val; public Node left; public Node right; public Node() {} public Node(int _val) { val = _val; } public Node(int _val,Node _left,Node _right) { val = .

2021-02-07 20:31:53 61

原创 剑指 Offer 39. 数组中出现次数超过一半的数字

分治题解//解法一:分治 不是很快 60%class Solution { public int majorityElement(int[] nums) { return findMode(nums, 0, nums.length - 1); } private int findMode(int nums[], int leftIndex, int rightIndex) { if (leftIndex == rightIndex) { return nums[leftI.

2021-02-07 20:14:35 82

原创 剑指 Offer 40. 最小的k个数

//解法一:分治最快class Solution { public int[] getLeastNumbers(int[] arr, int k) { if(k == 0 || arr.length == 0) return new int[0]; return quickSearch(arr, 0, arr.length - 1, k - 1); } private int[] quickSearch(int[] nums.

2021-02-07 17:18:31 62

原创 剑指 Offer 42. 连续子数组的最大和

题解//解法一:分治class Solution { public int maxSubArray(int[] nums) { return merge(nums, 0, nums.length - 1); } public int merge(int[] nums, int l, int r){ if(l == r) return nums[l]; int mid = l + r >> 1;.

2021-02-07 16:56:53 58

原创 LeetCode 264. 丑数 II

官方题解//解法一:优先队列(最小堆)class Ugly { public int[] nums = new int[1690]; Ugly() { HashSet<Long> seen = new HashSet(); PriorityQueue<Long> heap = new PriorityQueue<Long>(); seen.add(1L); heap.add(1L); long currUgly, n.

2021-02-05 21:33:10 67

原创 LeetCode 218. 天际线问题

大佬题解//解法一:优先队列(PriorityQueue)class Solution { public List<List<Integer>> getSkyline(int[][] buildings) { List<List<Integer>> points = new ArrayList<>(); List<List<Integer>> results = new Arr..

2021-02-05 20:38:33 185

原创 LeetCode 215. 数组中的第K个最大元素

大佬题解//解法一:长度为len的小顶堆,前k个直接插入,后面的元素依次和堆顶比较是否出插入,如果插入就把堆顶先拿掉class Solution { public int findKthLargest(int[] nums, int k) { PriorityQueue<Integer> q = new PriorityQueue<Integer>(k,(a, b)->a - b); for(int i = 0;i < k; .

2021-02-05 19:10:58 67

原创 LeetCode 23. 合并K个升序链表

官方题解解法一:优先队列(无空间优化)/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; thi.

2021-02-05 18:32:32 67

原创 LeetCode 395. 至少有K个重复字符的最长子串

题解//循环26次,没次限定符合条件的不同字符有1,2,3.....26次class Solution { public int longestSubstring(String s, int k) { int res = 0; for(int i = 1; i <= 26; i++){ res = Math.max(res, helper(s, k, i)); } return res; }.

2021-02-03 21:29:49 64

原创 LeetCode 76. 最小覆盖子串

好题解class Solution { public String minWindow(String s, String t) { HashMap<Character, Integer> need = new HashMap<Character, Integer>(); HashMap<Character, Integer> window = new HashMap<>(); for (char c .

2021-02-03 20:31:18 57

原创 LeetCode 480. 滑动窗口中位数

官方题解class Solution { public double[] medianSlidingWindow(int[] nums, int k) { DualHeap dh = new DualHeap(k); for (int i = 0; i < k; ++i) { dh.insert(nums[i]); } double[] ans = new double[nums.length - k .

2021-02-03 07:55:31 84

空空如也

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

TA关注的人

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