1.用栈实现队列
private Stack<Integer> s1 ;
private Stack<Integer> s2 ;
public MyQueue() {
s1 = new Stack<>();
s2 = new Stack<>();
}
public void push(int x) {
s1.add(x);
}
public int pop() {
if (s1.isEmpty()&&s2.isEmpty()){
return -1;
}
if (!s1.isEmpty()&&s2.isEmpty()){
while (!s1.isEmpty()){
s2.add(s1.pop());
}
}
return s2.pop();
}
public int peek() {
if (s1.isEmpty()&&s2.isEmpty()){
return -1;
}
if (!s1.isEmpty()&&s2.isEmpty()){
while (!s1.isEmpty()){
s2.add(s1.pop());
}
}
return s2.peek();
}
public boolean empty() {
if (s1.isEmpty()&&s2.isEmpty()){
return true;
}else {
return false;
}
}
2.用队列实现栈
class MyStack {
private Queue<Integer> q1;
private Queue<Integer> q2;
public MyStack() {
q1 = new LinkedList<>();
q2 = new LinkedList<>();
}
public void push(int x) {
q1.add(x);
}
public int pop() {
if (q1.isEmpty() && q2.isEmpty()) {
return -1;
}
int temp = 0;
if (!q1.isEmpty()) {
while (!q1.isEmpty()) {
temp = q1.remove();
q2.add(temp);
}
q2.remove(temp);
} else {
while (!q2.isEmpty()) {
temp = q2.remove();
q1.add(temp);
}
q1.remove(temp);
}
return temp;
}
public int top() {
if (q1.isEmpty() && q2.isEmpty()) {
return -1;
}
int temp = 0;
if (!q1.isEmpty()) {
while (!q1.isEmpty()) {
temp = q1.remove();
q2.add(temp);
}
} else {
while (!q2.isEmpty()) {
temp = q2.remove();
q1.add(temp);
}
}
return temp;
}
public boolean empty() {
if (q1.isEmpty() && q2.isEmpty()) {
return true;
} else {
return false;
}
}
}
3. 01 矩阵
class Solution {
public int[][] updateMatrix(int[][] matrix) {
int len = matrix.length, len2 = matrix[0].length;
int[][] ans = new int[len][len2];
for (int i =0;i<len;i++){
for (int j = 0;j<len2; j++){
ans[i][j]=BFS(matrix,len,len2,i,j);
}
}
return ans;
}
private int BFS(int[][] matrix, int len, int len2, int i, int j) {
int count=0;
Queue<Integer> queue = new LinkedList<>();
Set<Integer> visited = new HashSet<>();
queue.add(i*len2+j);
while (!queue.isEmpty()){
int size = queue.size();
for (int k=0;k<size;k++){
int temp = queue.remove();
int a = temp/len2,b = temp%len2;
if (visited.contains(temp)) continue;
else visited.add(temp);
if (matrix[a][b]==0){
return count;
}
if (a-1>=0) queue.add((a-1)*len2+b);
if (a+1<len) queue.add((a+1)*len2+b);
if (b-1>=0) queue.add(a*len2+b-1);
if (b+1<len2) queue.add(a*len2+b+1);
}
count++;
}
return count;
}
}
4.图像渲染
class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
int len = image.length,len2 = image[0].length;
int target = image[sr][sc];
BFS(image,len,len2,sr,sc,target,newColor);
return image;
}
private void BFS(int[][] image, int len, int len2, int sr, int sc,int target ,int newColor) {
Queue<Integer> queue = new LinkedList<>();
Set<Integer> visited = new HashSet<>();
queue.add(sr*len2+sc);
image[sr][sc]=newColor;
while (!queue.isEmpty()){
int size = queue.size();
for (int i=0;i<size;i++){
int temp = queue.poll(),a = temp/len2,b=temp%len2;
if (visited.contains(temp)) continue;
else visited.add(temp);image[a][b]=newColor;
if (a-1>=0&&image[a-1][b]==target) queue.add((a-1)*len2+b);
if (a+1<len&&image[a+1][b]==target) queue.add((a+1)*len2+b);
if (b-1>=0&&image[a][b-1]==target) queue.add(a*len2+b-1);
if (b+1<len2&&image[a][b+1]==target) queue.add(a*len2+b+1);
}
}
}
}
class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
int len = image.length,len2 = image[0].length;
int target = image[sr][sc];
if (target!=newColor){
DFS(image,len,len2,sr,sc,target,newColor);
}
return image;
}
private void DFS(int[][] image, int len, int len2, int a, int b, int target, int newColor) {
image[a][b]=newColor;
if (a-1>=0&&image[a-1][b]==target) DFS(image,len,len2,a-1,b,target,newColor);;
if (a+1<len&&image[a+1][b]==target) DFS(image,len,len2,a+1,b,target,newColor);;
if (b-1>=0&&image[a][b-1]==target) DFS(image,len,len2,a,b-1,target,newColor);;
if (b+1<len2&&image[a][b+1]==target) DFS(image,len,len2,a,b+1,target,newColor);;
}
}
5.钥匙和房间
class Solution {
private int count;
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
count = BFS(rooms,0);
if (count == rooms.size()) {
return true;
}
return false;
}
private int BFS(List<List<Integer>> rooms, int i) {
Queue<Integer> queue = new LinkedList<>();
Set<Integer> visited = new HashSet<>();
queue.add(i);
while (!queue.isEmpty()){
int temp = queue.poll();
if (visited.contains(temp)) continue;
else visited.add(temp);
for (int j=0;j<rooms.get(temp).size();j++){
queue.add(rooms.get(temp).get(j));
}
count++;
}
return count;
}
}
6.字符串解码
搞了半天,搞混了,以后再看看