自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

原创 LeetCode 148. 排序链表

官方 class Solution { public ListNode sortList(ListNode head) { return sortList(head, null); } public ListNode sortList(ListNode head, ListNode tail) { if (head == null) { return head; } if (head.nex..

2021-03-12 17:17:36 184

转载 字节跳动高频面试题之排序奇升偶降链表

原文链接:字节跳动高频面试题之排序奇升偶降链表_Erazer_Control-CSDN博客 题目描述 给定一个奇数位升序,偶数位降序的链表,将其重新排序。 输入: 1->8->3->6->5->4->7->2->NULL 输出: 1->2->3->4->5->6->7->8->NULL 复制代码 题目分析 按奇偶位置拆分链表,得1->3->5->7->NULL和8->6->

2021-03-10 09:52:23 555

原创 LeetCode 132. 分割回文串 II

官方 class Solution { public int minCut(String s) { int len = s.length(); if(len == 1) return 0; boolean[][] dp = new boolean[len][len]; for(int i = len - 1; i >= 0; i--){ for(int j = i.

2021-03-08 16:35:13 188

原创 LeetCode 133. 克隆图

官方 //1.dfs 用哈希表 记录 哪个node被访问过,遇到被访问的直接返回 /* // Definition for a Node. class Node { public int val; public List<Node> neighbors; public Node() { val = 0; neighbors = new ArrayList<Node>(); } public Node(i...

2021-03-05 23:16:54 103

原创 LeetCode 117. 填充每个节点的下一个右侧节点指针 II

//写法一:类似层次遍历,用到了全局指针last和nextStart。last用来做连接,nextStart是下一行的第一个非空节点 //https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node-ii/solution/tian-chong-mei-ge-jie-dian-de-xia-yi-ge-you-ce-15/ class Solution { Node last = null; No..

2021-03-05 18:36:12 117

原创 LeetCode 116. 填充每个节点的下一个右侧节点指针

//递归写法:把左子树的所有靠右边的节点的next指向右子树的所有靠左边的节点 //https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/solution/cdi-gui-jian-dan-yi-dong-by-ting-yu-8t/ /* // Definition for a Node. class Node { public int val; public Node left; ..

2021-03-05 17:00:15 82

原创 LeetCode 232. 用栈实现队列

官方 //模拟栈 当out为空时把in里面的pop出来push进out in和out都为空才算空 class MyQueue { Stack<Integer> in = new Stack<Integer>(); Stack<Integer> out = new Stack<Integer>(); /** Initialize your data structure here. */ public MyQueue() { ..

2021-03-05 10:55:59 97 1

原创 LeetCode 114. 二叉树展开为链表

大佬 //左右中 先把俩子树拉直,然后把左子树换成右子树,这时原来的左子树变成新的右子树,找到新右子树的最后一个节点连接旧的右子树 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val =..

2021-03-04 23:40:59 81 1

原创 LeetCode 109. 有序链表转换二叉搜索树

官方 //快慢指针找中点,从中点二分建立二叉搜索树 /** * 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.

2021-03-04 23:11:52 112 1

原创 LeetCode 98. 验证二叉搜索树

大佬 //中序遍历时,判断当前节点是否大于中序遍历的前一个节点,如果大于,说明满足 BST,继续遍历;否则直接返回 false。 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = ..

2021-03-04 21:19:03 71 1

原创 剑指 Offer 55 - II. 平衡二叉树 || 110. 平衡二叉树

大佬 //利用递归返回的标记判断 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public boolean isBalanced(TreeNode root) {.

2021-03-04 00:15:50 84 1

原创 剑指 Offer 34. 二叉树中和为某一值的路径 || LeetCode 113. 路径总和 II

大佬 //众所周知,叶子节点是左右都为null doge /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { List<List<Integer>&gt.

2021-03-03 23:23:17 85 1

原创 剑指 Offer 38. 字符串的排列

//用set剪纸不用排序了 class Solution { ArrayList<String> res = new ArrayList<String>(); public String[] permutation(String s) { char[] str = s.toCharArray(); dfs(str.length, 0, str); String[] strings = new String[res.si.

2021-03-03 22:13:48 78 1

原创 LeetCode 338. 比特位计数

//https://leetcode-cn.com/problems/counting-bits/solution/bi-te-wei-ji-shu-by-leetcode-solution-0t1i/ class Solution { public int[] countBits(int num) { int[] res = new int[num + 1]; int high = 0; for(int i = 1; i <= nu...

2021-03-03 08:42:09 96

原创 LeetCode 131. 分割回文串

//动态规划bool数组求出下表之间是否组成回文串,dfs求具体的回文串 class Solution { List<List<String>> res = new ArrayList<>(); public List<List<String>> partition(String s) { int len = s.length(); boolean[][] dp = new boolean[len].

2021-03-02 23:23:49 89

原创 LeetCode 93. 复原 IP 地址

大佬 import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Deque; import java.util.List; import java.util.Stack; public class Solution { public List<String> restoreIpAddresses(String s) { int len = s.length(); ..

2021-03-02 22:47:48 85

原创 LeetCode 89. 格雷编码

大佬 class Solution { List<Integer> res = new ArrayList<Integer>(); public List<Integer> grayCode(int n) { if(n == 0){ res.add(0); return res; }else{ res = grayCode(n - 1); .

2021-03-02 21:18:14 89

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

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

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

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

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

原创 LeetCode 75. 颜色分类

大佬题解 //三个区域用两个指针 ,中间为1的地方直接让i++即可,遇到0和2则swap class 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 60

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

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

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

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

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

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

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

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

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

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

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

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

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

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

原创 通用线段树的写法

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

2021-02-09 23:35:57 88

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

空空如也

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

TA关注的人

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