mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
Merge pull request #2777 from Jasonyou-boy/feature/nextString
feat(图论): 字符串接龙 Java优化版 并修复 beginStr==endStr 边界条件
This commit is contained in:
@ -277,7 +277,7 @@ ACM格式大家在输出结果的时候,要关注看看格式问题,特别
|
|||||||
|
|
||||||
有录友可能会想,ACM格式就是麻烦,有空格没有空格有什么影响,结果对了不就行了?
|
有录友可能会想,ACM格式就是麻烦,有空格没有空格有什么影响,结果对了不就行了?
|
||||||
|
|
||||||
ACM模式相对于核心代码模式(力扣) 更考验大家对代码的掌控能力。 例如工程代码里,输出输出都是要自己控制的。这也是为什么大公司笔试,都是ACM模式。
|
ACM模式相对于核心代码模式(力扣) 更考验大家对代码的掌控能力。 例如工程代码里,输入输出都是要自己控制的。这也是为什么大公司笔试,都是ACM模式。
|
||||||
|
|
||||||
以上代码中,结果都存在了 result数组里(二维数组,每一行是一个结果),最后将其打印出来。(重点看注释)
|
以上代码中,结果都存在了 result数组里(二维数组,每一行是一个结果),最后将其打印出来。(重点看注释)
|
||||||
|
|
||||||
|
@ -223,120 +223,126 @@ public:
|
|||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
### Java
|
### Java
|
||||||
DFS
|
|
||||||
```java
|
|
||||||
//这里的实现为主函数处理每个岛屿的第一块陆地 方式
|
|
||||||
//所以是主函数直接置count为1,剩余的交给dfs来做。
|
|
||||||
import java.util.*;
|
|
||||||
public class Main{
|
|
||||||
static int[][] dir = {{0,-1}, {1,0}, {0,1}, {-1, 0}};//四个方向
|
|
||||||
static int count = 0;
|
|
||||||
public static void dfs(boolean[][] visited, int x, int y, int[][] grid){
|
|
||||||
for(int i = 0; i < 4; i++){
|
|
||||||
int nextX = x + dir[i][0];
|
|
||||||
int nextY = y + dir[i][1];
|
|
||||||
if(nextX < 0 || nextY < 0 || nextY >= grid[0].length || nextX >= grid.length){
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if(!visited[nextX][nextY] && grid[nextX][nextY] == 1){
|
|
||||||
count++;
|
|
||||||
visited[nextX][nextY] = true;
|
|
||||||
dfs(visited, nextX, nextY, grid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public static void main(String[] args){
|
|
||||||
Scanner in = new Scanner(System.in);
|
|
||||||
int n = in.nextInt();
|
|
||||||
int m = in.nextInt();
|
|
||||||
int[][] grid = new int[n][m];
|
|
||||||
for(int i = 0; i < n; i++){
|
|
||||||
for(int j = 0; j < m; j++){
|
|
||||||
grid[i][j] = in.nextInt();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int result = 0;
|
```java
|
||||||
|
import java.util.*;
|
||||||
|
import java.math.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DFS版
|
||||||
|
*/
|
||||||
|
public class Main{
|
||||||
|
|
||||||
|
static final int[][] dir={{0,1},{1,0},{0,-1},{-1,0}};
|
||||||
|
static int result=0;
|
||||||
|
static int count=0;
|
||||||
|
|
||||||
|
public static void main(String[] args){
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
int n = scanner.nextInt();
|
||||||
|
int m = scanner.nextInt();
|
||||||
|
int[][] map = new int[n][m];
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
for (int j = 0; j < m; j++) {
|
||||||
|
map[i][j]=scanner.nextInt();
|
||||||
|
}
|
||||||
|
}
|
||||||
boolean[][] visited = new boolean[n][m];
|
boolean[][] visited = new boolean[n][m];
|
||||||
for(int i = 0; i < n; i++){
|
for (int i = 0; i < n; i++) {
|
||||||
for(int j = 0; j < m; j++){
|
for (int j = 0; j < m; j++) {
|
||||||
if(!visited[i][j] && grid[i][j] == 1){
|
if(!visited[i][j]&&map[i][j]==1){
|
||||||
visited[i][j] = true;
|
count=0;
|
||||||
count = 1;
|
dfs(map,visited,i,j);
|
||||||
dfs(visited, i, j, grid);
|
result= Math.max(count, result);
|
||||||
//dfs遍历完了一座岛屿,就比较count和result,保留最大的
|
|
||||||
result = Math.max(result, count);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println(result);
|
System.out.println(result);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
```
|
|
||||||
BFS
|
|
||||||
```java
|
|
||||||
import java.util.*;
|
|
||||||
public class Main{
|
|
||||||
static int[][] dir = {{0,-1}, {1,0}, {0,1}, {-1, 0}};//下右上左的顺序
|
|
||||||
static int count = 0;
|
|
||||||
public static void bfs(boolean[][] visited, int x, int y, int[][] grid){
|
|
||||||
Queue<pair> queue = new LinkedList<pair>();
|
|
||||||
queue.add(new pair(x,y));
|
|
||||||
count = 1; //该岛屿的第一块陆地被visit了
|
|
||||||
|
|
||||||
//对这个岛屿的所有都入队,除非上下左右都没有未访问的陆地
|
static void dfs(int[][] map,boolean[][] visited,int x,int y){
|
||||||
while(!queue.isEmpty()){
|
count++;
|
||||||
int curX = queue.peek().x;
|
visited[x][y]=true;
|
||||||
int curY = queue.poll().y;
|
for (int i = 0; i < 4; i++) {
|
||||||
//对每块陆地都进行上下左右的入队和计算(遍历),自然就是按广度优先了
|
int nextX=x+dir[i][0];
|
||||||
for(int i = 0; i < 4; i++){
|
int nextY=y+dir[i][1];
|
||||||
int nextX = curX + dir[i][0];
|
//水或者已经访问过的跳过
|
||||||
int nextY = curY + dir[i][1];
|
if(nextX<0||nextY<0
|
||||||
if(nextX < 0 || nextY < 0 || nextX >= grid.length || nextY >= grid[0].length){
|
||nextX>=map.length||nextY>=map[0].length
|
||||||
continue;
|
||visited[nextX][nextY]||map[nextX][nextY]==0)continue;
|
||||||
}
|
|
||||||
if(!visited[nextX][nextY] && grid[nextX][nextY] == 1){
|
dfs(map,visited,nextX,nextY);
|
||||||
count++;
|
|
||||||
queue.add(new pair(nextX, nextY));
|
|
||||||
visited[nextX][nextY] = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
```
|
||||||
|
|
||||||
static class pair{
|
```java
|
||||||
|
import java.util.*;
|
||||||
|
import java.math.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* BFS版
|
||||||
|
*/
|
||||||
|
public class Main {
|
||||||
|
static class Node {
|
||||||
int x;
|
int x;
|
||||||
int y;
|
int y;
|
||||||
pair(int x, int y){
|
|
||||||
|
public Node(int x, int y) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args){
|
static final int[][] dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
|
||||||
Scanner in = new Scanner(System.in);
|
static int result = 0;
|
||||||
int n = in.nextInt();
|
static int count = 0;
|
||||||
int m = in.nextInt();
|
|
||||||
int[][] grid = new int[n][m];
|
public static void main(String[] args) {
|
||||||
for(int i = 0; i < n; i++){
|
Scanner scanner = new Scanner(System.in);
|
||||||
for(int j = 0; j < m; j++){
|
int n = scanner.nextInt();
|
||||||
grid[i][j] = in.nextInt();
|
int m = scanner.nextInt();
|
||||||
|
int[][] map = new int[n][m];
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
for (int j = 0; j < m; j++) {
|
||||||
|
map[i][j] = scanner.nextInt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int result = 0;
|
|
||||||
boolean[][] visited = new boolean[n][m];
|
boolean[][] visited = new boolean[n][m];
|
||||||
for(int i = 0; i < n; i++){
|
for (int i = 0; i < n; i++) {
|
||||||
for(int j = 0; j < m; j++){
|
for (int j = 0; j < m; j++) {
|
||||||
if(!visited[i][j] && grid[i][j] == 1){
|
if (!visited[i][j] && map[i][j] == 1) {
|
||||||
visited[i][j] = true;
|
count = 0;
|
||||||
bfs(visited, i, j, grid);
|
bfs(map, visited, i, j);
|
||||||
result = Math.max(result, count);
|
result = Math.max(count, result);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println(result);
|
System.out.println(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void bfs(int[][] map, boolean[][] visited, int x, int y) {
|
||||||
|
Queue<Node> q = new LinkedList<>();
|
||||||
|
q.add(new Node(x, y));
|
||||||
|
visited[x][y] = true;
|
||||||
|
count++;
|
||||||
|
while (!q.isEmpty()) {
|
||||||
|
Node node = q.remove();
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
int nextX = node.x + dir[i][0];
|
||||||
|
int nextY = node.y + dir[i][1];
|
||||||
|
if (nextX < 0 || nextY < 0 || nextX >= map.length || nextY >= map[0].length || visited[nextX][nextY] || map[nextX][nextY] == 0)
|
||||||
|
continue;
|
||||||
|
q.add(new Node(nextX, nextY));
|
||||||
|
visited[nextX][nextY] = true;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
### Python
|
### Python
|
||||||
|
|
||||||
|
@ -153,64 +153,68 @@ int main() {
|
|||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
### Java
|
### Java
|
||||||
|
|
||||||
```Java
|
```Java
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
// BFS方法
|
public static void main(String[] args) {
|
||||||
public static int ladderLength(String beginWord, String endWord, List<String> wordList) {
|
Scanner scanner = new Scanner(System.in);
|
||||||
// 使用set作为查询容器,效率更高
|
int n = scanner.nextInt();
|
||||||
HashSet<String> set = new HashSet<>(wordList);
|
scanner.nextLine();
|
||||||
|
String beginStr = scanner.next();
|
||||||
// 声明一个queue存储每次变更一个字符得到的且存在于容器中的新字符串
|
String endStr = scanner.next();
|
||||||
Queue<String> queue = new LinkedList<>();
|
scanner.nextLine();
|
||||||
|
List<String> wordList = new ArrayList<>();
|
||||||
// 声明一个hashMap存储遍历到的字符串以及所走过的路径path
|
wordList.add(beginStr);
|
||||||
HashMap<String, Integer> visitMap = new HashMap<>();
|
wordList.add(endStr);
|
||||||
queue.offer(beginWord);
|
for (int i = 0; i < n; i++) {
|
||||||
visitMap.put(beginWord, 1);
|
wordList.add(scanner.nextLine());
|
||||||
|
|
||||||
while (!queue.isEmpty()) {
|
|
||||||
String curWord = queue.poll();
|
|
||||||
int path = visitMap.get(curWord);
|
|
||||||
|
|
||||||
for (int i = 0; i < curWord.length(); i++) {
|
|
||||||
char[] ch = curWord.toCharArray();
|
|
||||||
// 每个位置尝试26个字母
|
|
||||||
for (char k = 'a'; k <= 'z'; k++) {
|
|
||||||
ch[i] = k;
|
|
||||||
|
|
||||||
String newWord = new String(ch);
|
|
||||||
if (newWord.equals(endWord)) return path + 1;
|
|
||||||
|
|
||||||
// 如果这个新字符串存在于容器且之前未被访问到
|
|
||||||
if (set.contains(newWord) && !visitMap.containsKey(newWord)) {
|
|
||||||
visitMap.put(newWord, path + 1);
|
|
||||||
queue.offer(newWord);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
int count = bfs(beginStr, endStr, wordList);
|
||||||
return 0;
|
System.out.println(count);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main (String[] args) {
|
/**
|
||||||
/* code */
|
* 广度优先搜索-寻找最短路径
|
||||||
// 接收输入
|
*/
|
||||||
Scanner sc = new Scanner(System.in);
|
public static int bfs(String beginStr, String endStr, List<String> wordList) {
|
||||||
int N = sc.nextInt();
|
int len = 1;
|
||||||
sc.nextLine();
|
Set<String> set = new HashSet<>(wordList);
|
||||||
String[] strs = sc.nextLine().split(" ");
|
Set<String> visited = new HashSet<>();
|
||||||
|
Queue<String> q = new LinkedList<>();
|
||||||
List<String> wordList = new ArrayList<>();
|
visited.add(beginStr);
|
||||||
for (int i = 0; i < N; i++) {
|
q.add(beginStr);
|
||||||
wordList.add(sc.nextLine());
|
q.add(null);
|
||||||
|
while (!q.isEmpty()) {
|
||||||
|
String node = q.remove();
|
||||||
|
//上一层结束,若下一层还有节点进入下一层
|
||||||
|
if (node == null) {
|
||||||
|
if (!q.isEmpty()) {
|
||||||
|
len++;
|
||||||
|
q.add(null);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
char[] charArray = node.toCharArray();
|
||||||
|
//寻找邻接节点
|
||||||
|
for (int i = 0; i < charArray.length; i++) {
|
||||||
|
//记录旧值,用于回滚修改
|
||||||
|
char old = charArray[i];
|
||||||
|
for (char j = 'a'; j <= 'z'; j++) {
|
||||||
|
charArray[i] = j;
|
||||||
|
String newWord = new String(charArray);
|
||||||
|
if (set.contains(newWord) && !visited.contains(newWord)) {
|
||||||
|
q.add(newWord);
|
||||||
|
visited.add(newWord);
|
||||||
|
//找到结尾
|
||||||
|
if (newWord.equals(endStr)) return len + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
charArray[i] = old;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
// wordList.add(strs[1]);
|
|
||||||
|
|
||||||
// 打印结果
|
|
||||||
int result = ladderLength(strs[0], strs[1], wordList);
|
|
||||||
System.out.println(result);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user