This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dfs(root: TreeNode) -> int: | |
if not root: | |
return 0 | |
return 1 + max(dfs(root.left), dfs(root.right)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dfs(self,root: TreeNode) -> int: | |
if not root: | |
return 0 | |
cur_level=[root] | |
res = 0 | |
while cur_level: | |
# 为 next数组申请空间 | |
next_level=[] | |
res +=1 | |
for node in cur_level: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 去重 ---> 满足两个条件,不是第一个元素【下标非零】,其次与前一个数不相等 | |
// 注意迭代器申明 | |
vector<int> :: iterator unique(vector<int> &a) { | |
int j = 0; | |
for (int i = 0; i < a.size(); i++) { | |
// 下标的值域为 [0, n] | |
if (!i || a[i] != a[i - 1]) { | |
a[j++] = a[i]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = 3 | |
# 外层控制层数,生成几行;内层控制每行中的数字,外层作为因子控制每次相加的n的倍数 | |
matrix = [[s + n * k for s in range(1, n+1)] for k in range(0, n)] | |
print(matrix) | |
# version 2 | |
m, n = 7, 4 | |
matrix = [[k * n + 1 * s for s in range(1, n)] for k in range(0, m)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void reverse(char * str, int strSize) { | |
for (int j = 0; j < strSize / 2; j++) { | |
// 区间二分,对换字符串,只需要交换前 2 / 1 | |
char tmp = str[j]; | |
str[j] = str[strSize - j - 1]; | |
str[strSize - j - 1] = tmp; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def reverse(char:list, charSize:int) ->None: | |
# 遍历数组的前半部分 | |
for j in range(0, charSize // 2): | |
# 设置中间变量,用于交换; | |
# 将用于交换的部分赋值给中间变量 | |
tmp = char[j] | |
# 将左半边的值赋值为右半边的值 | |
char[j] = char[charSize - j - 1] | |
# 将右半边值赋值为左半边的值 | |
char[charSize - j - 1] = tmp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stack> | |
using namespace std; | |
int main() { | |
stack<int> stack; | |
stack.push(21); | |
stack.push(22); | |
stack.push(24); | |
stack.push(25); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 宏定义,相当于匿名函数,因此需要括号包裹,但三元运算符后面的部分,相当于返回值,因此不需要括号包裹 | |
#define MAX(a, b) ((a) > (b) ? a : b) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 内联函数,注意原地修改,因此传参均为字符指针,不需要返回值 | |
static inline void swap(char* a, char* b) { | |
char c = *a; | |
*a = *b; | |
*b = c; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) { | |
// 多值排序,使用匿名函数 [](){} 没有不能访问的变量,形参; 排序规则:身高递增(数组位序0)或者 (身高相等排序人数(数组位序1),递减顺序) | |
sort(people.begin(), people.end(), [](const vector<int>& u, const vector<int>& v) { | |
return u[0] < v[0] || (u[0] == v[0] && u[1] > v[1]); | |
}); | |
int n = people.size(); | |
vector<vector<int>> ans(n); | |
for (const vector<int>& person: people) { | |
int spaces = person[1] + 1; | |
for (int i = 0; i < n; i++) { |
OlderNewer