提交 6ad114c3 编写于 作者: ToTensor's avatar ToTensor

增加习题数量

上级 71b9659a
{
"node_id": "dailycode-eab0b681e8ab4258a3286cf1d7642d64",
"node_id": "dailycode-857b20ebfd244d708b1010bbecd88556",
"keywords": [],
"children": [],
"keywords_must": [],
......
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "596d96c92b4b46c39c8f9a5731941081",
"author": "csdn.net",
"keywords": "树,深度优先搜索,广度优先搜索,二叉树"
}
\ No newline at end of file
# 对称二叉树
<p>给定一个二叉树,检查它是否是镜像对称的。</p>
<p>&nbsp;</p>
<p>例如,二叉树&nbsp;<code>[1,2,2,3,4,4,3]</code> 是对称的。</p>
<pre> 1
/ \
2 2
/ \ / \
3 4 4 3
</pre>
<p>&nbsp;</p>
<p>但是下面这个&nbsp;<code>[1,2,2,null,3,null,3]</code> 则不是镜像对称的:</p>
<pre> 1
/ \
2 2
\ \
3 3
</pre>
<p>&nbsp;</p>
<p><strong>进阶:</strong></p>
<p>你可以运用递归和迭代两种方法解决这个问题吗?</p>
## template
```python
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
def judge(left, right):
if not left and not right:
return True
elif not left or not right:
return False
else:
return left.val == right.val and judge(left.right, right.left) and judge(left.left, right.right)
return judge(root, root)
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-93691725213e43c3a260aeb9f012cd04",
"node_id": "dailycode-fffbbc837b574bf8b24cc5dbf815e374",
"keywords": [],
"children": [],
"keywords_must": [],
......
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "1a8b3997ea044b9aa0fc4b4c425c39a5",
"author": "csdn.net",
"keywords": "树,深度优先搜索,广度优先搜索,二叉树"
}
\ No newline at end of file
# 二叉树的最大深度
<p>给定一个二叉树,找出其最大深度。</p>
<p>二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。</p>
<p><strong>说明:</strong>&nbsp;叶子节点是指没有子节点的节点。</p>
<p><strong>示例:</strong><br>
给定二叉树 <code>[3,9,20,null,null,15,7]</code></p>
<pre> 3
/ \
9 20
/ \
15 7</pre>
<p>返回它的最大深度&nbsp;3 。</p>
## template
```python
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def maxDepth(self, root: TreeNode) -> int:
if root is None:
return 0
maxdepth = max(self.maxDepth(root.left),self.maxDepth(root.right)) + 1
return maxdepth
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-11b18eef90c24b0dbb4e062de3ebfe75",
"node_id": "dailycode-d7b84e9a9afd4d659afb4a7f453180aa",
"keywords": [],
"children": [],
"keywords_must": [],
......
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "418607231ee44c04bf6bbb9e29f3ad69",
"author": "csdn.net",
"keywords": "树,二叉搜索树,数组,分治,二叉树"
}
\ No newline at end of file
# 将有序数组转换为二叉搜索树
<p>给你一个整数数组 <code>nums</code> ,其中元素已经按 <strong>升序</strong> 排列,请你将其转换为一棵 <strong>高度平衡</strong> 二叉搜索树。</p>
<p><strong>高度平衡 </strong>二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过 1 」的二叉树。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" />
<pre>
<strong>输入:</strong>nums = [-10,-3,0,5,9]
<strong>输出:</strong>[0,-3,9,-10,null,5]
<strong>解释:</strong>[0,-10,5,null,-3,null,9] 也将被视为正确答案:
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" />
</pre>
<p><strong>示例 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" />
<pre>
<strong>输入:</strong>nums = [1,3]
<strong>输出:</strong>[3,1]
<strong>解释:</strong>[1,3] 和 [3,1] 都是高度平衡二叉搜索树。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>nums</code><strong>严格递增</strong> 顺序排列</li>
</ul>
## template
```python
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def sortedArrayToBST(self, nums):
"""
:type nums: List[int]
:rtype: TreeNode
"""
if not nums:
return None
mid = len(nums) // 2
root = TreeNode(nums[mid])
root.left = self.sortedArrayToBST(nums[:mid])
root.right = self.sortedArrayToBST(nums[mid + 1:])
return root
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-b694a6666fd54ceabeec06b1cf481e76",
"node_id": "dailycode-2076f08b30ca41268d61320e997b2feb",
"keywords": [],
"children": [],
"keywords_must": [],
......
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "b993e22fcd844d719b7028b0332dd289",
"exercise_id": "527b626cb0ac45e0bb5440fc57ba7aaf",
"author": "csdn.net",
"keywords": "树,深度优先搜索,二叉树"
}
\ No newline at end of file
# 平衡二叉树
<p>给定一个二叉树,判断它是否是高度平衡的二叉树。</p>
<p>本题中,一棵高度平衡二叉树定义为:</p>
<blockquote>
<p>一个二叉树<em>每个节点 </em>的左右两个子树的高度差的绝对值不超过 1 。</p>
</blockquote>
<p> </p>
<p><strong>示例 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg" style="width: 342px; height: 221px;" />
<pre>
<strong>输入:</strong>root = [3,9,20,null,null,15,7]
<strong>输出:</strong>true
</pre>
<p><strong>示例 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg" style="width: 452px; height: 301px;" />
<pre>
<strong>输入:</strong>root = [1,2,2,3,3,null,null,4,4]
<strong>输出:</strong>false
</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入:</strong>root = []
<strong>输出:</strong>true
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li>树中的节点数在范围 <code>[0, 5000]</code></li>
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
</ul>
## template
```python
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def isBalanced(self, root):
if not root:
return True
left_depth = self.get_depth(root.left)
right_depth = self.get_depth(root.right)
if abs(left_depth - right_depth) > 1:
return False
else:
return self.isBalanced(root.left) and self.isBalanced(root.right)
def get_depth(self, root):
if root is None:
return 0
else:
return max(self.get_depth(root.left), self.get_depth(root.right)) + 1
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-a30e8dd902af4c63bccad92d15bf88a4",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "0d322201b5c84b42963c70f0d58a3b14",
"author": "csdn.net",
"keywords": "树,深度优先搜索,广度优先搜索,二叉树"
}
\ No newline at end of file
# 二叉树的最小深度
<p>给定一个二叉树,找出其最小深度。</p>
<p>最小深度是从根节点到最近叶子节点的最短路径上的节点数量。</p>
<p><strong>说明:</strong>叶子节点是指没有子节点的节点。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg" style="width: 432px; height: 302px;" />
<pre>
<strong>输入:</strong>root = [3,9,20,null,null,15,7]
<strong>输出:</strong>2
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong>root = [2,null,3,null,4,null,5,null,6]
<strong>输出:</strong>5
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li>树中节点数的范围在 <code>[0, 10<sup>5</sup>]</code></li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
## template
```python
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def minDepth(self, root: TreeNode) -> int:
if not root:
return 0
queue = [root]
count = 1
while queue:
next_queue = []
for node in queue:
if not node.left and not node.right:
return count
if node.left:
next_queue.append(node.left)
if node.right:
next_queue.append(node.right)
queue = next_queue
count += 1
return count
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-7d73aa3dd0cd4eb892fa44ddb37e4c2d",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "9f961ad0206648198ba16c674bd376f7",
"exercise_id": "d31b89d8cc584f96b2d037e3bd0c270b",
"author": "csdn.net",
"keywords": "哈希表,字符串,滑动窗口"
"keywords": "树,深度优先搜索,二叉树"
}
\ No newline at end of file
# 路径总和
<p>给你二叉树的根节点 <code>root</code> 和一个表示目标和的整数 <code>targetSum</code> ,判断该树中是否存在 <strong>根节点到叶子节点</strong> 的路径,这条路径上所有节点值相加等于目标和 <code>targetSum</code></p>
<p><strong>叶子节点</strong> 是指没有子节点的节点。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;" />
<pre>
<strong>输入:</strong>root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
<strong>输出:</strong>true
</pre>
<p><strong>示例 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" />
<pre>
<strong>输入:</strong>root = [1,2,3], targetSum = 5
<strong>输出:</strong>false
</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入:</strong>root = [1,2], targetSum = 0
<strong>输出:</strong>false
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li>树中节点的数目在范围 <code>[0, 5000]</code></li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li><code>-1000 <= targetSum <= 1000</code></li>
</ul>
## template
```python
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def hasPathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: bool
"""
if root is None:
return False
if sum == root.val and root.left is None and root.right is None:
return True
return self.hasPathSum(root.left, sum-root.val) or self.hasPathSum(root.right, sum-root.val)
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-ccae2c71a01242969ef74c6b66f3f9de",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "a1166dcdfb0249239e461a48463b66a9",
"exercise_id": "a31850a1e76b4f1e88bd85c9c24ec688",
"author": "csdn.net",
"keywords": "数组,动态规划"
}
\ No newline at end of file
# 杨辉三角
<p>给定一个非负整数 <em><code>numRows</code></em>生成「杨辉三角」的前 <em><code>numRows</code> </em>行。</p>
<p><small>在「杨辉三角」中,每个数是它左上方和右上方的数的和。</small></p>
<p><img alt="" src="https://pic.leetcode-cn.com/1626927345-DZmfxB-PascalTriangleAnimated2.gif" /></p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong> numRows = 5
<strong>输出:</strong> [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong> numRows = 1
<strong>输出:</strong> [[1]]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= numRows <= 30</code></li>
</ul>
## template
```python
class Solution:
def generate(self, numRows: int) -> List[List[int]]:
if numRows == 0:
return []
if numRows == 1:
return [[1]]
if numRows == 2:
return [[1], [1, 1]]
result = [[1], [1, 1]] + [[] for i in range(numRows - 2)]
for i in range(2, numRows):
for j in range(i + 1):
if j == 0 or j == i:
result[i].append(1)
else:
result[i].append(result[i - 1][j - 1] + result[i - 1][j])
return result
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-84e718d951274716ac515bf786401eec",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "9d2fe777ecec45f8ba4729b64a610a63",
"author": "csdn.net",
"keywords": "数组,动态规划"
}
\ No newline at end of file
# 杨辉三角 II
<p>给定一个非负索引 <code>rowIndex</code>,返回「杨辉三角」的第 <code>rowIndex</code><em> </em>行。</p>
<p><small>在「杨辉三角」中,每个数是它左上方和右上方的数的和。</small></p>
<p><img alt="" src="https://pic.leetcode-cn.com/1626927345-DZmfxB-PascalTriangleAnimated2.gif" /></p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong> rowIndex = 3
<strong>输出:</strong> [1,3,3,1]
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong> rowIndex = 0
<strong>输出:</strong> [1]
</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入:</strong> rowIndex = 1
<strong>输出:</strong> [1,1]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 <= rowIndex <= 33</code></li>
</ul>
<p> </p>
<p><strong>进阶:</strong></p>
<p>你可以优化你的算法到 <code><em>O</em>(<i>rowIndex</i>)</code> 空间复杂度吗?</p>
## template
```python
class Solution(object):
def getRow(self, rowIndex):
"""
:type rowIndex: int
:rtype: List[int]
"""
if rowIndex == 0:
return [1]
pas = [1]
for i in range(rowIndex):
newLine = list(map(lambda x, y: x + y, [0] + pas, pas + [0]))
pas = newLine
return pas
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-b731ace07be543f681b7f899761c0466",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "c7e978e2413d43bf91613f4e6ff13213",
"author": "csdn.net",
"keywords": "数组,动态规划"
}
\ No newline at end of file
# 买卖股票的最佳时机
<p>给定一个数组 <code>prices</code> ,它的第 <code>i</code> 个元素 <code>prices[i]</code> 表示一支给定股票第 <code>i</code> 天的价格。</p>
<p>你只能选择 <strong>某一天</strong> 买入这只股票,并选择在 <strong>未来的某一个不同的日子</strong> 卖出该股票。设计一个算法来计算你所能获取的最大利润。</p>
<p>返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 <code>0</code></p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong>[7,1,5,3,6,4]
<strong>输出:</strong>5
<strong>解释:</strong>在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong>prices = [7,6,4,3,1]
<strong>输出:</strong>0
<strong>解释:</strong>在这种情况下, 没有交易完成, 所以最大利润为 0。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= prices.length <= 10<sup>5</sup></code></li>
<li><code>0 <= prices[i] <= 10<sup>4</sup></code></li>
</ul>
## template
```python
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
temp = []
if not prices or len(prices) == 1:
return 0
for i in range(len(prices) - 1):
temp.append(max(prices[i + 1 :]) - prices[i])
if max(temp) >= 0:
return max(temp)
else:
return 0
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-ecc091b493f749e8a3309f75b7170e78",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "8603270bc7f24c5491c77be4ccb437ce",
"author": "csdn.net",
"keywords": "贪心,数组,动态规划"
}
\ No newline at end of file
# 买卖股票的最佳时机 II
<p>给定一个数组 <code>prices</code> ,其中 <code>prices[i]</code> 是一支给定股票第 <code>i</code> 天的价格。</p>
<p>设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。</p>
<p><strong>注意:</strong>你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong> prices = [7,1,5,3,6,4]
<strong>输出:</strong> 7
<strong>解释:</strong> 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
  随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong> prices = [1,2,3,4,5]
<strong>输出:</strong> 4
<strong>解释:</strong> 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
  注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入:</strong> prices = [7,6,4,3,1]
<strong>输出:</strong> 0
<strong>解释:</strong> 在这种情况下, 没有交易完成, 所以最大利润为 0。</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= prices.length <= 3 * 10<sup>4</sup></code></li>
<li><code>0 <= prices[i] <= 10<sup>4</sup></code></li>
</ul>
## template
```python
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
hold = 0
pric = []
temp = []
flag = 0
msum = 0
if len(prices) <= 2:
if not prices:
return 0
if len(prices) == 1:
return 0
if prices[0] > prices[1]:
return 0
if prices[0] < prices[1]:
return prices[1] - prices[0]
for i in range(len(prices) - 1):
if prices[i + 1] > prices[i] and hold != 1:
hold = 1
flag = i
continue
if prices[i + 1] < prices[i] and hold == 1:
pric.append(prices[flag])
pric.append(prices[i])
hold = 0
else:
continue
for i in range(0, len(pric), 2):
temp.append(pric[i + 1] - pric[i])
msum = sum(temp)
if hold == 1:
msum = msum + prices[-1] - prices[flag]
return msum
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-38358c269a97421ab9ef0ddc4846a0a8",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "8211bd137d5249b28c413ef2fe4c8474",
"exercise_id": "14ba2edd5caa47d98a9d42a3dcbe1889",
"author": "csdn.net",
"keywords": "双指针,字符串"
}
\ No newline at end of file
# 验证回文串
<p>给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。</p>
<p><strong>说明:</strong>本题中,我们将空字符串定义为有效的回文串。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong> "A man, a plan, a canal: Panama"
<strong>输出:</strong> true
<strong>解释:</strong>"amanaplanacanalpanama" 是回文串
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong> "race a car"
<strong>输出:</strong> false
<strong>解释:</strong>"raceacar" 不是回文串
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= s.length <= 2 * 10<sup>5</sup></code></li>
<li>字符串 <code>s</code> 由 ASCII 字符组成</li>
</ul>
## template
```python
class Solution(object):
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
i = 0
j = len(s) - 1
while i < j:
if s[i].isalnum() and s[j].isalnum():
if s[i].upper() == s[j].upper():
i += 1
j -= 1
else:
return False
else:
if not s[i].isalnum():
i += 1
if not s[j].isalnum():
j -= 1
return True
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-b8b700c8be4a456889f3bdf2508bda92",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "ae412fd242cc4bb4a36d550479a9ab13",
"author": "csdn.net",
"keywords": "位运算,数组"
}
\ No newline at end of file
# 只出现一次的数字
<p>给定一个<strong>非空</strong>整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。</p>
<p><strong>说明:</strong></p>
<p>你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?</p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong> [2,2,1]
<strong>输出:</strong> 1
</pre>
<p><strong>示例&nbsp;2:</strong></p>
<pre><strong>输入:</strong> [4,1,2,1,2]
<strong>输出:</strong> 4</pre>
## template
```python
class Solution:
def singleNumber(self, nums: List[int]) -> int:
nums = sorted(nums)
i = 0
while i < len(nums) - 1:
if nums[i] == nums[i + 1]:
i += 2
else:
return nums[i]
return nums[i]
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-f38f728eb8ce496791f8904f6a664173",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "c1ed8b0c386f4957a33dae2b72214ebc",
"author": "csdn.net",
"keywords": "哈希表,链表,双指针"
}
\ No newline at end of file
# 环形链表
<p>给定一个链表,判断链表中是否有环。</p>
<p>如果链表中有某个节点,可以通过连续跟踪 <code>next</code> 指针再次到达,则链表中存在环。 为了表示给定链表中的环,我们使用整数 <code>pos</code> 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 <code>pos</code><code>-1</code>,则在该链表中没有环。<strong>注意:<code>pos</code> 不作为参数进行传递</strong>,仅仅是为了标识链表的实际情况。</p>
<p>如果链表中存在环,则返回 <code>true</code> 。 否则,返回 <code>false</code></p>
<p>&nbsp;</p>
<p><strong>进阶:</strong></p>
<p>你能用 <em>O(1)</em>(即,常量)内存解决此问题吗?</p>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/07/circularlinkedlist.png" style="height: 97px; width: 300px;"></p>
<pre><strong>输入:</strong>head = [3,2,0,-4], pos = 1
<strong>输出:</strong>true
<strong>解释:</strong>链表中有一个环,其尾部连接到第二个节点。
</pre>
<p><strong>示例&nbsp;2:</strong></p>
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 74px; width: 141px;"></p>
<pre><strong>输入:</strong>head = [1,2], pos = 0
<strong>输出:</strong>true
<strong>解释:</strong>链表中有一个环,其尾部连接到第一个节点。
</pre>
<p><strong>示例 3:</strong></p>
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 45px; width: 45px;"></p>
<pre><strong>输入:</strong>head = [1], pos = -1
<strong>输出:</strong>false
<strong>解释:</strong>链表中没有环。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li>链表中节点的数目范围是 <code>[0, 10<sup>4</sup>]</code></li>
<li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
<li><code>pos</code><code>-1</code> 或者链表中的一个 <strong>有效索引</strong></li>
</ul>
## template
```python
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def hasCycle(self, head: ListNode) -> bool:
if not (head and head.next):
return False
slow = head
fast = head.next
while fast.next and fast.next.next:
if slow == fast:
return True
slow = slow.next
fast = fast.next.next
return False
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-bc5c9dcfba284e84a8c6152a629882cd",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "801926d6688b49c3aa255367ef29d997",
"author": "csdn.net",
"keywords": "栈,树,深度优先搜索,二叉树"
}
\ No newline at end of file
# 二叉树的前序遍历
<p>给你二叉树的根节点 <code>root</code> ,返回它节点值的 <strong>前序</strong><em> </em>遍历。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg" style="width: 202px; height: 324px;" />
<pre>
<strong>输入:</strong>root = [1,null,2,3]
<strong>输出:</strong>[1,2,3]
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong>root = []
<strong>输出:</strong>[]
</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入:</strong>root = [1]
<strong>输出:</strong>[1]
</pre>
<p><strong>示例 4:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/inorder_5.jpg" style="width: 202px; height: 202px;" />
<pre>
<strong>输入:</strong>root = [1,2]
<strong>输出:</strong>[1,2]
</pre>
<p><strong>示例 5:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/inorder_4.jpg" style="width: 202px; height: 202px;" />
<pre>
<strong>输入:</strong>root = [1,null,2]
<strong>输出:</strong>[1,2]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li>树中节点数目在范围 <code>[0, 100]</code></li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>进阶:</strong>递归算法很简单,你可以通过迭代算法完成吗?</p>
## template
```python
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def __init__(self):
self.ans = []
def preorderTraversal(self, root: TreeNode) -> List[int]:
if not root:
return []
self.ans.append(root.val)
self.preorderTraversal(root.left)
self.preorderTraversal(root.right)
return self.ans
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-d4fe0f464d814a09ba53b9dbfbf56e3a",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "54d6ae2161f2477997472d9efe8863c6",
"author": "csdn.net",
"keywords": "栈,树,深度优先搜索,二叉树"
}
\ No newline at end of file
# 二叉树的后序遍历
<p>给定一个二叉树,返回它的 <em>后序&nbsp;</em>遍历。</p>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong> [1,null,2,3]
1
\
2
/
3
<strong>输出:</strong> [3,2,1]</pre>
<p><strong>进阶:</strong>&nbsp;递归算法很简单,你可以通过迭代算法完成吗?</p>
## template
```python
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def postorderTraversal(self, root: TreeNode):
if root is None:
return []
stack, output = [], []
stack.append(root)
while stack:
node = stack.pop()
output.append(node.val)
if node.left:
stack.append(node.left)
if node.right:
stack.append(node.right)
return output[::-1]
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-66d52c7607294afba95f84755f347292",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "23a7c3aaad084cc9898bb5b78c0a8f1a",
"exercise_id": "4bb5f28525fe48478a71b212cd28e59e",
"author": "csdn.net",
"keywords": "数据库"
"keywords": "栈,设计"
}
\ No newline at end of file
# 最小栈
<p>设计一个支持 <code>push</code><code>pop</code><code>top</code> 操作,并能在常数时间内检索到最小元素的栈。</p>
<ul>
<li><code>push(x)</code> &mdash;&mdash; 将元素 x 推入栈中。</li>
<li><code>pop()</code>&nbsp;&mdash;&mdash; 删除栈顶的元素。</li>
<li><code>top()</code>&nbsp;&mdash;&mdash; 获取栈顶元素。</li>
<li><code>getMin()</code> &mdash;&mdash; 检索栈中的最小元素。</li>
</ul>
<p>&nbsp;</p>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong>
[&quot;MinStack&quot;,&quot;push&quot;,&quot;push&quot;,&quot;push&quot;,&quot;getMin&quot;,&quot;pop&quot;,&quot;top&quot;,&quot;getMin&quot;]
[[],[-2],[0],[-3],[],[],[],[]]
<strong>输出:</strong>
[null,null,null,null,-3,null,0,-2]
<strong>解释:</strong>
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --&gt; 返回 -3.
minStack.pop();
minStack.top(); --&gt; 返回 0.
minStack.getMin(); --&gt; 返回 -2.
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>pop</code><code>top</code><code>getMin</code> 操作总是在 <strong>非空栈</strong> 上调用。</li>
</ul>
## template
```python
class MinStack:
def __init__(self):
self.data = [(None, float("inf"))]
def push(self, x: "int") -> "None":
self.data.append((x, min(x, self.data[-1][1])))
def pop(self) -> "None":
if len(self.data) > 1:
self.data.pop()
def top(self) -> "int":
return self.data[-1][0]
def getMin(self) -> "int":
return self.data[-1][1]
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(x)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-fd779c4b6023499ca4ca56d2a5622f53",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "d212ea52512549ee9e3064e2add0ba00",
"author": "csdn.net",
"keywords": "哈希表,链表,双指针"
}
\ No newline at end of file
# 相交链表
<p>给你两个单链表的头节点 <code>headA</code><code>headB</code> ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 <code>null</code></p>
<p>图示两个链表在节点 <code>c1</code> 开始相交<strong></strong></p>
<p><a href="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/14/160_statement.png" target="_blank"><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/14/160_statement.png" style="height: 130px; width: 400px;" /></a></p>
<p>题目数据 <strong>保证</strong> 整个链式结构中不存在环。</p>
<p><strong>注意</strong>,函数返回结果后,链表必须 <strong>保持其原始结构</strong></p>
<p> </p>
<p><strong>示例 1:</strong></p>
<p><a href="https://assets.leetcode.com/uploads/2018/12/13/160_example_1.png" target="_blank"><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/14/160_example_1.png" style="height: 130px; width: 400px;" /></a></p>
<pre>
<strong>输入:</strong>intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
<strong>输出:</strong>Intersected at '8'
<strong>解释:</strong>相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。
在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
</pre>
<p><strong>示例 2:</strong></p>
<p><a href="https://assets.leetcode.com/uploads/2018/12/13/160_example_2.png" target="_blank"><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/14/160_example_2.png" style="height: 136px; width: 350px;" /></a></p>
<pre>
<strong>输入:</strong>intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
<strong>输出:</strong>Intersected at '2'
<strong>解释:</strong>相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。
在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。
</pre>
<p><strong>示例 3:</strong></p>
<p><a href="https://assets.leetcode.com/uploads/2018/12/13/160_example_3.png" target="_blank"><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/14/160_example_3.png" style="height: 126px; width: 200px;" /></a></p>
<pre>
<strong>输入:</strong>intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
<strong>输出:</strong>null
<strong>解释:</strong>从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。
由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
这两个链表不相交,因此返回 null 。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>listA</code> 中节点数目为 <code>m</code></li>
<li><code>listB</code> 中节点数目为 <code>n</code></li>
<li><code>0 <= m, n <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li><code>0 <= skipA <= m</code></li>
<li><code>0 <= skipB <= n</code></li>
<li>如果 <code>listA</code><code>listB</code> 没有交点,<code>intersectVal</code><code>0</code></li>
<li>如果 <code>listA</code><code>listB</code> 有交点,<code>intersectVal == listA[skipA + 1] == listB[skipB + 1]</code></li>
</ul>
<p> </p>
<p><strong>进阶:</strong>你能否设计一个时间复杂度 <code>O(n)</code> 、仅用 <code>O(1)</code> 内存的解决方案?</p>
## template
```python
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
if not headA or not headB:
return None
wei_A = []
while headA:
wei_A.append(headA)
headA = headA.next
wei_A = wei_A[::-1]
wei_B = []
while headB:
wei_B.append(headB)
headB = headB.next
wei_B = wei_B[::-1]
count = -1
lA = len(wei_A)
lB = len(wei_B)
if lA < lB:
copy = wei_B
wei_B = wei_A
wei_A = copy
lA, lB = lB, lA
for i in range(lB):
if wei_A[i] == wei_B[i]:
count += 1
else:
break
if count != -1:
return wei_A[count]
else:
return None
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-9c0694e75d184730a04e2b16c008bcff",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "35f3bc4609d04f7a84232885378e69c6",
"author": "csdn.net",
"keywords": "数组,双指针,二分查找"
}
\ No newline at end of file
# 两数之和 II
<p>给定一个已按照<strong><em> </em>非递减顺序排列&nbsp; </strong>的整数数组&nbsp;<code>numbers</code> ,请你从数组中找出两个数满足相加之和等于目标数&nbsp;<code>target</code></p>
<p>函数应该以长度为 <code>2</code> 的整数数组的形式返回这两个数的下标值<em></em><code>numbers</code> 的下标 <strong>从 1 开始计数</strong> ,所以答案数组应当满足 <code>1 &lt;= answer[0] &lt; answer[1] &lt;= numbers.length</code></p>
<p>你可以假设每个输入 <strong>只对应唯一的答案</strong> ,而且你 <strong>不可以</strong> 重复使用相同的元素。</p>
&nbsp;
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong>numbers = [2,7,11,15], target = 9
<strong>输出:</strong>[1,2]
<strong>解释:</strong>2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong>numbers = [2,3,4], target = 6
<strong>输出:</strong>[1,3]
</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入:</strong>numbers = [-1,0], target = -1
<strong>输出:</strong>[1,2]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 &lt;= numbers.length &lt;= 3 * 10<sup>4</sup></code></li>
<li><code>-1000 &lt;= numbers[i] &lt;= 1000</code></li>
<li><code>numbers</code><strong>非递减顺序</strong> 排列</li>
<li><code>-1000 &lt;= target &lt;= 1000</code></li>
<li><strong>仅存在一个有效答案</strong></li>
</ul>
## template
```python
class Solution(object):
def twoSum(self, numbers, target):
"""
:type numbers: List[int]
:type target: int
:rtype: List[int]
"""
d = {}
size = 0
while size < len(numbers):
if not numbers[size] in d:
d[numbers[size]] = size + 1
if target - numbers[size] in d:
if d[target - numbers[size]] < size + 1:
answer = [d[target - numbers[size]], size + 1]
return answer
size = size + 1
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-2bc12d92020145e9ac2a3ffc6c585faa",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "93d222e11bc247efb7e2e1bf99b1db48",
"author": "csdn.net",
"keywords": "数学,字符串"
}
\ No newline at end of file
# Excel表列名称
<p>给你一个整数 <code>columnNumber</code> ,返回它在 Excel 表中相对应的列名称。</p>
<p>例如:</p>
<pre>
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
</pre>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong>columnNumber = 1
<strong>输出:</strong>"A"
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong>columnNumber = 28
<strong>输出:</strong>"AB"
</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入:</strong>columnNumber = 701
<strong>输出:</strong>"ZY"
</pre>
<p><strong>示例 4:</strong></p>
<pre>
<strong>输入:</strong>columnNumber = 2147483647
<strong>输出:</strong>"FXSHRXW"
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= columnNumber <= 2<sup>31</sup> - 1</code></li>
</ul>
## template
```python
class Solution(object):
def convertToTitle(self, n):
"""
:type n: int
:rtype: str
"""
d = {}
r = []
a = ""
for i in range(1, 27):
d[i] = chr(64 + i)
if n <= 26:
return d[n]
if n % 26 == 0:
n = n / 26 - 1
a = "Z"
while n > 26:
s = n % 26
n = n // 26
r.append(s)
result = d[n]
for i in r[::-1]:
result += d[i]
return result + a
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-b39f0553588c467692394aaf6fac7c7e",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "02def488570747418e9d712a1f26f324",
"author": "csdn.net",
"keywords": "数组,哈希表,分治,计数,排序"
}
\ No newline at end of file
# 多数元素
<p>给定一个大小为 <em>n </em>的数组,找到其中的多数元素。多数元素是指在数组中出现次数 <strong>大于</strong> <code>⌊ n/2 ⌋</code> 的元素。</p>
<p>你可以假设数组是非空的,并且给定的数组总是存在多数元素。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong>[3,2,3]
<strong>输出:</strong>3</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong>[2,2,1,1,1,2,2]
<strong>输出:</strong>2
</pre>
<p> </p>
<p><strong>进阶:</strong></p>
<ul>
<li>尝试设计时间复杂度为 O(n)、空间复杂度为 O(1) 的算法解决此问题。</li>
</ul>
## template
```python
class Solution:
def majorityElement(self, nums: List[int]) -> int:
count, candi = 0, 0
for i in nums:
if i == candi:
count += 1
else:
if count == 0:
candi = i
count = 1
else:
count -= 1
return candi
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-5305f23daf5b42dd98e7bbdc5293c844",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "aec8ebe4c8c34fc39cf954a403a34847",
"author": "csdn.net",
"keywords": "数学,字符串"
}
\ No newline at end of file
# Excel表列序号
<p>给你一个字符串&nbsp;<code>columnTitle</code> ,表示 Excel 表格中的列名称。返回该列名称对应的列序号。</p>
<p>&nbsp;</p>
<p>例如,</p>
<pre>
A -&gt; 1
B -&gt; 2
C -&gt; 3
...
Z -&gt; 26
AA -&gt; 27
AB -&gt; 28
...
</pre>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong> columnTitle = "A"
<strong>输出:</strong> 1
</pre>
<p><strong>示例&nbsp;2:</strong></p>
<pre>
<strong>输入: </strong>columnTitle = "AB"
<strong>输出:</strong> 28
</pre>
<p><strong>示例&nbsp;3:</strong></p>
<pre>
<strong>输入: </strong>columnTitle = "ZY"
<strong>输出:</strong> 701</pre>
<p><strong>示例 4:</strong></p>
<pre>
<strong>输入: </strong>columnTitle = "FXSHRXW"
<strong>输出: </strong>2147483647
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= columnTitle.length &lt;= 7</code></li>
<li><code>columnTitle</code> 仅由大写英文组成</li>
<li><code>columnTitle</code> 在范围 <code>["A", "FXSHRXW"]</code></li>
</ul>
## template
```python
class Solution:
def titleToNumber(self, s: str) -> int:
ans = 0
for i in range(len(s)):
n = ord(s[i]) - ord("A") + 1
ans = ans * 26 + n
return ans
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-a9a0b2f07bdd4d47aa980509ff681936",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "a4d7fa87cede461a9ba8e62a01343421",
"exercise_id": "6b2211be24844846afddd9cec234f4e5",
"author": "csdn.net",
"keywords": "shell"
"keywords": "数学"
}
\ No newline at end of file
# 阶乘后的零
<p>给定一个整数 <code>n</code> ,返回 <code>n!</code> 结果中尾随零的数量。</p>
<p>提示&nbsp;<code>n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1</code></p>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong>n = 3
<strong>输出:</strong>0
<strong>解释:</strong>3! = 6 ,不含尾随 0
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong>n = 5
<strong>输出:</strong>1
<strong>解释:</strong>5! = 120 ,有一个尾随 0
</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入:</strong>n = 0
<strong>输出:</strong>0
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 &lt;= n &lt;= 10<sup>4</sup></code></li>
</ul>
<p>&nbsp;</p>
<p><b>进阶:</b>你可以设计并实现对数时间复杂度的算法来解决此问题吗?</p>
## template
```python
class Solution:
def trailingZeroes(self, n: int) -> int:
zero_count = 0
current_multiple = 5
while n >= current_multiple:
zero_count += n // current_multiple
current_multiple *= 5
return zero_count
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-ba1e4e9897d64eaf9ac066a03437b62f",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "1ba368a03960439599d6d1b5e6867284",
"author": "csdn.net",
"keywords": "位运算,分治"
}
\ No newline at end of file
# 颠倒二进制位
<p>颠倒给定的 32 位无符号整数的二进制位。</p>
<p><strong>提示:</strong></p>
<ul>
<li>请注意,在某些语言(如 Java)中,没有无符号整数类型。在这种情况下,输入和输出都将被指定为有符号整数类型,并且不应影响您的实现,因为无论整数是有符号的还是无符号的,其内部的二进制表示形式都是相同的。</li>
<li>在 Java 中,编译器使用<a href="https://baike.baidu.com/item/二进制补码/5295284" target="_blank">二进制补码</a>记法来表示有符号整数。因此,在 <strong>示例 2</strong>&nbsp;中,输入表示有符号整数 <code>-3</code>,输出表示有符号整数 <code>-1073741825</code></li>
</ul>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong>n = 00000010100101000001111010011100
<strong>输出:</strong>964176192 (00111001011110000010100101000000)
<strong>解释:</strong>输入的二进制串 <strong>00000010100101000001111010011100 </strong>表示无符号整数<strong> 43261596</strong><strong>
</strong> 因此返回 964176192,其二进制表示形式为 <strong>00111001011110000010100101000000</strong></pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong>n = 11111111111111111111111111111101
<strong>输出:</strong>3221225471 (10111111111111111111111111111111)
<strong>解释:</strong>输入的二进制串 <strong>11111111111111111111111111111101</strong> 表示无符号整数 4294967293,
&nbsp; 因此返回 3221225471 其二进制表示形式为 <strong>10111111111111111111111111111111 。</strong></pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li>输入是一个长度为 <code>32</code> 的二进制字符串</li>
</ul>
<p>&nbsp;</p>
<p><strong>进阶</strong>: 如果多次调用这个函数,你将如何优化你的算法?</p>
## template
```python
class Solution:
def reverseBits(self, n: int) -> int:
j = 0
y = bin(n)[2::]
if len(y) != 32:
j = 32 - len(y)
m = y[::-1]
m = m + "0" * j
return int(m, 2)
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-e46a5de28b3b481ca0a2f72f087542bf",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "9ac6dd56154b4494a3cf9d08fab84d02",
"exercise_id": "430b7b13abf747cc86a8ceb4b3db33d8",
"author": "csdn.net",
"keywords": "位运算"
}
\ No newline at end of file
# 位1的个数
<p>编写一个函数,输入是一个无符号整数(以二进制串的形式),返回其二进制表达式中数字位数为 '1' 的个数(也被称为<a href="https://baike.baidu.com/item/%E6%B1%89%E6%98%8E%E9%87%8D%E9%87%8F" target="_blank">汉明重量</a>)。</p>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li>请注意,在某些语言(如 Java)中,没有无符号整数类型。在这种情况下,输入和输出都将被指定为有符号整数类型,并且不应影响您的实现,因为无论整数是有符号的还是无符号的,其内部的二进制表示形式都是相同的。</li>
<li>在 Java 中,编译器使用<a href="https://baike.baidu.com/item/二进制补码/5295284" target="_blank">二进制补码</a>记法来表示有符号整数。因此,在上面的 <strong>示例 3</strong> 中,输入表示有符号整数 <code>-3</code></li>
</ul>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong>00000000000000000000000000001011
<strong>输出:</strong>3
<strong>解释:</strong>输入的二进制串 <code><strong>00000000000000000000000000001011</strong> 中,共有三位为 '1'。</code>
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong>00000000000000000000000010000000
<strong>输出:</strong>1
<strong>解释:</strong>输入的二进制串 <strong>00000000000000000000000010000000</strong> 中,共有一位为 '1'。
</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入:</strong>11111111111111111111111111111101
<strong>输出:</strong>31
<strong>解释:</strong>输入的二进制串 <strong>11111111111111111111111111111101</strong> 中,共有 31 位为 '1'。</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li>输入必须是长度为 <code>32</code><strong>二进制串</strong></li>
</ul>
<ul>
</ul>
<p> </p>
<p><strong>进阶</strong></p>
<ul>
<li>如果多次调用这个函数,你将如何优化你的算法?</li>
</ul>
## template
```python
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
if n == 1:
return nums[0]
dic = {}
for num in nums:
if num in dic.keys():
dic[num] += 1
if dic[num] > n / 2:
return num
else:
dic[num] = 1
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-00d14814b994486488ba5843af2e195d",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "2ae80a2197fc4ecf8a5cf1a7b5a944fe",
"author": "csdn.net",
"keywords": "哈希表,数学,双指针"
}
\ No newline at end of file
# 快乐数
<p>编写一个算法来判断一个数 <code>n</code> 是不是快乐数。</p>
<p>「快乐数」定义为:</p>
<ul>
<li>对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。</li>
<li>然后重复这个过程直到这个数变为 1,也可能是 <strong>无限循环</strong> 但始终变不到 1。</li>
<li>如果 <strong>可以变为</strong>  1,那么这个数就是快乐数。</li>
</ul>
<p>如果 <code>n</code> 是快乐数就返回 <code>true</code> ;不是,则返回 <code>false</code></p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong>19
<strong>输出:</strong>true
<strong>解释:
</strong>1<sup>2</sup> + 9<sup>2</sup> = 82
8<sup>2</sup> + 2<sup>2</sup> = 68
6<sup>2</sup> + 8<sup>2</sup> = 100
1<sup>2</sup> + 0<sup>2</sup> + 0<sup>2</sup> = 1
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong>n = 2
<strong>输出:</strong>false
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= n <= 2<sup>31</sup> - 1</code></li>
</ul>
## template
```python
class Solution(object):
def isHappy(self, n):
"""
:type n: int
:rtype: bool
"""
d = {}
while True:
l = list(map(int, list(str(n))))
m = 0
for i in l:
m += i ** 2
if m in d:
print(d)
return False
if m == 1:
print(d)
return True
d[m] = m
n = m
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-f9e03e7069b44ff183ff2c353b154b9d",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "270e7864b56d4254955b30b93c85451b",
"exercise_id": "ca6e65c26e5545acac0eb3d2f4da1baa",
"author": "csdn.net",
"keywords": "数据库"
"keywords": "递归,链表"
}
\ No newline at end of file
# 移除链表元素
给你一个链表的头节点 <code>head</code> 和一个整数 <code>val</code> ,请你删除链表中所有满足 <code>Node.val == val</code> 的节点,并返回 <strong>新的头节点</strong>
<p> </p>
<p><strong>示例 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/06/removelinked-list.jpg" style="width: 500px; height: 142px;" />
<pre>
<strong>输入:</strong>head = [1,2,6,3,4,5,6], val = 6
<strong>输出:</strong>[1,2,3,4,5]
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong>head = [], val = 1
<strong>输出:</strong>[]
</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入:</strong>head = [7,7,7,7], val = 7
<strong>输出:</strong>[]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li>列表中的节点数目在范围 <code>[0, 10<sup>4</sup>]</code></li>
<li><code>1 <= Node.val <= 50</code></li>
<li><code>0 <= val <= 50</code></li>
</ul>
## template
```python
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def removeElements(self, head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
p = ListNode(0)
p.next = head
while p.next:
if p.next.val == val:
if p.next == head:
head = head.next
p.next = head
else:
p.next = p.next.next
else:
p = p.next
return head
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-dbd76c754b4040be93aa6f8f90e7b451",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "c89c149170ef4f7f80640c0daaa90947",
"author": "csdn.net",
"keywords": "数组,数学,枚举,数论"
}
\ No newline at end of file
# 计数质数
<p>统计所有小于非负整数&nbsp;<em><code>n</code>&nbsp;</em>的质数的数量。</p>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>n = 10
<strong>输出:</strong>4
<strong>解释:</strong>小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。
</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>n = 0
<strong>输出:</strong>0
</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入:</strong>n = 1
<strong>输出</strong>:0
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 &lt;= n &lt;= 5 * 10<sup>6</sup></code></li>
</ul>
## template
```python
class Solution:
def countPrimes(self, n: int) -> int:
is_prime = [1] * n
count = 0
for i in range(2, n):
if is_prime[i]:
count += 1
for j in range(i * i, n, i):
is_prime[j] = 0
return count
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-0dea8f94ab7349a68257ce162478c6a7",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "59b9df4c8d5e478bbcc729f611519790",
"author": "csdn.net",
"keywords": "哈希表,字符串"
}
\ No newline at end of file
# 同构字符串
<p>给定两个字符串 <em><strong></strong></em>和 <strong><em>t</em></strong>,判断它们是否是同构的。</p>
<p>如果 <em><strong></strong></em>中的字符可以按某种映射关系替换得到 <strong><em></em></strong>,那么这两个字符串是同构的。</p>
<p>每个出现的字符都应当映射到另一个字符,同时不改变字符的顺序。不同字符不能映射到同一个字符上,相同字符只能映射到同一个字符上,字符可以映射到自己本身。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong><strong><em>s</em></strong> = <code>"egg", </code><strong><em>t = </em></strong><code>"add"</code>
<strong>输出:</strong>true
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong><strong><em>s</em></strong> = <code>"foo", </code><strong><em>t = </em></strong><code>"bar"</code>
<strong>输出:</strong>false</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入:</strong><strong><em>s</em></strong> = <code>"paper", </code><strong><em>t = </em></strong><code>"title"</code>
<strong>输出:</strong>true</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li>可以假设 <em><strong></strong></em><strong><em>t </em></strong>长度相同。</li>
</ul>
## template
```python
class Solution(object):
def isIsomorphic(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
if len(s) != len(t):
return False
if len(s) == None or len(s) < 2:
return True
smap = {}
for i in range(len(s)):
if s[i] not in smap and t[i] in smap.values():
return False
if s[i] in smap and smap[s[i]] != t[i]:
return False
smap[s[i]] = t[i]
return True
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-c0d398d68a614160a0d6e59063e79b3a",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "d914beb248f541e7b429b27bc0e74fc7",
"exercise_id": "2b89e3dc80484eda9e3ce71f840ca07e",
"author": "csdn.net",
"keywords": "数据库"
"keywords": "递归,链表"
}
\ No newline at end of file
# 反转链表
给你单链表的头节点 <code>head</code> ,请你反转链表,并返回反转后的链表。
<div class="original__bRMd">
<div>
<p> </p>
<p><strong>示例 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>输入:</strong>head = [1,2,3,4,5]
<strong>输出:</strong>[5,4,3,2,1]
</pre>
<p><strong>示例 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg" style="width: 182px; height: 222px;" />
<pre>
<strong>输入:</strong>head = [1,2]
<strong>输出:</strong>[2,1]
</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入:</strong>head = []
<strong>输出:</strong>[]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li>链表中节点的数目范围是 <code>[0, 5000]</code></li>
<li><code>-5000 <= Node.val <= 5000</code></li>
</ul>
<p> </p>
<p><strong>进阶:</strong>链表可以选用迭代或递归方式完成反转。你能否用两种方法解决这道题?</p>
</div>
</div>
## template
```python
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
current = head
nextNode = None
h = head
while current is not None and current.next is not None:
nextNode = current.next
if nextNode.next is not None:
tmpNode = current.next
current.next = nextNode.next
tmpNode.next = h
else:
current.next = None
nextNode.next = h
h = nextNode
return h
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-ddb49fc2280f49e3b19320ca08891245",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "aba9b369349943d094794981c2d6d224",
"author": "csdn.net",
"keywords": "数组,哈希表,排序"
}
\ No newline at end of file
# 存在重复元素
<p>给定一个整数数组,判断是否存在重复元素。</p>
<p>如果存在一值在数组中出现至少两次,函数返回 <code>true</code> 。如果数组中每个元素都不相同,则返回 <code>false</code></p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong> [1,2,3,1]
<strong>输出:</strong> true</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入: </strong>[1,2,3,4]
<strong>输出:</strong> false</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入: </strong>[1,1,1,3,3,4,3,2,4,2]
<strong>输出:</strong> true</pre>
## template
```python
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
nums.sort()
count = 0
while count < len(nums) - 1:
if nums[count] == nums[count + 1]:
return True
count += 1
return False
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-c577cd4335a846a5bae08b419ecf7236",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "ba81057039ca4027a1dec611797bbdc6",
"author": "csdn.net",
"keywords": "数组,哈希表,滑动窗口"
}
\ No newline at end of file
# 存在重复元素 II
<p>给定一个整数数组和一个整数&nbsp;<em>k</em>,判断数组中是否存在两个不同的索引<em>&nbsp;i</em>&nbsp;<em>&nbsp;j</em>,使得&nbsp;<strong>nums [i] = nums [j]</strong>,并且 <em>i</em><em>j</em>&nbsp;的差的 <strong>绝对值</strong> 至多为 <em>k</em></p>
<p>&nbsp;</p>
<p><strong>示例&nbsp;1:</strong></p>
<pre><strong>输入:</strong> nums = [1,2,3,1], k<em> </em>= 3
<strong>输出:</strong> true</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入: </strong>nums = [1,0,1,1], k<em> </em>=<em> </em>1
<strong>输出:</strong> true</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入: </strong>nums = [1,2,3,1,2,3], k<em> </em>=<em> </em>2
<strong>输出:</strong> false</pre>
## template
```python
class Solution:
def containsNearbyDuplicate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
if len(list(set(nums))) == len(nums):
return False
left = 0
right = left + k
if k >= len(nums):
return len(list(set(nums))) < len(nums)
while right < len(nums):
while left < right:
if nums[left] == nums[right]:
return True
else:
right -= 1
left += 1
right = left + k
if len(list(set(nums[left:]))) < len(nums[left:]):
return True
return False
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-cffa52e97c224fdf8530778290a05c2d",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "ef25569152e840bba97c52891b63d50c",
"author": "csdn.net",
"keywords": "栈,设计,队列"
}
\ No newline at end of file
# 用队列实现栈
<p>请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(<code>push</code><code>top</code><code>pop</code><code>empty</code>)。</p>
<p>实现 <code>MyStack</code> 类:</p>
<ul>
<li><code>void push(int x)</code> 将元素 x 压入栈顶。</li>
<li><code>int pop()</code> 移除并返回栈顶元素。</li>
<li><code>int top()</code> 返回栈顶元素。</li>
<li><code>boolean empty()</code> 如果栈是空的,返回 <code>true</code> ;否则,返回 <code>false</code></li>
</ul>
<p> </p>
<p><strong>注意:</strong></p>
<ul>
<li>你只能使用队列的基本操作 —— 也就是 <code>push to back</code><code>peek/pop from front</code><code>size</code> 和 <code>is empty</code> 这些操作。</li>
<li>你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。</li>
</ul>
<p> </p>
<p><strong>示例:</strong></p>
<pre>
<strong>输入:</strong>
["MyStack", "push", "push", "top", "pop", "empty"]
[[], [1], [2], [], [], []]
<strong>输出:</strong>
[null, null, null, 2, 2, false]
<strong>解释:</strong>
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // 返回 2
myStack.pop(); // 返回 2
myStack.empty(); // 返回 False
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= x <= 9</code></li>
<li>最多调用<code>100</code><code>push</code><code>pop</code><code>top</code><code>empty</code></li>
<li>每次调用 <code>pop</code><code>top</code> 都保证栈不为空</li>
</ul>
<p> </p>
<p><strong>进阶:</strong>你能否实现每种操作的均摊时间复杂度为 <code>O(1)</code> 的栈?换句话说,执行 <code>n</code> 个操作的总时间复杂度 <code>O(n)</code> ,尽管其中某个操作可能需要比其他操作更长的时间。你可以使用两个以上的队列。</p>
## template
```python
class MyStack:
def __init__(self):
"""
Initialize your data structure here.
"""
self.queue = []
self.help = []
def push(self, x):
"""
Push element x onto stack.
"""
while len(self.queue) > 0:
self.help.append(self.queue.pop(0))
self.queue.append(x)
while len(self.help) > 0:
self.queue.append(self.help.pop(0))
def pop(self):
"""
Removes the element on top of the stack and returns that element.
"""
de = self.queue.pop(0)
return de
def top(self):
"""
Get the top element.
"""
de = self.queue[0]
return de
def empty(self) -> bool:
"""
Returns whether the stack is empty.
"""
if len(self.queue) == 0:
return True
return False
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-86b0aaa37e4b44069fb95d809c4ef7de",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"source": "solution.md",
"exercise_id": "da18811d80254852b6599c3d89672d3d",
"author": "csdn.net",
"keywords": "树,深度优先搜索,广度优先搜索,二叉树"
}
\ No newline at end of file
# 翻转二叉树
<p>翻转一棵二叉树。</p>
<p><strong>示例:</strong></p>
<p>输入:</p>
<pre> 4
/ \
2 7
/ \ / \
1 3 6 9</pre>
<p>输出:</p>
<pre> 4
/ \
7 2
/ \ / \
9 6 3 1</pre>
<p><strong>备注:</strong><br>
这个问题是受到 <a href="https://twitter.com/mxcl" target="_blank">Max Howell </a><a href="https://twitter.com/mxcl/status/608682016205344768" target="_blank">原问题</a> 启发的 :</p>
<blockquote>谷歌:我们90%的工程师使用您编写的软件(Homebrew),但是您却无法在面试时在白板上写出翻转二叉树这道题,这太糟糕了。</blockquote>
## template
```python
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def invertTree(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
if not root:
return None
root.left, root.right = root.right, root.left
self.invertTree(root.left)
self.invertTree(root.right)
return root
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```
\ No newline at end of file
{
"node_id": "dailycode-fddac17d19ec4980b2c56a61a6606757",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"export": [
"solution.json"
]
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册