随笔分类 - leetcode简单题
写一些leetcode简单题,目的是联系算法和js语法。
摘要:链接 https://leetcode.cn/problems/final-prices-with-a-special-discount-in-a-shop/description/ 思路: 单调栈 单调栈,顾名思义,就是在栈内,元素要么是单调递减的,要么是单调递增的。 这个题目要求我们找下一个更小
阅读全文
摘要:链接 https://leetcode.cn/problems/next-greater-element-i/description/ 思路 1. 暴力解法 暴力解法没啥好说的,对于nums1中的元素,先找到其在nums2中的位置,然后往后找比他大的第1个元素就好了。这样的做法是O(m*n)的时间复
阅读全文
摘要:链接 https://leetcode.cn/problems/teemo-attacking/description/ 思路 这题思路其实很简单,需要2个变量,1个记住总数,1个记住之前遍历过的序列能够访问到的最大值。 如果遍历到某个数时,这个最大值比他大,就证明之前序列中的某个加和被浪费掉了,所
阅读全文
摘要:链接: https://leetcode.cn/problems/max-consecutive-ones/description/ 思路: 没啥好说的吧.....基础题目 代码 class Solution: def findMaxConsecutiveOnes(self, nums: List[
阅读全文
摘要:链接 https://leetcode.cn/problems/license-key-formatting/description/ 思路 字符串处理,没啥好说的... 代码 class Solution: def licenseKeyFormatting(self, s: str, k: int
阅读全文
摘要:链接 https://leetcode.cn/problems/number-complement/description/ 思路 没啥好说的,二进制与十进制的互相转换,基本功。 代码 class Solution: def findComplement(self, num: int) -> int
阅读全文
摘要:链接 https://leetcode.cn/problems/island-perimeter/description/ 思路 这题理论上来讲可以用深搜广搜来做,但我第一时间没搞明白怎么做,所以就先迭代一发。 思路就是: 1. 题目给定的只有1个岛屿,那么我们可以遍历整个grid,对于发现的新岛屿
阅读全文
摘要:链接: https://leetcode.cn/problems/repeated-substring-pattern/description/ 思路: 这题其实挺有意思的,我一开始寻思按照字符读到一个dict里统计各个字符的个数,讲道理每个字符的个数是相同的才对。(我承认我傻了,忽略了aab这种情
阅读全文
摘要:链接 https://leetcode.cn/problems/arranging-coins/description/ 问题分析 这题看数据规模,遍历肯定搞不定。 看数据规律,我们优先考虑二分。 然后单拎出来一个函数用来计算求和即可。 其中,二分如果不好判断边界,就假定极限情况(来到了left =
阅读全文
摘要:链接 https://leetcode.cn/problems/number-of-segments-in-a-string/description/ 分析 直接split就好了。。 代码 class Solution: def countSegments(self, s: str) -> int:
阅读全文
摘要:链接 https://leetcode.cn/problems/add-strings/description/ 分析 大数相加而已,倒着遍历,然后相加就好了。 代码 class Solution: def addStrings(self, num1: str, num2: str) -> str:
阅读全文
摘要:地址: https://leetcode.cn/problems/third-maximum-number/description/ 思路: 设置一个set用来存储数据就Ok了,set还能天然去重。 注意判断新数据是否在set中。 代码: class Solution: def thirdMax(s
阅读全文
摘要:链接 https://leetcode.cn/problems/fizz-buzz/description/ 分析 没啥好分析的。。。注意他的下标是从1开始的,要把咱们自己的下标转换成虚拟下标。 代码 class Solution: def fizzBuzz(self, n: int): """ 给
阅读全文
摘要:链接 https://leetcode.cn/problems/longest-palindrome/description/ 分析 这题其实就是想让我们组成回文串。 回文串的特点: 1. 如果回文串长度为奇数,那么只有1个字符是奇数个,其余全是偶数个。 2. 如果回文串长度为偶数,那么全部字符都为
阅读全文
摘要:链接: https://leetcode.cn/problems/convert-a-number-to-hexadecimal/description/ 分析: 实话实说,我最怕编码类问题了,因为我真的不会,当时读大学的时候也没学好这块。 我的思路是: 首先对数据进行分类,分成正数、负数和零来分别
阅读全文
摘要:https://leetcode.cn/problems/sum-of-left-leaves/description/ 【分析】 该题要求左叶子之和。 如果我们对当前节点进行叶子节点的判断,那么我们是不知道当前节点是左叶子还是右叶子的。 所以我们应该在叶子结点的上层(父节点)进行判断。 【代码】
阅读全文
摘要:https://leetcode.cn/problems/binary-watch/description/ 分析 这是典型的循环DFS问题。 循环DFS一般应用在: 1. 输出字符的按位全排列。(比如一共4个数字,输出3个数字的全部组合) 2. 输出字符的全排列。(结合visited数组) 3.
阅读全文
摘要:问题描述 https://leetcode.cn/problems/binary-tree-paths/description/ 解题思路 叶子结点时,添加到结果序列即可。 代码 # Definition for a binary tree node. # class TreeNode: # def
阅读全文
摘要:问题描述 https://leetcode.cn/problems/invert-binary-tree/description/ 解题思路 没啥好说的,python的交换简单极了。 代码 # Definition for a binary tree node. # class TreeNode:
阅读全文
摘要:问题描述 https://leetcode.cn/problems/binary-tree-postorder-traversal/description/ 解题思路 这个题和先序一样,没啥好说的。 代码 # Definition for a binary tree node. # class Tr
阅读全文