问题
Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.
循环,创建一个上界和一个下界。
当达到界限时,改变方向。
更新上界和下界的数值。
当上界小于下界时返回。
1 | class Solution { |
问题
Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.
循环,创建一个上界和一个下界。
当达到界限时,改变方向。
更新上界和下界的数值。
当上界小于下界时返回。
1 | class Solution { |
问题
According to Wikipedia’s article: “The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.”The board is made up of an m x n grid of cells, where each cell has an initial state: live (represented by a 1) or dead (represented by a 0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):
- Any live cell with fewer than two live neighbors dies as if caused by under-population.
- Any live cell with two or three live neighbors lives on to the next generation.
- Any live cell with more than three live neighbors dies, as if by over-population.
- Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the m x n grid board, return the next state.
辅助方法,计算每个位置四周有生命的总和。
根据规则填写到新数组。
1 | class Solution { |
145. Binary Tree Postorder Traversal
问题
Given the root of a binary tree, return the postorder traversal of its nodes’ values.
后序遍历。
先递归左子节点。
然后递归右子节点。
最后将当前节点加入数组。
1 | /** |
94. Binary Tree Inorder Traversal
问题
Given the root of a binary tree, return the inorder traversal of its nodes’ values.
中序遍历。
先递归左子节点。
然后将当前节点加入数组。
最后递归右子节点。
1 | /** |
144. Binary Tree Preorder Traversal
问题
Given the root of a binary tree, return the preorder traversal of its nodes’ values.
先序遍历。
先将当前节点加入数组。
然后递归左子节点。
最后递归右子节点。
1 | /** |
232. Implement Queue using Stacks
问题
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty).Implement the MyQueue class:
void push(int x) Pushes element x to the back of the queue.
int pop() Removes the element from the front of the queue and returns it.
int peek() Returns the element at the front of the queue.
boolean empty() Returns true if the queue is empty, false otherwise.
Notes:You must use only standard operations of a stack, which means only push to top, peek/pop from top, size, and is empty operations are valid.
Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack’s standard operations.
创建两个栈。
当入队列时,将元素压入第一个栈。
当出队列或进行其他操作时,如第二个栈为空,则将第一个栈的元素倒出到第二个栈。
此时第二个栈内的内容为顺序。
1 | class MyQueue { |
问题
Given a 2D grid of size m x n and an integer k. You need to shift the grid k times.In one shift operation:
Element at grid[i][j] moves to grid[i][j + 1].
Element at grid[i][n - 1] moves to grid[i + 1][0].
Element at grid[m - 1][n - 1] moves to grid[0][0].
Return the 2D grid after applying shift operation k times.
遍历整个数组,将索引值加上移动的次数,得到新的位置。
1 | class Solution { |
问题
You are keeping score for a baseball game with strange rules. The game consists of several rounds, where the scores of past rounds may affect future rounds’ scores.At the beginning of the game, you start with an empty record. You are given a list of strings ops, where ops[i] is the ith operation you must apply to the record and is one of the following:
- An integer x - Record a new score of x.
- “+” - Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores.
- “D” - Record a new score that is double the previous score. It is guaranteed there will always be a previous score.
- “C” - Invalidate the previous score, removing it from the record. It is guaranteed there will always be a previous score.
Return the sum of all the scores on the record.
遍历选项,根据内容决定对ArrayList的操作。
然后遍历将ArrayList加和,返回。
1 | class Solution { |
问题
You are given an m x n grid where each cell can have one of three values:
- 0 representing an empty cell,
- 1 representing a fresh orange, or
- 2 representing a rotten orange.
Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten.Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1.
由于腐烂的橘子每次都只能影响周围的橘子,因此采用BFS。
将所有腐烂的橘子加入队列。
如果没有新鲜的橘子,则返回0。
每次出队列,如果周围有新鲜的橘子存在,则将新鲜的橘子替换为腐烂并加入队列。
每个level结束后,time+1。
最后遍历一遍,如果还有新鲜的橘子,返回-1。
1 | class Solution { |
问题
Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell.
The distance between two adjacent cells is 1.
由于是搜索最近的距离,因此可以采用BFS搜索。
首先创建一个距离矩阵,将所有原矩阵为0的位置填上距离0,将其他位置填上无穷大。
使用BFS搜索,将所有0的坐标放入队列。
取出队列头元素,将其周围的距离矩阵的元素与自身距离矩阵的元素+1比较,将较小的值设置在周围的距离矩阵上。
同时,将改变数值的坐标再次放入队列。
1 | class Solution{ |
116. Populating Next Right Pointers in Each Node
问题
You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
1
2
3
4
5
6 struct Node {
int val;
Node *left;
Node *right;
Node *next;
>}Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
Initially, all next pointers are set to NULL.
递归,当root为null时返回。
如果root有右节点,则左节点next指向右节点。
如果root有右节点同时next已经指向了一个节点,则将其右节点next指向该节点的左子节点。
递归左右子节点,并返回root。
1 | /* |
BFS搜索每一个节点,将节点指向队列中next下一个节点。
当计数器达到2的指数时,将节点指向null。
1 | class Solution { |
问题
Given a string s containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, determine if the input string is valid.An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
用栈储存遍历中的字符。
如果是“(”,“{”或“[”,则入栈。
如果是其他字符,且不与栈顶的字符成对,则返回false。
其他情况需要pop掉栈顶。
1 | class Solution { |
问题
You are given two binary trees root1 and root2.Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree.
Return the merged tree.
Note: The merging process must start from the root nodes of both trees.
递归。将root1和root2合并到root1。
如果一个节点为null,则返回另一个节点。
否则root1的值为root1 + root2的值。
root1.left递归root1和root2的left。
root2.right递归root1和root2的right。
返回root1。
1 | /** |
83. Remove Duplicates from Sorted List
问题
Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.
设置前一个节点和当前节点两个指针。
由于是有数的链表,遍历时可以直接比较两个节点。
如相等则前一个节点的next指向当前节点的next。
1 | /** |
You are given an m x n binary matrix grid. An island is a group of 1’s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
The area of an island is the number of cells with a value 1 in the island.
Return the maximum area of an island in grid. If there is no island, return 0.
DFS搜索, 维护一个变量count,为连续执行DFS搜索的次数。
遍历地图上所有为1的位置。
运行BFS时每次将全局变量count增加1。
执行完毕后BFS时将全局变量count清零。
遍历每个等于1的地图块。
递归周围四个地图块,当超越数组范围,或者当前位置不为1时返回。
进行搜索时将搜索过的地图块标记为0,不再重复搜索。
1 | class Solution { |