leetcode两数相加求大神翻译一下代码中每一句是什么意思

45722 条评论分享收藏感谢收起赞同 30548 条评论分享收藏感谢收起LeetCode 495. Teemo Attacking 题目翻译+AC代码
英文原题:
In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo’s attacking ascending time series towards Ashe and the poisoning time duration per Teemo’s attacking, you need to output the total time that Ashe is in poisoned condition.
You may assume that Teemo attacks at the very beginning of a specific time point, and makes Ashe be in poisoned condition immediately.
Example 1:
Input: [1,4], 2
Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned immediately.
This poisoned status will last 2 seconds until the end of time point 2.
And at time point 4, Teemo attacks Ashe again, and causes Ashe to be in poisoned status for another 2 seconds.
So you finally need to output 4.
Example 2:
Input: [1,2], 2
Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned.
This poisoned status will last 2 seconds until the end of time point 2.
However, at the beginning of time point 2, Teemo attacks Ashe again who is already in poisoned status.
Since the poisoned status won't add up together, though the second poisoning attack will still work at time point 2, it will stop at the end of time point 3.
So you finally need to output 3.
1.You may assume the length of given time series array won`t exceed 10000.
2.You may assume the numbers in the Teemo’s attacking time series and his poisoning time duration per attacking are non-negative integers, which won’t exceed 10,000,000.
在LLP的世界里,有一个名叫Teemo的英雄,他的攻击可以让他的敌人Ashe陷入中毒的状态。现在,我们将给出Teemo对Ashe发起攻击的时间点的上升序列,以及每次攻击后Ashe中毒状态持续的时间,你需要输出Ashe处于中毒状态的时间总和。
假设Teemo的攻击发起在每个时间点的最开端,并且在发起攻击时便立刻使Ashe陷入中毒状态。
输入:[1.4],2
解释:在第1个时间单位,对发起进攻,并使立刻处于中毒状态。的这种中毒状态将持续2个时间单位,直到第2个时间单位结束,中毒状态结束。
在第4个时间单位,再次对发起进攻,使再次陷入持续2个时间单位的中毒状态中。
所以,你最终输出的结果是4。
输入:[1,2],2
解释:在第1个时间单位,对发起攻击并使立刻处于中毒状态。的这种中毒状态将持续2个时间单位,直到第2个时间单位结束,中毒状态结束。
然而,在第2个时间单位一开始,便对已经处在中毒状态的再次发起了攻击。
中毒状态不会叠加,不过第二次攻击对带来的中毒作用仍然会在第2个时间单位开始,的中毒状态将会在第3个时间单位结束时结束。
所以,你最终输出结果是3。
1.假设时间点序列的长度不超过10000。
2.假设时间点序列中的时间点和中毒状态持续时间都是非负整数,而且不超过10000。
解题思路:
只需要注意一点,中毒时间重叠时,不能重复计数(也就是第二个输入样例中出现的情况)。
class Solution {
int findPoisonedDuration(vector&int&& timeSeries, int duration) {
int cnt = 0;
if (timeSeries.size() == 0)
for (int i = 0; i & (int)timeSeries.size()-1; i++) {
for (int k = 0; k & k++) {
if ((timeSeries[i] + k) != timeSeries[i + 1]) {
return cnt +
Teemo Attacking
LeetCode 495. Teemo Attacking
leetcode495 Teemo Attacking Java
495. Teemo Attacking(Java)
【LeetCode】495. Teemo Attacking
Leetcode——495. Teemo Attacking
LeetCode 38 Count and Say(计数与报数)
leetcode21题 题解 翻译 C语言版 Python版
【LeetCode】495. Teemo Attacking【M】【41】
leetcode 495. Teemo Attacking 提莫攻击 + 计算间隔
没有更多推荐了,風立ちぬ、いざ生きめやも」
LeetCode 54 Spiral Matrix(螺旋矩阵)(Array)(*)
给定一个m*n的矩阵(m行 n列),以螺旋状返回矩阵中的所有元素。
例如,给定以下矩阵
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
你应该返回 [1,2,3,6,9,8,7,4,5] .
Given a matrix of m*n elements (m rows, n columns), return all elements of the matrix in spiral order.
For example,
Given the following matrix:
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
You should return [1,2,3,6,9,8,7,4,5] .
一开始我自以为很容易一眼就看出规律来,不过是用四个变量分别表示横纵坐标的起始点、再用两个变量表示当前的坐标,然后慢慢循环就Ok了。好吧,也许可以,但这种方法以后再也不想用了,实在是浪费时间又让人烦躁。
直接取看了别人的解法,其实有异曲同工,不过这个解法变量用的少了控制却更加巧妙。
向上的行是u,向下的行是d,向左的列是l,向右的列是r。
k是spiral的当前索引,不断往后走。
控制结束的是这4个方向的索引的大小比较。
class Solution {
vector&int& spiralOrder(vector&vector&int&&& matrix) {
if (matrix.empty()) return {};
int m = matrix.size(), n = matrix[0].size();
vector&int& spiral(m * n);
int u = 0, d = m - 1, l = 0, r = n - 1, k = 0;
while (true) {
for (int col = col &= col++) spiral[k++] = matrix[u][col];
if (++u & d) break;
for (int row = row &= row++) spiral[k++] = matrix[row][r];
if (--r & l) break;
for (int col = col &= col--) spiral[k++] = matrix[d][col];
if (--d & u) break;
for (int row = row &= row--) spiral[k++] = matrix[row][l];
if (++l & r) break;
updated at 2016/09/04
public List&Integer& spiralOrder(int[][] matrix) {
List&Integer& spiral = new ArrayList&&();
if (matrix.length & 1) return
int m = matrix.length, n = matrix[0].
int u = 0, d = m - 1, l = 0, r = n - 1, k = 0;
while (true) {
for (int col = col &= col++) spiral.add(k++, matrix[u][col]);
if (++u & d) break;
for (int row = row &= row++) spiral.add(k++, matrix[row][r]);
if (--r & l) break;
for (int col = col &= col--) spiral.add(k++, matrix[d][col]);
if (--d & u) break;
for (int row = row &= row--) spiral.add(k++, matrix[row][l]);
if (++l & r) break;
扫码向博主提问
擅长领域:
没有更多推荐了,36239 条评论分享收藏感谢收起赞同 18421 条评论分享收藏感谢收起風立ちぬ、いざ生きめやも」
LeetCode 235 Lowest Common Ancestor of a Binary Search Tree(二叉搜索树的最小公共祖先)
给定一个二叉搜索树(BST),找出两个给定节点的最小公共祖先(LCA)。
根据维基百科对于LCA的定义:“最小公共祖先的定义是对于两个节点v和s有一个最小的节点T,
以至于v和s都是T的后代(其中我们允许节点是自身的后代)。”
_______6______
例如,对于节点2和8的最小公共祖先是节点6.
另一个是2和4的LCA是2,因为根据LCA的定义,一个节点的是自身是后代。
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined
between two nodes v and w as the lowest node in T that has both v and w as descendants
(where we allow a node to be a descendant of itself).”
_______6______
For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6.
Another example is LCA of nodes 2 and 4 is 2,
since a node can be a descendant of itself according to the LCA definition.
这个时候一定要先知道什么是BST,那么就来回顾一下:
图片来源于维基百科(有这么权威的资料,我就不自己瞎说了,逃……
博客发完之后发现图片看不清了,那就把文字节选下来好了,thanks,Wikipedia!
二叉查找树(英语:Binary Search Tree),也称二叉搜索树、有序二叉树(英语:ordered binary tree),排序二叉树(英语:sorted binary tree),
是指一棵空树或者具有下列性质的二叉树:
若任意节点的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
任意节点的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
任意节点的左、右子树也分别为二叉查找树;
没有键值相等的节点。
还是用擅长的递归吧,我好慌,每次都是效率不行的递归。
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (root == p || root == q) return
if (root == NULL || p == NULL || q == NULL) return NULL;
if (p-&val & root-&val && q-&val & root-&val) {
lowestCommonAncestor(root-&left, p, q);
else if (p-&val & root-&val && q-&val &root-&val) {
lowestCommonAncestor(root-&right, p, q);
根据LCA的定义,可以是节点自身,那么如果相等的话就可以直接返回啦。
如果任何一个为空了,结果不也为空么?
其余的两种情况,对应这个图来看看。对于3和5,如果(2)比它们都小,那就应该往右走了;对于0和2,如果(6)比它们都大,那就应该往左走了;对于3和7,如果(6)在它们中间,那就直接返回了。
_______6______
再高级的解法我还没想到,以后再补充~
* Definition for a binary tree node.
* struct TreeNode {
TreeNode *
TreeNode *
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
class Solution {
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (root == p || root == q) return
if (root == NULL || p == NULL || q == NULL) return NULL;
if (p-&val & root-&val && q-&val & root-&val) {
lowestCommonAncestor(root-&left, p, q);
else if (p-&val & root-&val && q-&val &root-&val) {
lowestCommonAncestor(root-&right, p, q);
扫码向博主提问
擅长领域:
LeetCode 236. Lowest Common Ancestor of a Binary Tree(二叉树的最低公共祖先)
leetcode 235: Lowest Common Ancestor of a Binary Search Tree
LeetCode 235: Lowest Common Ancestor of a Binary Search Tree
leetcode 236: Lowest Common Ancestor of a Binary Tree
235. Lowest Common Ancestor of a Binary Search Tree [easy] (Python)
[Java]Leetcode236 Lowest Common Ancestor of a Binary Tree
LeetCode_Lowest Common Ancestor
[LeetCode] 236 Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点
235 二叉搜索树的最小共同父节点
236. Lowest Common Ancestor of a Binary Tree
[Java]LeetCode235 Lowest Common Ancestor of a Binary Search Tree
没有更多推荐了,}

我要回帖

更多关于 用一句代码形容自己 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信