10 2020 档案
摘要:class Solution { public boolean isPowerOfTwo(int n) { return n > 0 && (n & (n-1)) == 0; } } 这里引用评论区Kico大神的解释。
阅读全文
摘要://异或运算 //1、交换律:a ^ b ^ c <=> a ^ c ^ b //2、任何数于0异或为任何数 0 ^ n => n //3、相同的数异或为0: n ^ n => 0 class Solution { public int singleNumber(int[] nums) { int
阅读全文
摘要://二分法查找 class Solution { public int findDuplicate(int[] nums) { //定义数组左边界的指针 left,右边界指针 right int left = 1; int right = nums.length - 1; while(left <
阅读全文
摘要://采用数组桶 class Solution { public int[] findErrorNums(int[] nums) { //定义一个新数组来统计nums数组中元素出现的次数 int[] count = new int[nums.length + 1]; //统计每个元素出现的次数 for
阅读全文
摘要:/* 左下角的元素是这一行中最小的元素,同时又是这一列中最大的元素。比较左下角元素和目标: 1.若左下角元素等于目标,则找到 2.若左下角元素大于目标,则目标不可能存在于当前矩阵的最后一行,问题规模可以减小为在去掉最后一行的子矩阵中寻找目标 3.若左下角元素小于目标,则目标不可能存在于当前矩阵的第一
阅读全文
摘要:class Solution { public int findMaxConsecutiveOnes(int[] nums) { //定义俩个计数器,max为最大的1的数量,cur为当前1的数量 int max = 0; int cur = 0; //遍历数组,如果数组中的元素为1,cur+1,为0
阅读全文
摘要:class Solution { public void moveZeroes(int[] nums) { //定义一个指针,用来记录数组中非0元素的个数 int j = 0; for(int i = 0;i < nums.length;i++){ if(nums[i] != 0){ nums[j+
阅读全文
摘要:class Solution { public int countBinarySubstrings(String s) { //定义 last变量 代表 前一种数字的数量 int last = 0; //定义 cur变量 代表 当前数字的数量 int cur = 1; //定义 res变量,统计结果
阅读全文
摘要:class Solution { public boolean isPalindrome(int x) { //边界判断 //首先X不能为负, 0可以是回文数,如果最后一位为0,那么要求X第一位也为0,这显然是不可能的 if(x < 0 || (x % 10 == 0 && x != 0)) ret
阅读全文
摘要://中心拓展法 class Solution { public int countSubstrings(String s) { //定义一个变量 统计有多少个回文子串 int res = 0; //定义回文子串的中心点,可能是一个,也可能是2个 比如aba、abba //这里为什么是 2 * s.l
阅读全文
摘要://回文串,偶数个的字符可够成回文串,然后最中间的字符可为奇数个 class Solution { public int longestPalindrome(String s) { //定义一个大小写字母的计数器,统计每个字母出现的次数 int[] count = new int[128]; //遍
阅读全文
摘要://方法一,排序,将字符串s、t 分别按字母的升降序排序,比较s、t是否相等 //方法二,使用哈希表 class Solution { public boolean isAnagram(String s, String t) { //如果俩个字符串长度不等,直接返回 if(s.length() !=
阅读全文
摘要:当我按照官方的思路写出代码,提交后并未通过,查看错误,发现算法错误的将[2147483647,-2147483648]也视为连续的整数了,这是因为我没有考虑到int类型的边界。将代码稍加修改,即成功提交 //哈希表,建议看官方的题解,尤其是演示动画 class Solution { public i
阅读全文
摘要://通过哈希表来查重 class Solution { public boolean containsDuplicate(int[] nums) { Set<Integer> set = new HashSet<>(); for(int i = 0;i < nums.length;i++){ if(
阅读全文
摘要:class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer,Integer> hashmap = new HashMap<>(); int n = nums.length; for(int i = 0; i <
阅读全文
摘要:class Solution { public int[] nextGreaterElements(int[] nums) { //结果集 int len = nums.length; int[] res = new int[len]; Arrays.fill(res,-1); //栈,保存下标 S
阅读全文
摘要://暴力法 // class Solution { // public int[] dailyTemperatures(int[] T) { // // // int n = T.length; // int [] end = new int[n]; // for(int i = 0;i < n;
阅读全文
摘要:class Solution { public boolean isValid(String s) { //首先边界判断,如果是奇数个 直接返回flase if(s.length() % 2 == 1){ return false ; } //将(){} []存到一个HashMap 中 这样查找用O
阅读全文
摘要:class MinStack { //定义一个数据栈s1,一个最小元素栈minStack Stack<Integer> s1,minStack; /** initialize your data structure here. */ public MinStack() { s1 = new Stac
阅读全文
摘要:class MyStack { //定义一个队列 Queue<Integer> queue = new LinkedList<>(); /** Initialize your data structure here. */ public MyStack() { } /** Push element
阅读全文
摘要:class MyQueue { //定义俩个栈 private Stack<Integer> s1 = new Stack<>(); private Stack<Integer> s2 = new Stack<>(); //队首元素 private int front = 0; /** Initia
阅读全文
摘要:class Solution { //定义一个众数的最大数量值 int maxCount = 0; //定义一个当前众数节点的数量 int count = 0; //定义当前众数节点的值 int cur = 0; //存放众数的集合 List<Integer> list = new ArrayLis
阅读全文
摘要://利用二叉搜索树中序遍历得到一个升序序列,可衍生出多种问题,注意举一反三 //1.此题计算任意俩节点的最小差值,根据二叉搜索树的性质,这俩个节点一定是相邻节点,父子关系 //2.定义一个存放最小值得变量,用于返回结果 //3.根据中序遍历的过程,定义一个变量存放上一次遍历的节点,用于和本次遍历的节
阅读全文
摘要://又是考察树的遍历的熟练度,首先可以想到,我们先把树遍历完,把结果集放到一个容器里,再判断2数之和能否等于给定值 //官方题解 用了HashSet,我觉得不错,顺着思路写一个 /* class Solution { public boolean findTarget(TreeNode root,
阅读全文
摘要://方法1,将有序链表的元素全部保存到一个List,就将此问题转化为了108题 //方法2,直接找链表的中心点,(876题),剩下的就和108的思想一样了,本人用方法二解题 class Solution { public TreeNode sortedListToBST(ListNode head)
阅读全文
摘要:class Solution { public TreeNode sortedArrayToBST(int[] nums) { //二叉搜索树(又称二叉排序树) 的中序遍历 可得到一个升序序列,又平衡二叉树 中所有节点的左右子树的高度差绝对值不超过1. //根据这个升序序列构建平衡二叉树,根节点为序
阅读全文
摘要:class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { //递归边界判断 if(root == null || root == p || root == q) ret
阅读全文
摘要:class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { //root节点的值 大于p、q俩个节点,说明公共祖先在根节点的左侧 if(root.val > p.val
阅读全文
摘要://迭代法 class Solution { //定义一个变量,用于返回第K小的元素 int res = 0; //定义计数变量 int count = 0; public int kthSmallest(TreeNode root, int k) { search(root,k); return
阅读全文
摘要://根据根节点的值 与边界的值 进行比较 可得出 // node.val > high,那么修剪后的二叉树必定出现在节点的左边。 // node.val < low,那么修剪后的二叉树出现在节点的右边 // 否则,修剪树的两边。 class Solution { public TreeNode tr
阅读全文
摘要:下面的俩张图是 LeetCode上的题解https://leetcode-cn.com/problems/binary-tree-inorder-traversal/solution/die-dai-fa-by-jason-2/ 我根据思路用Java做了实现,放在了最后。 PS:在学习的过程中,各路
阅读全文
摘要:class Solution { public List<Integer> postorderTraversal(TreeNode root) { //一般解法,前序遍历后,翻转下结果集,注意下 与前序遍历的进栈顺序不一样 //(前序) 根左右 --> 变为 根右左 --> 翻转 左右根 (后续)
阅读全文
摘要:二叉树的前序遍历(迭代法) 1 class Solution { 2 public List<Integer> preorderTraversal(TreeNode root) { 3 //前序遍历非递归法需要借助一个栈 4 Stack<TreeNode> stack = new Stack<>()
阅读全文

浙公网安备 33010602011771号