剑指 Offer 11. 旋转数组的最小数字
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
T(n) : O(n)
S(n) : O(n)
1 | class Solution { |
T(n) : O(nlogn)
S(n) : O(1)
1 | class Solution { |
对于当走到i时,如果i处的数字不是i的话,就把这个数字移动到正确的位置nums[nums[i]],然后对交换过来的数字继续判断是不是==i。
这样一来所有的数字都会被交换到正确的位置,当碰到重复数字时,那他一定不在正确的位置,这时候想把它交换过去的话,就会发现,唉!交换过去的位置已经有他了,那么就可以判断出来时重复了
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
1 | class CQueue { |
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
26进制转为10进制,就n转10差不多
1 | class Solution { |
review 1
2
3
4
5
6
7
8
9class Solution {
public:
int titleToNumber(string columnTitle) {
int ret{ 0 };
for(auto ch : columnTitle)
ret = ret * 26 + (ch - 'A' + 1); // 注意这个括号是必要的,因为加法符号是左结合,所以如果没括号会先加ch导致溢出。
return ret;
}
};
1 | class Solution { |
当且仅当和边界相连的O块区域才可能被表留,而在中间的就全都被覆盖,因此先沿着边界检查,把所有这种O区域改成1表示要保留的,然后把里面剩下的O改成X,1改回O就行了
1 | class Solution { |
关于stackoverflow https://leetcode.com/problems/surrounded-regions/discuss/41612/A-really-simple-and-readable-C++-solutionuff0conly-cost-12ms/39811
每个递归调用32B存储空间(1个int)(有待考证),然后250*250
就占用32 * 250 * 250 / 2 = 1000000B = 1M = 栈大小
review
1 | class Solution { |
1 | /** |
S(n) : O(logn)
由inorder的顺序来看,很神奇,找规律
1 | Nice work. However, I think it'd better to make this part more clear: the number of times that you will find prev.val >= root.val depends on whether the two nodes that get swapped are next to each other in the sequence of in-order traversal. |
1 | How do we find these two elements? For example, we have the following tree that is printed as in order traversal: |
迭代实现 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void recoverTree(TreeNode* root) {
TreeNode* first = nullptr, *second = nullptr, *prev = nullptr, *cur = root;
stack<TreeNode*> s;
while(!s.empty() || cur)
{
while(cur)
{
s.push(cur);
cur = cur->left;
}
cur = s.top();
s.pop();
if(prev && prev->val > cur->val)
{
if(!first)
first = prev;
if(first)
second = cur;
if(prev != first)
break;
}
prev = cur;
cur = cur->right;
}
swap(first->val, second->val);
}
};
https://leetcode.com/problems/recover-binary-search-tree/discuss/32559/Detail-Explain-about-How-Morris-Traversal-Finds-two-Incorrect-Pointer
1 | class Solution { |
1 | /* |
1 | /* |
每层遍历下去,每层从最左边开始,然后操作他的儿子们,当一层没儿子就停止
递归
1 | /* |