LeetCode
秋刀鱼不过期
这个作者很懒,什么都没留下…
展开
-
617.合并二叉树
解题思路: 从根节点开始遍历两个树,对应节点合并。 1.对应节点为空,则合并节点也为空。 2.对应节点不为空,则合并节点为两节点之和 class Solution { public TreeNode mergeTrees(TreeNode root1, TreeNode root2) { if(root1==null){ return root2; } if(root2==null){ ..原创 2021-12-10 08:43:06 · 287 阅读 · 0 评论 -
647.回文字符串
解题思路: 枚举出每个字符串的回文中心,然后定义两个指针,分别左右依次遍历。 长度为 n 的字符串会生成 2n-1 组回文中心。(注意回文中心可以是一个字符,两个字符以上,但这里我们只需要定义一个,两个字符即可。当中心字符是三位字符,可以由一位字符遍历依次得到,中心字符是四位时,可以由两位字符遍历一次得到) 代码 class Solution { public int countSubstrings(String s) { int count=0; for(int.原创 2021-12-02 19:46:21 · 183 阅读 · 0 评论 -
739.每日温度
解题思路: 当前下标所对应的温度依次往后遍历,直到第一次比它温度高的下标出现。 两下标相减即可。 class Solution { public int[] dailyTemperatures(int[] temperatures) { int len=temperatures.length; int[] res=new int[len]; for(int i=0;i<len;i++){ int currentTemp.原创 2021-11-29 12:31:00 · 189 阅读 · 0 评论 -
700.二叉搜索树的搜索
二叉搜索树特点: 左子树所有节点的元素值均小于根的元素值; 右子树所有节点的元素值均大于根的元素值; 题解: 若root为空,则返回null; 若val=root.val,则返回root; 若val<root.val,则返回root.left; 若val>root.val,则返回root.right; class Solution { public TreeNode searchBST(TreeNode root, int val) { if(val==roo..原创 2021-11-26 08:44:02 · 149 阅读 · 0 评论 -
349.两个数组的交集
class Solution { public int[] intersection(int[] nums1, int[] nums2) { Set<Integer> set1 = new HashSet<>(); Set<Integer> set2 = new HashSet<>(); for(int i:nums1){ set1.add(i); }.原创 2021-06-06 21:28:54 · 35 阅读 · 0 评论 -
322.零钱兑换
动态规划 public class Solution { public int coinChange(int[] coins, int amount) { int max = amount + 1; int[] dp = new int[amount + 1]; Arrays.fill(dp, max); dp[0] = 0; for (int i = 1; i <= amount; i++) { .原创 2021-06-06 21:22:01 · 39 阅读 · 0 评论 -
344.翻转字符串
class Solution { public void reverseString(char[] s) { int len = s.length; for (int i = 0; i < len/2; i++) { char temp = s[i]; s[i] = s[len-i-1]; s[len-i-1] = temp; } } }原创 2021-06-06 21:07:45 · 35 阅读 · 0 评论 -
338.比特位计数
Brian Kernighan 算法,a与a-1二进制相与,重复此操作,知道a为0,就可得二进制中1的个数 class Solution { public int[] countBits(int n) { int[] bit=new int [n+1]; for(int i=0;i<=n;i++){ bit[i]=countnumber(i); } return bit; } public in.原创 2021-06-01 20:53:53 · 35 阅读 · 0 评论 -
226.反转二叉树
从叶子结点开始翻转,遍历到root节点左右子树都已经翻转之后。在交换两子树的位置 class Solution { public TreeNode invertTree(TreeNode root) { if(root==null){ return null; } TreeNode left=invertTree(root.left); TreeNode right=invertTree(root.right).原创 2021-06-01 20:30:29 · 35 阅读 · 0 评论 -
220.存在重复元素III
用pari容器,类似HashMap存取键和键对象,然后在排序,判断键与键对象是否小于t和k. class Solution { public: bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) { vector<pair<long,long>>hash; for(int i=0;i<nums.size();i++){ .原创 2021-05-30 21:05:46 · 28 阅读 · 0 评论 -
215.数组中的第K个最大元素
快速排序,从大到小。然后在输出 class Solution { public: int findKthLargest(vector<int>& nums, int k) { sort(nums.begin(), nums.end(), greater<int>()); return nums[k - 1]; } }; 看了其他解题方法,了解了一下堆排,以前接触太少了。 class Solution { public.原创 2021-05-30 20:35:14 · 35 阅读 · 0 评论 -
206.反转链表
递归 class Solution { public ListNode reverseList(ListNode head) { if (head == null || head.next == null) { return head; } ListNode newHead = reverseList(head.next); head.next.next = head; head.next = .原创 2021-05-30 20:10:25 · 25 阅读 · 0 评论 -
204.计算质数
打表 class Solution { public int countPrimes(int n) { boolean[] is_primes=new boolean[n+10]; int count=0; for(int i=0;i<n;i++){ is_primes[i]=true; } is_primes[0]=is_primes[1]=false; for(int .原创 2021-05-29 20:33:41 · 34 阅读 · 0 评论 -
203.移除链表元素
方法:第一种情况,要删除的节点不是头结点。找到要删除节点的前一个节点直接pre.next=pre.next.next 第二种情况,要删除的节点是头结点。head=head.next。如果删除后值还是等于要删除节点的值直接循环。 class Solution { public ListNode removeElements(ListNode head, int val) { //删除值相同的头结点后,可能新的头结点也值相等,用循环解决 while(head!=null.原创 2021-05-29 19:26:55 · 24 阅读 · 0 评论 -
200岛屿数量
深度优先搜索,当遇到岛屿时,搜索它四个方位是否为岛屿,如果是则在搜索该岛屿的四个方位,依次进行下去,并把是岛屿的标记为0,知道遇到海水。原创 2021-05-29 19:05:15 · 43 阅读 · 0 评论 -
视图ajax
ajax异步的Javascript 在不重复加载页面的情况下,对页面进行局部的刷新 $.ajax({ ‘url’:请求地址, ‘type’:请求方式 ‘dataType’:预期返回的数据格式 ‘data’;参数 }).success(function(data){ //回调函数 }) ...原创 2021-05-23 21:15:48 · 57 阅读 · 0 评论 -
202.快乐数
class Solution { private int getNext(int n) { int totalSum = 0; while (n > 0) { int d = n % 10; n = n / 10; totalSum += d * d;//平方和相加,并求得下一个数 } return totalSum; } public ..原创 2021-05-23 20:36:19 · 31 阅读 · 0 评论 -
152.乘积最大的子数组
我们可以根据正负性进行分类讨论。 考虑当前位置如果是一个负数的话,那么我们希望以它前一个位置结尾的某个段的积也是个负数,这样就可以负负得正,并且我们希望这个积尽可能「负得更多」,即尽可能小。如果当前位置是一个正数的话,我们更希望以它前一个位置结尾的某个段的积也是个正数,并且希望它尽可能地大。于是这里我们可以再维护一个f min(i),它表示以第 i 个元素结尾的乘积最小子数组的乘积,那么我们可以得到这样的动态规划转移方程: class Solution { public int maxProdu.转载 2021-05-23 20:19:19 · 46 阅读 · 0 评论 -
141.环形链表
用哈希表来存储已经访问过的节点,当我们每次到到这个节点时,如果在哈希表中,就已经时访问过的,就能构成一个环。 public class Solution { public boolean hasCycle(ListNode head) { Set<ListNode> hasCycleSet=new HashSet<ListNode>(); while(head!=null){ if(!hasCy.原创 2021-05-23 20:01:17 · 32 阅读 · 0 评论 -
139.单词拆分
动态规划,把字符串s分割成两部分s[0,j-1]和s[j,i] 看两部分是否合法,dp[j]==true表示s[0,j-1]合法,则只需要考虑s[j,i]是否在wordDict里即可 public class Solution { public boolean wordBreak(String s, List<String> wordDict) { Set<String> word = new HashSet(wordDict); boole.原创 2021-05-23 19:43:30 · 28 阅读 · 0 评论 -
137.只出现一次的数字
利用哈希映射统计数组中每个元素的出现次数。每个键值对,键表示一个元素,值表示其出现的次数。 class Solution { public int singleNumber(int[] nums) { Map<Integer,Integer> map=new HashMap<Integer,Integer>(); for(int num:nums){ map.put(num,map.getOrDefault(num,0.原创 2021-05-23 19:18:30 · 34 阅读 · 0 评论 -
129.求根节点到叶节点数字之和
深度优先搜索,从根节点开始,遍历每个节点,如果遇到叶子节点,则将叶子节点对应的数字加到数字之和。如果当前节点不是叶子节点,则计算其子节点对应的数字,然后对子节点递归遍历。 class Solution { public int sumNumbers(TreeNode root) { return dfs(root, 0); } public int dfs(TreeNode root, int preSum) { if (root == null).原创 2021-05-23 17:31:39 · 46 阅读 · 0 评论 -
111.二叉树的最小深度
思路深度搜索出每一个节点的深度,对每一个非叶子节点,计算出它左右节点的最小深度。 class Solution { public int minDepth(TreeNode root) { if(root==null){ return 0; } if(root.left==null&&root.right==null){ return 1; } in.原创 2021-05-23 17:14:31 · 28 阅读 · 0 评论 -
141.二叉树展开为链表
思路是先序遍历,先根节点root,然后左子树,最后右子树,存入链表中。然后使得前一个根节点的左节点为空pre.left=null,右节点为下一个即可,pre.right=curr即可。 ```java class Solution { public void flatten(TreeNode root) { List<TreeNode> list=new ArrayList<TreeNode>(); PreTraversal(root,li.原创 2021-05-23 16:50:02 · 32 阅读 · 0 评论 -
198 打家劫舍
动态规划 当房屋k小于2时,偷取最高金额的一家 当房屋k大于2时分两种情况: 1,偷窃第 k间房屋,那么就不能偷窃第k−1 间房屋,偷窃总金额为前 k−2 间房屋的最高总金额与第 k间房屋的金额之和。 2,不偷窃第k间房屋,偷窃总金额为前 k−1 间房屋的最高总金额。 处。 class Solution { public int rob(int[] nums) { if(nums==null) return 0; int len=nums.l.原创 2021-05-16 17:04:55 · 40 阅读 · 0 评论 -
155 最小栈
用栈实现栈 当一个元素要入栈时,我们取当前辅助栈的栈顶存储的最小值,与当前元素比较得出最小值,将这个最小值插入辅助栈中; 当一个元素要出栈时,我们把辅助栈的栈顶元素也一并弹出; 在任意一个时刻,栈内元素的最小值就存储在辅助栈的栈顶元素中 class MinStack { Deque<Integer> xStack; Deque<Integer> minStack; public MinStack() { minStack = new Lin.原创 2021-05-16 12:54:56 · 44 阅读 · 0 评论