200 岛屿数量
题目:给你一个由 '1'(陆地)和 '0'(水)组成的的二维网格,请你计算网格中岛屿的数量。岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
此外,你可以假设该网格的四条边均被水包围。
java
class Solution {
public static int[][] path={{0,1},{1,0},{-1,0},{0,-1}};
public int numIslands(char[][] grid) {
int m=grid.length;
int n=grid[0].length;
boolean[][] visited=new boolean[m][n];
int ans=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(!visited[i][j] && grid[i][j]=='1'){
ans++;
visited[i][j]=true;
dfs(visited,i,j,grid);
}
}
}
return ans;
}
private void dfs(boolean[][] visited,int x,int y,char[][] grid){
for(int i=0;i<4;i++){
int nextx=x+path[i][0];
int nexty=y+path[i][1];
if(nextx<0 || nexty<0 ||nextx>=grid.length || nexty>=grid[0].length) continue;
if(!visited[nextx][nexty] && grid[nextx][nexty]=='1'){
visited[nextx][nexty]=true;
dfs(visited,nextx,nexty,grid);
}
}
}
}
994 腐烂的橘子
题目:在给定的 m x n 网格 grid 中,每个单元格可以有以下三个值之一:值 0 代表空单元格;值 1 代表新鲜橘子;值 2 代表腐烂的橘子。每分钟,腐烂的橘子周围 4 个方向上相邻的新鲜橘子都会腐烂。返回直到单元格中没有新鲜橘子为止所必须经过的最小分钟数。如果不可能,返回 -1 。
java
class Solution {
private static final int[][] directions={{-1,0},{1,0},{0,-1},{0,1}};
public int orangesRotting(int[][] grid) {
int m=grid.length;
int n=grid[0].length;
int fresh=0;
List<int[]> q=new ArrayList<>();
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(grid[i][j]==1){
fresh++;
}else if(grid[i][j]==2){
q.add(new int[]{i,j});
}
}
}
int ans=0;
while(fresh>0 && !q.isEmpty()){
ans++;
List<int[]> tmp=q;
q=new ArrayList<>();
for(int[] pos:tmp){
for(int[] d:directions){
int i=pos[0]+d[0];
int j=pos[1]+d[1];
if(i>=0 && i<m && j>=0 && j<n &&grid[i][j]==1){
fresh--;
grid[i][j]=2;
q.add(new int[]{i,j});
}
}
}
}
return fresh>0?-1:ans;
}
}
207课程表
题目:你这个学期必须选修 numCourses 门课程,记为 0 到 numCourses - 1 。在选修某些课程之前需要一些先修课程。 先修课程按数组 prerequisites 给出,其中 prerequisites[i] = [ai, bi] ,表示如果要学习课程 ai 则必须先学习课程 bi 。例如,先修课程对 [0, 1] 表示:想要学习课程 0 ,你需要先完成课程 1。请你判断是否可能完成所有课程的学习?如果可以,返回 true ;否则,返回 false 。
java
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
List<Integer>[] g=new ArrayList[numCourses];
Arrays.setAll(g,i->new ArrayList<>());
for(int[] p:prerequisites){
g[p[1]].add(p[0]);
}
int[] colors=new int[numCourses];
for(int i=0;i<numCourses;i++){
if(colors[i]==0 && dfs(i,g,colors)){
return false;
}
}
return true;
}
private boolean dfs(int x,List<Integer>[] g,int[] colors){
colors[x]=1;
for(int y:g[x]){
if(colors[y]==1 || colors[y]==0 && dfs(y,g,colors)){
return true;
}
}
colors[x]=2;
return false;
}
}
208实现前缀树
题目:Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补全和拼写检查。
请你实现 Trie 类:Trie() 初始化前缀树对象。
void insert(String word) 向前缀树中插入字符串 word 。
boolean search(String word) 如果字符串 word 在前缀树中,返回 true(即,在检索之前已经插入);否则,返回 false 。
boolean startsWith(String prefix) 如果之前已经插入的字符串 word 的前缀之一为 prefix ,返回 true ;否则,返回 false 。
java
class Trie {
private static class Node{
Node[] son=new Node[26];
boolean end=false;
}
private final Node root=new Node();
public Trie() {
}
public void insert(String word) {
Node cur=root;
for(char c:word.toCharArray()){
c-='a';
if(cur.son[c]==null){ // 无路可走?
cur.son[c]=new Node(); // new 出来
}
cur=cur.son[c];
}
cur.end=true;
}
public boolean search(String word) {
return find(word)==2;
}
public boolean startsWith(String prefix) {
return find(prefix)!=0;
}
private int find(String word){
Node cur=root;
for(char c:word.toCharArray()){
c-='a';
if(cur.son[c]==null){
return 0;
}
cur=cur.son[c];
}
return cur.end? 2:1; // 走过同样的路(2=完全匹配,1=前缀匹配)
}
}