1、将图像顺时针旋转90°
def rotate(array):
hei = len(array)
wid = len(array[0])
for i in range(hei):
for j in range(i, wid):
tmp = array[i][j]
array[i][j] = array[j][i]
array[j][i] = tmp
tt = int(wid/2)
for i in range(hei):
for j in range(tt):
tmp = array[i][j]
array[i][j] = array[i][wid-j-1]
array[i][wid - j - 1] = tmp
return array
2、括号匹配
def check_par(strarr):
SYMBOLS = {'}': '{', ']': '[', ')': '(', '>': '<'}
ret = []
for c in strarr:
if c in SYMBOLS.values():
ret.append(c)
elif c in SYMBOLS.keys():
if ret and ret[-1] == SYMBOLS[c]:
ret.pop()
else:
return False
if ret:
return False
else:
return True
3、镜像二叉树
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
void Mirror(TreeNode *pRoot) {
if (pRoot == NULL || (pRoot->left == NULL && pRoot->right == NULL)){
return;
}
TreeNode* tmp = pRoot->left;
pRoot->left = pRoot->right;
pRoot->right = tmp;
if (pRoot->left){
Mirror(pRoot->left);
}
if (pRoot->right){
Mirror(pRoot->right);
}
}
};