点击跳转到题目位置
给你一个由 ‘1’(陆地)和 ‘0’(水)组成的的二维网格,请你计算网格中岛屿的数量。
岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
此外,你可以假设该网格的四条边均被水包围。
提示:
class Solution {
int[][] dir = {
{-1, 0},
{1, 0},
{0, 1},
{0, -1},
};
public int numIslands(char[][] grid) {
Map<Integer, Integer> hash = new HashMap<Integer, Integer>();
int m = grid.length;
int n = grid[0].length;
int ret = 0;
Queue<Integer> q = new LinkedList();
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
if(grid[i][j] == '1' && !hash.containsKey(500 * i + j)){
q.offer(500 * i + j);
hash.put(500 * i + j, 1);
++ret;
}
while(!q.isEmpty()){
int num = q.peek();
q.poll();
int x = num / 500;
int y = num % 500;
for(int k = 0; k < 4; ++k){
int tx = x + dir[k][0];
int ty = y + dir[k][1];
if(tx < 0 || tx >= m || ty < 0 || ty >= n || grid[tx][ty] == '0'){
continue;
}
if(!hash.containsKey(tx * 500 + ty)){
hash.put(tx * 500 + ty, 1);
q.offer(tx * 500 + ty);
}
}
}
}
}
return ret;
}
}
点击跳转到题目位置
在给定的 m x n 网格 grid 中,每个单元格可以有以下三个值之一:
返回 直到单元格中没有新鲜橘子为止所必须经过的最小分钟数。如果不可能,返回 -1 。
提示:
class Solution {
int[][] dir = {
{-1, 0},
{1, 0},
{0, -1},
{0, 1},
};
public int orangesRotting(int[][] grid) {
Queue<Integer> q = new LinkedList<Integer>();
int m = grid.length;
int n = grid[0].length;
int num = 0;
int res = 0;
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
if(grid[i][j] == 1){
num++;
}
if(grid[i][j] == 2){
q.offer(i * 100 + j);
}
}
}
if(num == 0){
return 0;
}
while(!q.isEmpty()){
int len = q.size();
System.out.println(len);
res++;
for(int i = 0; i < len; ++i){
int curr = q.peek();
q.poll();
int x = curr / 100;
int y = curr % 100;
for(int k = 0; k < 4; ++k){
int tx = dir[k][0] + x;
int ty = dir[k][1] + y;
if(tx < 0 || tx >= m || ty < 0 || ty >= n){
continue;
}
if(grid[tx][ty] == 1){
grid[tx][ty] = 2;
q.offer(tx * 100 + ty);
num--;
if(num == 0){
System.out.println("ggggggg");
break;
}
}
}
if(num == 0){
break;
}
}
if(num == 0){
break;
}
}
return num == 0 ? res : -1;
}
}
点击跳转到题目位置
你这个学期必须选修 numCourses 门课程,记为 0 到 numCourses - 1 。
在选修某些课程之前需要一些先修课程。 先修课程按数组 prerequisites 给出,其中 prerequisites[i] = [ai, bi] ,表示如果要学习课程 ai 则 必须 先学习课程 bi 。
请你判断是否可能完成所有课程的学习?如果可以,返回 true ;否则,返回 false 。
提示:
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
List<List<Integer>> edge = new ArrayList<>();
for (int i = 0; i < numCourses; ++i) {
edge.add(new ArrayList<>());
}
int[] deg = new int[numCourses];
int n = prerequisites.length;
for(int i = 0; i < n; ++i){
int x = prerequisites[i][0];
int y = prerequisites[i][1];
deg[x]++;
edge.get(y).add(x);
}
Queue<Integer> q = new LinkedList<Integer>();
for(int i = 0; i < numCourses; ++i){
if(deg[i] == 0){
q.offer(i);
}
}
while(!q.isEmpty()){
int x = q.peek();
q.poll();
numCourses--;
for(int i = 0; i < edge.get(x).size(); ++i){
int num = edge.get(x).get(i);
deg[num]--;
if(deg[num] == 0){
q.offer(num);
}
}
}
return numCourses == 0;
}
}
点击跳转到题目位置
Trie(发音类似 “try”)或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补全和拼写检查。
请你实现 Trie 类:
提示:
class TrieNode{
TrieNode[] next;
boolean end;
TrieNode(){
next = new TrieNode[26];
end = false;
}
}
class Trie {
TrieNode root;
public Trie() {
root = new TrieNode();
}
public void insert(String word) {
TrieNode now = root;
for(int i = 0; i < word.length(); ++i){
int child = word.charAt(i) - 'a';
if(now.next[child] == null){
now.next[child] = new TrieNode();
}
now = now.next[child];
}
now.end = true;
}
public boolean search(String word) {
TrieNode now = root;
for(int i = 0; i < word.length(); ++i){
int child = word.charAt(i) - 'a';
if(now.next[child] == null){
return false;
}
now = now.next[child];
}
return now.end;
}
public boolean startsWith(String prefix) {
TrieNode now = root;
for(int i = 0; i < prefix.length(); ++i){
int child = prefix.charAt(i) - 'a';
if(now.next[child] == null){
return false;
}
now = now.next[child];
}
return true;
}
}
/**
* Your Trie object will be instantiated and called as such:
* Trie obj = new Trie();
* obj.insert(word);
* boolean param_2 = obj.search(word);
* boolean param_3 = obj.startsWith(prefix);
*/