LeetCode
coder_xiaoyou
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Leetcode 151. 翻转字符串里的单词
Leetcode 151. 翻转字符串里的单词原题连接class Solution { public String reverseWords(String s) { if (s == null || s.length() == 0) { return ""; } StringBuilder builder = new StringBuilder(); int length = s.length();原创 2021-02-08 16:01:42 · 185 阅读 · 0 评论 -
Leetcode 146. LRU 缓存机制
Leetcode 146. LRU 缓存机制原题连接class LRUCache { // 缓存中实际存储的元素个数 private int size; // 缓存的容量 private final int capacity; // 双向链表的虚拟头指针 private final DoubleLinkedNode head; // 双向链表的虚拟尾指针 private final DoubleLinkedNode tail; //原创 2021-02-08 15:14:52 · 164 阅读 · 0 评论 -
Leetcode 318. 最大单词长度乘积
Leetcode 318. 最大单词长度乘积原题链接class Solution { public int maxProduct(String[] words) { int length = words.length; if (length == 1) { return 0; } int result = 0; for (int i = 0; i < words.length; i++原创 2021-02-06 23:54:52 · 234 阅读 · 0 评论 -
Leetcode 268. 丢失的数字
Leetcode 268. 丢失的数字原题链接排序class Solution { public int missingNumber(int[] nums) { Arrays.sort(nums); int length = nums.length; if (nums[length - 1] != length) { return length; } for (int i = 0; i &原创 2021-02-06 22:11:31 · 261 阅读 · 0 评论 -
Leetcode 371. 两整数之和
Leetcode 371. 两整数之和原题链接位运算class Solution { public int getSum(int a, int b) { while (b != 0) { int temp = a ^ b; b = (a & b) << 1; a = temp; } return a; }}递归:class Solutio原创 2021-02-06 21:46:23 · 125 阅读 · 0 评论 -
Leetcode 260. 只出现一次的数字 III
Leetcode 260. 只出现一次的数字 III原题链接位运算class Solution { public int[] singleNumber(int[] nums) { // temp 表示a与b异或的结果(a与b在nums中只出现了依次) int temp = 0; // 对nums中的元素进行全员异或操作,得到a与b异或的结果 for (int num : nums) { temp ^= n原创 2021-02-06 21:31:03 · 195 阅读 · 0 评论 -
Leetcode 190. 颠倒二进制位
Leetcode 190. 颠倒二进制位原题链接位运算class Solution { // you need treat n as an unsigned value public int reverseBits(int n) { // 定义结果 int result = 0; // 最多32位 int count = 32; while (count > 0) { //原创 2021-02-06 15:00:07 · 169 阅读 · 0 评论 -
Leetcode 187. 重复的DNA序列
Leetcode 187. 重复的DNA序列原题链接线性时间窗口切片 + HashSetclass Solution { public List<String> findRepeatedDnaSequences(String s) { int len = 10; Set<String> seen = new HashSet<>(); Set<String> output = new HashSet原创 2021-02-06 14:32:47 · 196 阅读 · 0 评论 -
Leetcode 111. 二叉树的最小深度
Leetcode 111. 二叉树的最小深度原题链接广度优先class Solution { // 内部类,记录节点信息和当前节点的深度 static class QueueNode { TreeNode treeNode; int depth; public QueueNode(TreeNode node, int depth) { this.treeNode = node; this原创 2021-02-03 23:16:37 · 276 阅读 · 0 评论 -
Leetcode 92. 反转链表 II
Leetcode 92. 反转链表 II原题链接/** * 1. 找到第 m 个元素并记录下来,在查找第 m 个元素的同时,需要记录第 m-1 个元素的位置 * 2. 依次反转第 m 个元素与第 m+1 个元素的关系,直到第 n 个元素 * 3. 反转结束后,需要记录下第 n 个元素和第 n+1 个元素的位置 * 4. 处理第 m-1 个元素与第 n 个元素的关系, (m-1).next = n * 5. 处理第 m 个元素与第 n+1 个元素的关系, m.next = (n+1) */原创 2021-02-03 22:20:50 · 152 阅读 · 0 评论 -
Leetcode 90. 子集 II
Leetcode 90. 子集 II原题链接class Solution { List<List<Integer>> result = new ArrayList<>(); int[] nums; int length; public List<List<Integer>> subsetsWithDup(int[] nums) { Arrays.sort(nums); thi原创 2021-02-03 21:41:49 · 117 阅读 · 0 评论 -
Leetcode 79. 单词搜索
Leetcode 79. 单词搜索原题链接class Solution { // 将二维网格的行数定义为全局变量 int boardRowLength; // 将二维网格的列数定义为全局变量 int boardColLength; // 将二维网格定义为全局变量,方便传参 char[][] board; // 将待搜索的单词定义为全局变量 String word; // 将待搜索的单词长度定义为全局变量 int wordL原创 2021-02-01 23:31:18 · 127 阅读 · 0 评论 -
Leetcode 61. 旋转链表
Leetcode 61. 旋转链表原题链接class Solution { public ListNode rotateRight(ListNode head, int k) { // 如果head为null或者k小于等于0,直接返回head,不需要旋转 if (head == null || k <= 0) { return head; } // 新建虚拟头结点 ListNode d原创 2021-01-31 20:42:55 · 138 阅读 · 0 评论 -
Leetcode 59. 螺旋矩阵 II
Leetcode 59. 螺旋矩阵 II原题链接class Solution { public int[][] generateMatrix(int n) { // 定义结果集 int[][] result = new int[n][n]; // 在n阶矩阵中,最多有length个元素 int length = n * n; // 定义旋转方向 int[][] directions = {{0, 1}原创 2021-01-31 19:53:58 · 128 阅读 · 0 评论 -
Leetcode 58. 最后一个单词的长度
Leetcode 58. 最后一个单词的长度原题链接class Solution { public int lengthOfLastWord(String s) { if (s == null) { return 0; } int end = s.length() - 1; // 从后向前遍历,定位第一个不为空的字符 while (end >= 0 && s.charAt原创 2021-01-31 17:30:05 · 96 阅读 · 0 评论 -
Leetcode 56. 合并区间
Leetcode 56. 合并区间原题链接class Solution { public int[][] merge(int[][] intervals) { if (intervals.length == 0) { return new int[0][2]; } // 根据数组中的第一个元素进行升序排序 Arrays.sort(intervals, Comparator.comparingInt(array原创 2021-01-31 17:13:10 · 113 阅读 · 0 评论 -
Leetcode55. 跳跃游戏
Leetcode55. 跳跃游戏原题链接class Solution { public boolean canJump(int[] nums) { //distance表示最远能够到达的位置 int distance = 0; int length = nums.length; // 循环的时候,需要满足 i 小于等于 distance for (int i = 0; i < length &&原创 2021-01-31 16:34:59 · 120 阅读 · 0 评论 -
Leetcode 52. N 皇后 II
Leetcode 52. N 皇后 II原题链接class Solution { int result = 0; // 将 n 皇后的 N 定义为全局变量,减少传参 int nQueue; // columns[i] 表示在第 i 列上是否存在皇后,如果存在,columns[i]为true;否则为false boolean[] columns; // diagonals[i] 表示在第 i 条斜线(左上-->右下)上是否存在皇后,如果存在,diago原创 2021-01-31 16:11:23 · 165 阅读 · 0 评论 -
Leetcode 51. N 皇后
Leetcode 51. N 皇后原题链接class Solution { // 定义结果集合 List<List<String>> result = new ArrayList<>(); // 将 n 皇后的 N 定义为全局变量,减少传参 int nQueue; // queues[i] 表示在第 i 行的第 queues[i] 列上,摆放皇后 int[] queues; // columns[i] 表示在第原创 2021-01-31 16:06:25 · 117 阅读 · 0 评论 -
LeetCode300. 最长递增子序列
LeetCode300. 最长递增子序列题目链接题目描述:给你一个整数数组 nums ,找到其中最长严格递增子序列的长度。子序列是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如,[3,6,2,7] 是数组 [0,3,1,6,2,2,7] 的子序列。示例:输入:nums = [10,9,2,5,3,7,101,18]输出:4解释:最长递增子序列是 [2,3,7,101],因此长度为 4 。题目分析:集合:所有以数字nums[i]结尾的子序列组成的集原创 2020-12-28 11:12:58 · 351 阅读 · 0 评论 -
LeetCode198.打家劫舍
LeetCode198.打家劫舍题目连接题目描述:你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。给定一个代表每个房屋存放金额的非负整数数组,计算你 不触动警报装置的情况下 ,一夜之内能够偷窃到的最高金额。题目分析:集合:状态表示:f(i):表示在前i个数中,所有不选择nums[i]的各种选法所组成的集合中,最大的值g(i):表示在前i个数中,所有原创 2020-12-27 13:41:49 · 121 阅读 · 0 评论 -
LeetCode91. 解码方法
LeetCode91. 解码方法题目链接题目描述:一条包含字母 A-Z 的消息通过以下方式进行了编码:'A' -> 1'B' -> 2...'Z' -> 26给定一个只包含数字的非空字符串,请计算解码方法的总数。题目数据保证答案肯定是一个 32 位的整数。示例:输入:s = “12”输出:2解释:它可以解码为 “AB”(1 2)或者 “L”(12)。分析:状态表示:f(i),所有由前i个数字解码得到的字符串属性:计算解码得到的字符串集合数量集合原创 2020-12-27 13:11:03 · 147 阅读 · 0 评论 -
LeetCode63. 不同路径 II
LeetCode63. 不同路径 II题目链接题目:一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?网格中的障碍物和空位置分别用 1 和 0 来表示。分析:集合:从起点开始,到达(i,j)的所有路径组成的集合状态表示:dp[i][j],从起点到达(i,j)的所有路径的总数属性:计算从起原创 2020-12-25 17:57:36 · 111 阅读 · 0 评论 -
LeetCode120. 三角形最小路径和
LeetCode120. 三角形最小路径和给定一个三角形,找出自顶向下的最小路径和。每一步只能移动到下一行中相邻的结点上。相邻的结点 在这里指的是 下标与上一层结点下标 相同或者等于 上一层结点下标 + 1 的两个结点。例如,给定三角形:[ [2], [3,4], [6,5,7], [4,1,8,3]]自顶向下的最小路径和为 11(即,2 + 3 + 5 + 1 = 11)。集合:从起点到终点的所有路径组成的集合状态表示:dp[i][j],表示从起点到第i层原创 2020-12-25 13:21:44 · 2105 阅读 · 0 评论 -
LeetCode53.最大子序和
LeetCode53.最大子序和给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。示例:输入: [-2,1,-3,4,-1,2,1,-5,4]输出: 6解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。使用动态规划求解最大子序和问题集合:所有的子序列状态表示:f[i],表示以nums[i]结尾的子序列集合中的最大值属性:求每一个子序列的和,然后在所有子序列和中求最大值,result = max(f(0), f(1), f(原创 2020-12-24 20:08:11 · 135 阅读 · 1 评论 -
LeetCode145. 二叉树的后序遍历
LeetCode145. 二叉树的后序遍历非递归:class Solution { public List<Integer> postorderTraversal(TreeNode root) { LinkedList<Integer> result = new LinkedList<>(); Stack<TreeNode> stack = new Stack<>(); if (root原创 2020-12-23 15:53:52 · 112 阅读 · 0 评论 -
Leetcode144. 二叉树的前序遍历
Leetcode144. 二叉树的前序遍历递归:class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> result = new ArrayList<>(); if (root == null) { return result; } // 借助栈完成二叉树的原创 2020-12-23 15:44:23 · 153 阅读 · 0 评论 -
Leetcode108. 将有序数组转换为二叉搜索树
Leetcode108. 将有序数组转换为二叉搜索树class Solution { public TreeNode sortedArrayToBST(int[] nums) { return helper(nums, 0, nums.length - 1); } private TreeNode helper(int[] nums, int left, int right) { // 递归结束条件:left > right原创 2020-12-23 13:37:05 · 126 阅读 · 0 评论 -
Leetcode102. 二叉树的层序遍历
Leetcode102. 二叉树的层序遍历class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> result = new ArrayList<>(); if (root == null) { return result; }原创 2020-12-23 13:27:40 · 151 阅读 · 0 评论 -
Leetcode40. 组合总和 II
Leetcode40. 组合总和 IIclass Solution { List<List<Integer>> result = new ArrayList<>(); List<Integer> path = new ArrayList<>(); public List<List<Integer>> combinationSum2(int[] candidates, int target) {原创 2020-12-23 13:16:22 · 109 阅读 · 0 评论 -
Leetcode387. 字符串中的第一个唯一字符
Leetcode387. 字符串中的第一个唯一字符class Solution { public int firstUniqChar(String s) { if (s == null || "".equals(s.trim())) { return -1; } // 开辟一个数组,记录每个字符出现的次数 int[] array = new int[26]; int length = s.len原创 2020-12-23 11:12:20 · 84 阅读 · 1 评论 -
LeetCode39. 组合总和
LeetCode39. 组合总和class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> result = new ArrayList<>(); if (candidates == null || candidates.length == 0)原创 2020-12-23 09:56:52 · 87 阅读 · 0 评论 -
Leetcode22括号生成
Leetcode 22 括号生成/** * 利用回溯法生成n对正确的括号 */class Solution { public List<String> generateParenthesis(int n) { List<String> result = new ArrayList<>(); backTrack(result, n, 0, 0, 0, new StringBuilder()); return原创 2020-12-22 22:31:53 · 136 阅读 · 0 评论
分享