
leetcode_java
宫城诗
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
20200714——第一千五百一十二题 好数对的数目
class Solution { public int numIdenticalPairs(int[] nums) { int count = 0; HashMap<Integer,Integer> hashmap = new HashMap(); for(int i = 0 ;i<nums.length;++i){ if(hashmap.containsKey(nums[i])){原创 2020-07-14 00:02:08 · 199 阅读 · 0 评论 -
20200713——第二十二题 括号生成
class Solution { public List<String> generateParenthesis(int n) { List<String> list = new ArrayList(); if(n<=0){ return list; } Core("",n,n,list); return list; } public void原创 2020-07-13 23:37:38 · 95 阅读 · 0 评论 -
20200713——第七十八题 子集
class Solution { List<List<Integer>> lists = new ArrayList(); public List<List<Integer>> subsets(int[] nums) { if(nums == null){ return lists; } Core(0,new ArrayList(),nums); retur原创 2020-07-13 23:25:00 · 160 阅读 · 0 评论 -
20200713——第四十六题 全排列
class Solution { List<List<Integer>> lists = new ArrayList(); public List<List<Integer>> permute(int[] nums) { if(nums == null){ return lists; } dfs(0,nums); return lists; }原创 2020-07-13 23:18:57 · 161 阅读 · 0 评论 -
202020713——第五百八十一题 最短无序连续子数组
class Solution { public int findUnsortedSubarray(int[] nums) { int[] newnums = nums.clone(); Arrays.sort(newnums); int start = nums.length,end = 0; for(int i = 0 ;i<nums.length;++i){ if(nums[i] != newnums[原创 2020-07-13 22:19:04 · 105 阅读 · 0 评论 -
20200712——第一百九十八题 打家劫舍
class Solution { public int rob(int[] nums) { int max = 0 ,pre = 0; for(int i = 0 ;i<nums.length;++i){ int temp = max; max = Math.max(pre+nums[i],temp); pre =temp; } return max;原创 2020-07-12 19:57:02 · 113 阅读 · 0 评论 -
20200712——第五百四十三题 二叉树的直径
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { int ans = 1; public int diameterOfBinaryTree(TreeNode ro原创 2020-07-12 18:24:22 · 100 阅读 · 0 评论 -
20200712——第五十三题 最大子序和
class Solution { public int maxSubArray(int[] nums) { int max = Integer.MIN_VALUE; int sum = 0 ; for(int i = 0 ;i<nums.length;++i){ if(sum < 0){ sum = 0; } sum +=nums[i];原创 2020-07-12 17:43:08 · 108 阅读 · 0 评论 -
20200712—— 第一百零一题 对称二叉树
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public boolean isSymmetric(TreeNode root) { if(root原创 2020-07-12 17:35:56 · 112 阅读 · 0 评论 -
20200712——第一百五十五题 最小栈
class MinStack { public Stack<Integer> Stack1; public Stack<Integer> Stack2; /** initialize your data structure here. */ public MinStack() { Stack1 = new Stack<>(); Stack2 = new Stack<>(); }原创 2020-07-12 17:00:05 · 105 阅读 · 0 评论 -
20200712——第四百三十七题 路径总和三
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public int pathSum(TreeNode root, int sum) { int cou原创 2020-07-12 02:46:22 · 95 阅读 · 0 评论 -
20200711——第一百六十题 相交链表
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public ListNode getIntersectionNod原创 2020-07-11 23:01:02 · 199 阅读 · 0 评论 -
20200708——第二百八十三题 移动零
class Solution { public void moveZeroes(int[] nums) { int j = 0; for(int i = 0;i<nums.length;++i){ if(nums[i] != 0){ if(i != j){ nums[j] = nums[i]; nums[i] = 0;原创 2020-07-08 23:18:26 · 139 阅读 · 0 评论 -
20200708——第五百三十八题 把二叉搜索树转为累加树
第一次做的时候用的很麻烦的方法,现在重新刷到这道题仔细思考一下。二叉搜索树,右边的大,左边的小,中间的刚刚好。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { pri原创 2020-07-08 22:55:10 · 102 阅读 · 0 评论 -
20200707——第二十一题 合并两个有序链表
/** * 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 = next; } * }原创 2020-07-07 22:55:40 · 115 阅读 · 0 评论 -
20200707——第一百零四题 二叉树的最大深度
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public int maxDepth(TreeNode root) { if(root == null原创 2020-07-07 22:37:11 · 126 阅读 · 0 评论 -
20200707——第二百二十七题 翻转二叉树
没什么难度/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public TreeNode invertTree(TreeNode root) { if原创 2020-07-07 22:07:02 · 118 阅读 · 0 评论 -
20200415——第一百四十六题 LRU缓存机制
import java.util.Hashtable;/** * @Classname LRUCache * @Description TODO * @Date 2020/4/15 11:40 * @Created by mmz */public class LRUCache { class DlinkedNode{ int key; ...原创 2020-04-15 14:29:48 · 110 阅读 · 0 评论 -
20200413——第四百零六题 根据身高重建队列
没想到这种题应该怎么去做,看完官方的题解来一点自己的思路。先安排个子高的人,因为个子矮的人相对于个子高的人来说没有任何影响。就比如7,0 5,07身高的人在5的前面后面其实都没有影响,没有身高大于他的。所以先排列身高高的人,在去排列身高的低的人。package question406_根据身高重建队列;import java.util.*;/** * @Classname ...原创 2020-04-13 18:42:14 · 130 阅读 · 0 评论 -
20200413——第二百三十八题 除自身以外数组的乘积
第一种方式,虽然题目上说了不让用除法。我还是想用除法做一下。就是把所有的数的乘积为all然后分别去除每一个数这样是不对的因为要考虑的0的事情。如果有零,很难处理,如果不止一个零呢。这样if要判断好多,不是正解。0 1 2 3 4 5 0 总乘积是0直接否了。第二种package question238_除自身以外数组的乘积;/** * @Classname Soluti...原创 2020-04-13 14:06:34 · 98 阅读 · 0 评论 -
20200413——第三百三十八题 比特位计数
巩固基础集合与数组的转换数组转换成集合第一种方法 遍历添加int arrs[] = {1, 2}; //1.遍历 List<Integer> list = new ArrayList<>(); for (int ele : arrs) { list.add(ele); } ...原创 2020-04-13 13:26:07 · 148 阅读 · 0 评论 -
20200412——第一千零二十五题 除数博弈
原题爱丽丝和鲍勃一起玩游戏,他们轮流行动。爱丽丝先手开局。最初,黑板上有一个数字 N 。在每个玩家的回合,玩家需要执行以下操作:选出任一 x,满足 0 < x < N 且 N % x == 0 。 用 N - x 替换黑板上的数字 N 。如果玩家无法执行这些操作,就会输掉游戏。只有在爱丽丝在游戏中取得胜利时才返回 True,否则返回 false。假设两个玩家都以最佳状态参与...原创 2020-04-12 20:56:26 · 103 阅读 · 0 评论 -
20200409——第九十八题 验证二叉搜索树
思路思路很简单,用中序遍历整个二叉搜索树,把每个节点的值加入到一个ArrayList中,然后对ArrayList中的元素进行遍历,从0~length-2,如果前一个元素比后一个元素大就不是二叉搜索树。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNo...原创 2020-04-09 12:16:05 · 91 阅读 · 0 评论 -
20200408——第一百三十九题 单词拆分
class Solution { public boolean wordBreak(String s, List<String> wordDict) { return Core(s,new HashSet(wordDict),0); } public boolean Core(String s,Set<String> wordDic...原创 2020-04-08 22:52:49 · 220 阅读 · 0 评论 -
20200408——第三十九题 组合总和
class Solution { List<List<Integer>> lists = new ArrayList<>(); public List<List<Integer>> combinationSum(int[] candidates, int target) { if(candidates ==...原创 2020-04-08 19:05:21 · 112 阅读 · 0 评论 -
20200408——第三十四题 在排序数组中查找元素的第一个和最后一个
class Solution { public int[] searchRange(int[] nums, int target) { if(nums == null){ return new int[]{-1,-1}; } int first = find(true,nums,target); int...原创 2020-04-08 17:22:27 · 130 阅读 · 0 评论 -
20200408——第三十三题 搜索旋转排序数组
class Solution { public int search(int[] nums, int target) { int length = nums.length; int left = 0; int right = length-1; int mid = 0; while(left<=right...原创 2020-04-08 16:26:44 · 90 阅读 · 0 评论 -
20200406——第二百零八题 实现Trie
class Trie { private boolean is_string = false; private Trie[] next = new Trie[26]; /** Initialize your data structure here. */ public Trie() { } /** Inserts a word into...原创 2020-04-06 22:29:02 · 100 阅读 · 0 评论 -
20200406——第二百题 岛屿数量
class Solution { public int numIslands(char[][] grid) { if(grid == null ||grid.length ==0){ return 0; } int nr = grid.length; int nc =grid[0].length; ...原创 2020-04-06 22:05:44 · 120 阅读 · 0 评论 -
20200406——第一百四十八题 排序链表
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode sortList(Li...原创 2020-04-06 21:47:20 · 110 阅读 · 0 评论 -
20200406——第一百四十二题 环形链表二
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class ...原创 2020-04-06 19:04:35 · 136 阅读 · 0 评论 -
20200406——第七十六题 最小覆盖字串
class Solution { public static String minWindow(String s, String t) { if(s == null || t== null||s.length() < t.length()){ return ""; } int[] window = new ...原创 2020-04-06 17:40:15 · 189 阅读 · 0 评论 -
20200406——第四百九十四题 目标和
class Solution { int count = 0; public int findTargetSumWays(int[] nums, int S) { calucate(nums,0,0,S); return count; } public void calucate(int[] nums,int i,int sum,in...原创 2020-04-06 16:06:17 · 102 阅读 · 0 评论 -
20200406——第五百六十题 和为k的子数组
第一种暴力法class Solution { public int subarraySum(int[] nums, int k) { int count = 0; for(int i = 0;i<nums.length;++i){ for(int j = i+1;j<=nums.length;++j){ ...原创 2020-04-06 15:04:12 · 99 阅读 · 0 评论 -
20200406——第六百二十一 任务调度器
排序由于相同的任务之间必须有 n 的冷却时间,所以我们可以想到按照任务的数量来安排它们,即一种任务的出现次数越多,我们就越早地安排。例如有 5 种任务 A, B, C, D, E,且它们分别有 6, 1, 1, 1, 1 个时,假设冷却时间 n = 2,那么我们首先安排任务 A,随后在 2 单位的冷却时间里,我们安排任务 B, C,随后继续安排任务 A,再安排任务 D, E,以此类推。因此我们...原创 2020-04-06 14:42:56 · 136 阅读 · 0 评论 -
20200406——第六百四十七题 回文子串
动态规划如果给定一个区间,i,j 字符串如果a[i] == a[j] ,那么就判断a[i+i] 与a[j-1]是否相等一直到偶数或者奇数形式,判断这个字符串是不是回文字符串。状态:dp[i][j] 表示字符串s在[i,j]区间的子串是否是一个回文串。状态转移方程:当 s[i] == s[j] && (j - i < 2 || dp[i + 1][j - 1]) 时,...原创 2020-04-06 12:36:05 · 128 阅读 · 0 评论 -
20200406——第七百三十九题 每日温度
这道题也就是看后面多少天之后,会超过当前的这个温度。class Solution { public int[] dailyTemperatures(int[] T) { int[] temperature = new int[T.length]; for(int i = 0 ;i<T.length;++i){ int temp ...原创 2020-04-06 12:07:52 · 130 阅读 · 0 评论 -
20200403——第五四十三题 二叉树的直径
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { int ...原创 2020-04-03 20:01:19 · 104 阅读 · 0 评论 -
20200403——第五百八十一题 最短无序连续子数组
class Solution { public int findUnsortedSubarray(int[] nums) { int[] newnums =nums.clone(); Arrays.sort(newnums); int start = nums.length,end = 0; for (int i = 0; i...原创 2020-04-03 19:49:54 · 152 阅读 · 0 评论 -
20200403——第四百六十一题 汉明距离
class Solution { public int hammingDistance(int x, int y) { int result = x^y; int count = 0; while(result != 0){ if((result & 1) == 1){ coun...原创 2020-04-03 15:15:15 · 109 阅读 · 0 评论