随笔分类 -  LeetCode

摘要: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+ 阅读全文
posted @ 2020-10-30 15:11 peanut_zh 阅读(67) 评论(0) 推荐(0)
摘要:class Solution { public int countBinarySubstrings(String s) { //定义 last变量 代表 前一种数字的数量 int last = 0; //定义 cur变量 代表 当前数字的数量 int cur = 1; //定义 res变量,统计结果 阅读全文
posted @ 2020-10-28 23:51 peanut_zh 阅读(101) 评论(0) 推荐(0)
摘要:class Solution { public boolean isPalindrome(int x) { //边界判断 //首先X不能为负, 0可以是回文数,如果最后一位为0,那么要求X第一位也为0,这显然是不可能的 if(x < 0 || (x % 10 == 0 && x != 0)) ret 阅读全文
posted @ 2020-10-28 22:57 peanut_zh 阅读(78) 评论(0) 推荐(0)
摘要://中心拓展法 class Solution { public int countSubstrings(String s) { //定义一个变量 统计有多少个回文子串 int res = 0; //定义回文子串的中心点,可能是一个,也可能是2个 比如aba、abba //这里为什么是 2 * s.l 阅读全文
posted @ 2020-10-28 22:30 peanut_zh 阅读(95) 评论(0) 推荐(0)
摘要://回文串,偶数个的字符可够成回文串,然后最中间的字符可为奇数个 class Solution { public int longestPalindrome(String s) { //定义一个大小写字母的计数器,统计每个字母出现的次数 int[] count = new int[128]; //遍 阅读全文
posted @ 2020-10-28 21:21 peanut_zh 阅读(66) 评论(0) 推荐(0)
摘要://方法一,排序,将字符串s、t 分别按字母的升降序排序,比较s、t是否相等 //方法二,使用哈希表 class Solution { public boolean isAnagram(String s, String t) { //如果俩个字符串长度不等,直接返回 if(s.length() != 阅读全文
posted @ 2020-10-28 20:46 peanut_zh 阅读(85) 评论(0) 推荐(0)
摘要:当我按照官方的思路写出代码,提交后并未通过,查看错误,发现算法错误的将[2147483647,-2147483648]也视为连续的整数了,这是因为我没有考虑到int类型的边界。将代码稍加修改,即成功提交 //哈希表,建议看官方的题解,尤其是演示动画 class Solution { public i 阅读全文
posted @ 2020-10-28 17:35 peanut_zh 阅读(100) 评论(0) 推荐(0)
摘要://通过哈希表来查重 class Solution { public boolean containsDuplicate(int[] nums) { Set<Integer> set = new HashSet<>(); for(int i = 0;i < nums.length;i++){ if( 阅读全文
posted @ 2020-10-28 16:21 peanut_zh 阅读(78) 评论(0) 推荐(0)
摘要:class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer,Integer> hashmap = new HashMap<>(); int n = nums.length; for(int i = 0; i < 阅读全文
posted @ 2020-10-28 16:10 peanut_zh 阅读(68) 评论(0) 推荐(0)
摘要:class Solution { public int[] nextGreaterElements(int[] nums) { //结果集 int len = nums.length; int[] res = new int[len]; Arrays.fill(res,-1); //栈,保存下标 S 阅读全文
posted @ 2020-10-28 15:57 peanut_zh 阅读(45) 评论(0) 推荐(0)
摘要://暴力法 // class Solution { // public int[] dailyTemperatures(int[] T) { // // // int n = T.length; // int [] end = new int[n]; // for(int i = 0;i < n; 阅读全文
posted @ 2020-10-28 15:50 peanut_zh 阅读(65) 评论(0) 推荐(0)
摘要:class Solution { public boolean isValid(String s) { //首先边界判断,如果是奇数个 直接返回flase if(s.length() % 2 == 1){ return false ; } //将(){} []存到一个HashMap 中 这样查找用O 阅读全文
posted @ 2020-10-28 15:14 peanut_zh 阅读(81) 评论(0) 推荐(0)
摘要:class MinStack { //定义一个数据栈s1,一个最小元素栈minStack Stack<Integer> s1,minStack; /** initialize your data structure here. */ public MinStack() { s1 = new Stac 阅读全文
posted @ 2020-10-28 15:06 peanut_zh 阅读(77) 评论(0) 推荐(0)
摘要:class MyStack { //定义一个队列 Queue<Integer> queue = new LinkedList<>(); /** Initialize your data structure here. */ public MyStack() { } /** Push element 阅读全文
posted @ 2020-10-28 14:36 peanut_zh 阅读(74) 评论(0) 推荐(0)
摘要:class MyQueue { //定义俩个栈 private Stack<Integer> s1 = new Stack<>(); private Stack<Integer> s2 = new Stack<>(); //队首元素 private int front = 0; /** Initia 阅读全文
posted @ 2020-10-28 14:16 peanut_zh 阅读(73) 评论(0) 推荐(0)
摘要:class Solution { //定义一个众数的最大数量值 int maxCount = 0; //定义一个当前众数节点的数量 int count = 0; //定义当前众数节点的值 int cur = 0; //存放众数的集合 List<Integer> list = new ArrayLis 阅读全文
posted @ 2020-10-28 01:20 peanut_zh 阅读(76) 评论(0) 推荐(0)
摘要://利用二叉搜索树中序遍历得到一个升序序列,可衍生出多种问题,注意举一反三 //1.此题计算任意俩节点的最小差值,根据二叉搜索树的性质,这俩个节点一定是相邻节点,父子关系 //2.定义一个存放最小值得变量,用于返回结果 //3.根据中序遍历的过程,定义一个变量存放上一次遍历的节点,用于和本次遍历的节 阅读全文
posted @ 2020-10-28 00:32 peanut_zh 阅读(98) 评论(0) 推荐(0)
摘要://又是考察树的遍历的熟练度,首先可以想到,我们先把树遍历完,把结果集放到一个容器里,再判断2数之和能否等于给定值 //官方题解 用了HashSet,我觉得不错,顺着思路写一个 /* class Solution { public boolean findTarget(TreeNode root, 阅读全文
posted @ 2020-10-27 22:34 peanut_zh 阅读(109) 评论(0) 推荐(0)
摘要://方法1,将有序链表的元素全部保存到一个List,就将此问题转化为了108题 //方法2,直接找链表的中心点,(876题),剩下的就和108的思想一样了,本人用方法二解题 class Solution { public TreeNode sortedListToBST(ListNode head) 阅读全文
posted @ 2020-10-27 21:50 peanut_zh 阅读(58) 评论(0) 推荐(0)
摘要:class Solution { public TreeNode sortedArrayToBST(int[] nums) { //二叉搜索树(又称二叉排序树) 的中序遍历 可得到一个升序序列,又平衡二叉树 中所有节点的左右子树的高度差绝对值不超过1. //根据这个升序序列构建平衡二叉树,根节点为序 阅读全文
posted @ 2020-10-27 17:30 peanut_zh 阅读(62) 评论(0) 推荐(0)