判断平衡二叉树
def isBalanced(self, root: TreeNode) -> bool:
def recur(root):
if not root: return 0;
# 左右子树不平衡直接返回 -1
left = recur(root.left)
if left == -1 :return -1
right = recur(root.right)
if right == -1 :return -1
# 如果平衡则加一,如果不平衡返回-1
return max(left, right) + 1 if abs(left - right) <= 1 else -1
return recur(root) != -1
循环打印矩阵
public static int[] spiralOrder(int[][] matrix) {
if(matrix.length == 0 || matrix[0].length == 0){
return new int[0];
}
int row = matrix.length;
int col = matrix[0].length;
int size = row*col;
int[] res = new int[size];
int index = 0;
int left = 0, top = 0, right = col - 1, bottom = row - 1;
while(index < size){
//从左往右走
for (int i = left; i <= right; i++) {
res[index++] = matrix[top][i];
}
if (++top > bottom) {
break;
}
//从上往下走
for (int i = top; i <= bottom; i++) {
res[index++] = matrix[i][right];
}
if (--right < left) {
break;
}
//从右往左走
for (int i = right; i >= left; i--) {
res[index++] = matrix[bottom][i];
}
if (--bottom < top) {
break;
}
//从下往上走
for (int i = bottom; i >= top; i--) {
res[index++] = matrix[i][left];
}
if (++left > right) {
break;
}
}
return res;
}
合并有序链表
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head = new ListNode(-1);
ListNode index = head;
while(l1 != null && l2 != null){
if(l1.val < l2.val){
index.next = l1;
index = l1;
l1 = l1.next;
}else{
index.next = l2;
index = l2;
l2 = l2.next;
}
}
if(l1 != null){
index.next = l1;
}
if(l2 != null){
index.next = l2;
}
return head.next;
}
矩阵路径
public boolean exist(char[][] board, String word) {
if (board == null || board[0] == null || board.length == 0 || board[0].length == 0 || word == null || word.equals("")) {
return false;
}
boolean[][] isVisited = new boolean[board.length][board[0].length];
char[] chs = word.toCharArray();
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == chs[0]) {
if (bfs(board, i, j, isVisited, chs, 0)) return true;
}
}
}
return false;
}
private boolean bfs(char[][] board, int i, int j, boolean[][] isVisited, char[] chs, int index) {
if (index == chs.length) {
return true;
}
if (i < 0 || j < 0 || i == board.length || j == board[0].length || isVisited[i][j] || board[i][j] != chs[index]) {
return false;
}
isVisited[i][j] = true;
boolean res = bfs(board, i + 1, j, isVisited, chs, index + 1)
|| bfs(board, i - 1, j, isVisited, chs, index + 1)
|| bfs(board, i, j + 1, isVisited, chs, index + 1)
|| bfs(board, i, j - 1, isVisited, chs, index + 1);
isVisited[i][j] = false;
return res;
}
判断入栈出栈
public boolean validateStackSequences(int[] pushed, int[] popped) {
if(pushed.length != popped.length){
return false;
}
Map<Integer,Integer> dicMap = new HashMap(pushed.length);
for(int i = 0;i<pushed.length;i++){
dicMap.put(pushed[i],i);
}
int res = 0;
for(int i = 0;i<popped.length-1;i++){
int one = dicMap.get(popped[i]);
int two = dicMap.get(popped[i+1]);
if (one < two && res > two){
return false;
}
res = Math.max(dicMap.get(popped[i]),res);
}
return true;
}
构建二叉树
Arrays.copyOfRange(T[ ] original,int from,int to)
[from,to)
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder.length == 0){
return null;
}
//找到中序遍历的头结点
int root = 0;
for(int i = 0;i < inorder.length;i++){
if(preorder[0] == inorder[i]){
root = i;
}
}
TreeNode rootNode = new TreeNode(preorder[0]);
rootNode.left = buildTree(Arrays.copyOfRange(preorder,1,root+1),
Arrays.copyOfRange(inorder,0,root));
rootNode.right = buildTree(Arrays.copyOfRange(preorder,root+1,preorder.length),
Arrays.copyOfRange(inorder,root+1,inorder.length));
return rootNode;
}