Loading

摘要: 最长回文子串 题目 中等 和最长回文子序列类似 自己的做法: class Solution { public String longestPalindrome(String s) { int len = s.length(); int max = 1; int left = 0, right = 0 阅读全文
posted @ 2023-07-08 14:45 幻梦翱翔 阅读(11) 评论(0) 推荐(0) 编辑
摘要: 每日温度 题目 中等 什么时候用单调栈呢? 通常是一维数组,要寻找任一个元素的右边或者左边第一个比自己大或者小的元素的位置,此时我们就要想到可以用单调栈了。 class Solution { public int[] dailyTemperatures(int[] temperatures) { i 阅读全文
posted @ 2023-02-11 11:40 幻梦翱翔 阅读(24) 评论(0) 推荐(0) 编辑
摘要: 斐波那契数 题目 简单 class Solution { public int fib(int n) { if (n < 2) return n; int a = 0, b = 1, c = 0; for (int i = 1; i < n; i++) { c = a + b; a = b; b = 阅读全文
posted @ 2023-01-28 13:55 幻梦翱翔 阅读(36) 评论(0) 推荐(0) 编辑
摘要: 分发饼干 题目 简单 这里的局部最优就是大饼干喂给胃口大的,充分利用饼干尺寸喂饱一个,全局最优就是喂饱尽可能多的小孩。 class Solution { // 思路:优先考虑胃口,先喂饱大胃口 public int findContentChildren(int[] g, int[] s) { Ar 阅读全文
posted @ 2023-01-08 11:03 幻梦翱翔 阅读(61) 评论(0) 推荐(0) 编辑
摘要: 组合 题目 中等 class Solution { List<List<Integer>> result = new ArrayList<>(); LinkedList<Integer> path = new LinkedList<>(); public List<List<Integer>> co 阅读全文
posted @ 2022-12-31 11:32 幻梦翱翔 阅读(22) 评论(0) 推荐(0) 编辑
摘要: 二叉树的递归遍历 前序 中序 后序 简单 // 前序遍历·递归·LC144_二叉树的前序遍历 class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> result = new Arr 阅读全文
posted @ 2022-12-10 14:23 幻梦翱翔 阅读(19) 评论(0) 推荐(0) 编辑
摘要: 用栈实现队列 题目 简单 把 pop() 和 peek() 中可复用的部分提取出来 class MyQueue { Stack<Integer> stackIn; Stack<Integer> stackOut; /** Initialize your data structure here. */ 阅读全文
posted @ 2022-12-06 10:58 幻梦翱翔 阅读(23) 评论(0) 推荐(0) 编辑
摘要: 反转字符串 题目 简单 class Solution { public void reverseString(char[] s) { int l = 0; int r = s.length - 1; while (l < r) { s[l] ^= s[r]; //构造 a ^ b 的结果,并放在 a 阅读全文
posted @ 2022-12-03 15:50 幻梦翱翔 阅读(23) 评论(0) 推荐(0) 编辑
摘要: 有效的字母异位词 题目 简单 /** * 242. 有效的字母异位词 字典解法 * 时间复杂度O(m+n) 空间复杂度O(1) */ class Solution { public boolean isAnagram(String s, String t) { int[] record = new 阅读全文
posted @ 2022-11-30 11:12 幻梦翱翔 阅读(22) 评论(0) 推荐(0) 编辑
摘要: 移除链表元素 题目 简单 注意: 头节点也要删除时的处理方式 下面第三种方法,连续几个都为要删除的节点时要注意 /** * 添加虚节点方式 * 时间复杂度 O(n) * 空间复杂度 O(1) * @param head * @param val * @return */ public ListNod 阅读全文
posted @ 2022-11-24 11:09 幻梦翱翔 阅读(13) 评论(0) 推荐(0) 编辑