leetcode
艾斯比的日常
简单,快乐,分享
展开
-
LeetCode 387. 字符串中的第一个唯一字符
https://leetcode-cn.com/problems/first-unique-character-in-a-string/ 思路 使用 HashMap 维护元素的出现次数, key 是元素, value 是出现次数 遍历数组, 根据当前元素获取 map 的出现次数, 如果为 1 就返回当前索引 public int firstUniqChar(String s) { // 存储字符:出现次数的映射 Map<Character, Integer>原创 2022-03-05 13:35:14 · 233 阅读 · 0 评论 -
LeetCode 171. Excel 表列序号
https://leetcode-cn.com/problems/excel-sheet-column-number/ 思路: 26 进制转 10 进制 每次将原结果 x 26 字符可以相减, 表示 acsii 的 int 的减法 +1 表示索引从 1 开始 /* 26 进制转 10 进制 不是从 0 开始, 需要加 1 */ public int titleToNumber(String columnTitle) { int result = 0原创 2022-02-20 16:38:41 · 6763 阅读 · 0 评论 -
LeetCode 118. 杨辉三角
https://leetcode-cn.com/problems/pascals-triangle/ 规律: 每行的第一个和最后一个都是 1 其他元素索引值为 i, 那么该元素值是上一行的 i - 1 值加上 i 值 内循环的结束条件要包含等于的情况 public List<List<Integer>> generate(int numRows) { List<List<Integer>> list = new ArrayList<原创 2022-02-19 20:28:13 · 149 阅读 · 0 评论 -
LeetCode 94. 二叉树的中序遍历
https://leetcode-cn.com/problems/binary-tree-inorder-traversal/ 中序遍历: 左根右 public List<Integer> inorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<>(); inorder(root, list); return list; }原创 2022-02-17 20:47:06 · 7910 阅读 · 0 评论 -
LeetCode 344. 反转字符串
https://leetcode-cn.com/problems/reverse-string/ 思路: 双指针, 头指针和尾指针 当头指针索引小于尾指针索引时, 交换值 /** * 双指针 * @param s */ public void reverseString(char[] s) { int first = 0; int last = s.length - 1; while (first < last原创 2022-02-15 21:10:25 · 7386 阅读 · 0 评论 -
LeetCode 237. 删除链表中的节点
https://leetcode-cn.com/problems/delete-node-in-a-linked-list/ 思路 并不真的删除, 而是把要删除的节点的值更新为下个节点值 把要删除节点的 next 更新为 .next.next public void deleteNode(ListNode node) { node.val = node.next.val; node.next = node.next.next; } ...原创 2022-02-14 01:39:29 · 179 阅读 · 0 评论 -
LeetCode 234. 回文链表
https://leetcode-cn.com/problems/palindrome-linked-list/ 思路 链表元素值存入 list 双指针 (0 开始, 末尾开始) 判断值是否相等 /** 链表值加入集合 双指针 */ public boolean isPalindrome(ListNode head) { List<Integer> list = new ArrayList<>();原创 2022-02-14 01:01:51 · 156 阅读 · 0 评论 -
LeetCode 70. 爬楼梯
https://leetcode-cn.com/problems/climbing-stairs/ 动态规划 当前楼梯的方法数 = 前一阶楼梯的方法数 + 前前一阶楼梯的方法数 f (n) = f(n - 1) + f(n - 2) 状态转移: 遍历, 逐个计算当前楼梯的方法数 public int climbStairs(int n) { int p = 0, q = 0, r = 1; for (int i = 0; i < n; i++) {原创 2022-02-11 20:47:36 · 552 阅读 · 0 评论 -
LeetCode 53. 最大子数组和
https://leetcode-cn.com/problems/maximum-subarray/ 两个变量: 当前找到的最大值 (默认 0 元素), 用于累计的求和值 求和值如果小于 0, 则抛弃, 从新开始计算 sum, 否则求和值自加 刷新当前找到的最大值 public int maxSubArray(int[] nums) { int max = nums[0]; int sum = 0; for (int num : nums) {原创 2022-02-10 21:50:30 · 832 阅读 · 0 评论 -
LeetCode 543. 二叉树的直径
https://leetcode-cn.com/problems/diameter-of-binary-tree/ 二叉树的直径, 可以理解为左子树的深度加上右子树的深度 当前节点的深度为 左/右子树深度 + 1 int ans = 0; public int diameterOfBinaryTree(TreeNode root) { depth(root); return ans; } public int depth(TreeNode root)原创 2022-02-09 20:46:50 · 202 阅读 · 0 评论 -
LeetCode 101. 对称二叉树
https://leetcode-cn.com/problems/symmetric-tree/ public boolean isSymmetric(TreeNode root) { return check(root, root); } public boolean check(TreeNode left, TreeNode right) { if (left == null && right == null) {原创 2022-02-08 20:13:47 · 828 阅读 · 0 评论 -
LeetCode 160. 相交链表
https://leetcode-cn.com/problems/intersection-of-two-linked-lists/ public ListNode getIntersectionNode(ListNode headA, ListNode headB) { ListNode list1 = headA; ListNode list2 = headB; while (list1 != list2) { list1 = (原创 2022-02-07 00:37:16 · 285 阅读 · 0 评论 -
LeetCode 283. 移动零
https://leetcode-cn.com/problems/move-zeroes/ /* 双指针 左指针 已经处理好的 右指针 待处理的 */ public void moveZeroes(int[] nums) { int length = nums.length; int left = 0; int right = 0; while (right < leng原创 2022-02-05 15:50:59 · 256 阅读 · 0 评论 -
LeetCode 169. 多数元素
https://leetcode-cn.com/problems/majority-element/ 摩尔投票法 声明2个变量: 当前的多数元素, 计数器 count 为 0 更新当前多数元素 如果当前值和当前的多数元素相同, count 自增 1, 否则自减 1 /* 摩尔投票法 */ public int majorityElement(int[] nums) { int result = 0; int count = 0; for原创 2022-02-04 18:50:53 · 603 阅读 · 0 评论 -
LeetCode 617. 合并二叉树
https://leetcode-cn.com/problems/merge-two-binary-trees/ 核心思路 二叉树 DFS 的前序遍历 public TreeNode mergeTrees(TreeNode root1, TreeNode root2) { if (root1 == null) { return root2; } if (root2 == null) { return r原创 2022-02-02 11:14:37 · 355 阅读 · 0 评论 -
LeetCode 104. 二叉树的最大深度
https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/ 给定一个二叉树,找出其最大深度。 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。 说明: 叶子节点是指没有子节点的节点 思路 递归 比较左节点的层次和右节点的层次 +1 表示当前层次的计数 public int maxDepth(TreeNode root) { if (root == null) { return 0;原创 2022-01-31 00:33:52 · 435 阅读 · 0 评论 -
leetcode 226. 翻转二叉树
https://leetcode-cn.com/problems/invert-binary-tree/ 思路 递归调用翻转当前节点的左节点和翻转当前节点的右节点 调用完成后, 再翻转当前节点的左右节点 public TreeNode invertTree(TreeNode root) { if (root == null) { return null; } TreeNode left = invertTree(root.left原创 2022-01-28 20:05:59 · 314 阅读 · 0 评论 -
Leetcode 141. 环形链表
https://leetcode-cn.com/problems/linked-list-cycle/ 思路 快慢指针 循环条件注意避免空指针异常, 需要判断 fast != null && fast.next != null /* 思路: 快慢指针 */ public boolean hasCycle(ListNode head) { if (head == null) { return false; }原创 2022-01-23 22:08:07 · 272 阅读 · 0 评论 -
leetcode 1. 两数之和
https://leetcode-cn.com/problems/two-sum/ 思路 有两种方式 双层遍历, 时间复杂度 O (n^2) 使用 map 存储, key 是元素, value 是索引. 时间复杂度 O (n) /* 思路 O(n) map.containsKey key 放 num , value 索引 其他解法: 双层循环 O(n^2) */ public int[] twoSum(int[原创 2022-01-23 02:27:23 · 1759 阅读 · 0 评论 -
leetcode 14. 最长公共子串
题目 编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 “”。 https://leetcode-cn.com/problems/longest-common-prefix/ 代码 public String longestCommonPrefix(String[] strs) { // 临界值判定 if (strs == null || strs.length == 0) { return ""; }原创 2021-12-01 19:03:32 · 254 阅读 · 0 评论 -
leetcode 21. 合并两个有序链表
题目 将两个升序链表合并为一个新的 升序 链表并返回. 新链表是通过拼接给定的两个链表的所有节点组成的. https://leetcode-cn.com/problems/merge-two-sorted-lists/ 思路 链表为空的临界值判断 声明一个假头节点, 维护当前指针 循环判断当两个链表都不为空时, 进行 next 赋值, 并且移动原链表和新链表指针 循环结束后, 判断链表是否为空, 继续把剩下的链表接上 最终返回假头节点的 next 代码 public ListNode mergeTw原创 2021-11-28 00:07:46 · 223 阅读 · 0 评论 -
leetcode 53. 最大子序和
题目 https://leetcode-cn.com/problems/maximum-subarray/ 给定一个整数数组 nums , 找到一个具有最大和的连续子数组(子数组最少包含一个元素), 返回其最大和. 思路 动态规划 维护当前总和 和 最大总和 如果当前总和大于 0, 那么继续累计, 否则重新赋值 默认当前总和是 0, 最大总和是数组的 0 元素 将当前总和和最大总和比较, 更大的值赋值给最大总和, 最终会返回该结果 代码 public int maxSubArray(int[] nu原创 2021-11-26 01:46:23 · 36 阅读 · 0 评论 -
排序 - 冒泡排序
冒泡排序 思想 相邻元素比较. 如果左侧比右侧大, 那么互换位置 前段无序, 后段有序, 即排好序的元素都堆到了末尾 /** * */ @Test public void test() { int[] arr = {1, 6, 4, 2}; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length - i - 1; j原创 2021-11-18 00:55:11 · 70 阅读 · 0 评论 -
leetcode9.回文数
回文数 题目 给你一个整数 x , 如果 x 是一个回文整数, 返回 true ; 否则, 返回 false . 回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 例如, 121 是回文, 而 123 不是. 分析 负数总是 false 把目标数值整数反转, 得到的值和原值比较即可 代码 class Solution { public boolean isPalindrome(int x) { if (x < 0) { return false;原创 2021-11-05 00:08:30 · 4211 阅读 · 0 评论 -
leetcode 7 整数反转
题目 https://leetcode-cn.com/problems/reverse-integer/ 给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。 如果反转后整数超过 32 位的有符号整数的范围 [−231, 231 − 1] , 就返回 0. 假设环境不允许存储 64 位整数(有符号或无符号). 思路 对自身整除 10, 得到整数为抹掉最后一位的数值 对自身取余 10, 得到最后一位 通过乘以 10 实现每次向左移位 循环结束的条件是当前值不为 0 如果强转为 i原创 2021-11-03 21:56:50 · 3196 阅读 · 0 评论 -
leetcode 575. 分糖果
Alice 有 n 枚糖, 其中第 i 枚糖的类型为 candyType [i] . Alice 注意到她的体重正在增长, 所以前去拜访了一位医生. 医生建议 Alice 要少摄入糖分, 只吃掉她所有糖的 n / 2 即可(n 是一个偶数). Alice 非常喜欢这些糖, 她想要在遵循医生建议的情况下, 尽可能吃到最多不同种类的糖. 给你一个长度为 n 的整数数组 candyType , 返回: Alice 在仅吃掉 n / 2 枚糖的情况下, 可以吃到糖的最多种类数. class Solution原创 2021-11-02 23:23:21 · 78 阅读 · 0 评论