m*l
2 楼
了解这题DFS的话代码简洁而且大测试不超时。
就是想拿这题练习一下BFS的解法,自己吭哧吭哧写的代码超时了,不知道代码中的哪
一步太耗时?大家帮忙看一下,谢谢~
或者其他可以改进的地方大家也不妨指出~
代码如下:
public class Solution {
public static Queue queue = new LinkedList(); public boolean exist(char[][] board, String word) { if (word.equals("") || word == null) return true; if (board == null || board.length == 0) return false; int row = board.length; int col = board[0].length; String tmp = word; // save complete word for repeated use boolean[][] visited; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { word = tmp; visited = new boolean[row][col]; // refresh visited at beginning of every new iteration if (board[i][j] == word.charAt(0)) { word = word.substring(1); if (word.length() == 0) return true; Cell cell = new Cell(i, j); queue.offer(cell); visited[i][j] = true; if (bfs(board, word, visited)) { return true; } } } } return false; } public boolean bfs(char[][] board, String word, boolean[][] visited) { int[][] dir = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } }; while (!queue.isEmpty()) { Cell tmp = queue.poll(); int x = tmp.row; int y = tmp.col; for (int k = 0; k < 4; k++) { int i = x + dir[k][0]; int j = y + dir[k][1]; if (i >= 0 && j >= 0 && i < board.length && j < board[0]. length && board[i][j] == word.charAt(0) && !visited[i][j]) { word = word.substring(1); if (word.length() == 0) return true; Cell cell = new Cell(i, j); queue.offer(cell); visited[i][j] = true; } } } return false; }
static class Cell { int row; int col; public Cell (int row, int col) { this.row = row; this.col = col; } }
} | |
就是想拿这题练习一下BFS的解法,自己吭哧吭哧写的代码超时了,不知道代码中的哪
一步太耗时?大家帮忙看一下,谢谢~
或者其他可以改进的地方大家也不妨指出~
代码如下:
public class Solution {
public static Queue
public boolean exist(char[][] board, String word) {
if (word.equals("") || word == null)
return true;
if (board == null || board.length == 0)
return false;
int row = board.length;
int col = board[0].length;
String tmp = word; // save complete word for repeated use
boolean[][] visited;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
word = tmp;
visited = new boolean[row][col]; // refresh visited at
beginning of every new iteration
if (board[i][j] == word.charAt(0)) {
word = word.substring(1);
if (word.length() == 0) return true;
Cell cell = new Cell(i, j);
queue.offer(cell);
visited[i][j] = true;
if (bfs(board, word, visited)) {
return true;
}
}
}
}
return false;
}
public boolean bfs(char[][] board, String word, boolean[][] visited) {
int[][] dir = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
while (!queue.isEmpty()) {
Cell tmp = queue.poll();
int x = tmp.row;
int y = tmp.col;
for (int k = 0; k < 4; k++) {
int i = x + dir[k][0];
int j = y + dir[k][1];
if (i >= 0 && j >= 0 && i < board.length && j < board[0].
length && board[i][j] == word.charAt(0) && !visited[i][j]) {
word = word.substring(1);
if (word.length() == 0) return true;
Cell cell = new Cell(i, j);
queue.offer(cell);
visited[i][j] = true;
}
}
}
return false;
}
static class Cell {
int row;
int col;
public Cell (int row, int col) {
this.row = row;
this.col = col;
}
}
}
b*a
3 楼
这家伙对镜头太敏感了。每次他明明玩的欢,一看到我在照相/录像,立刻就停了。。。
e*e
4 楼
http://wiki.answers.com/Q/How_is_synthetic_leather_made
人造皮革
【在 t********f 的大作中提到】
: 万能的EBIZ啊,有么有朋友知道什么是Blended Leather?和普通的Leather有什么不同
: ,谢谢。
人造皮革
【在 t********f 的大作中提到】
: 万能的EBIZ啊,有么有朋友知道什么是Blended Leather?和普通的Leather有什么不同
: ,谢谢。
c*2
5 楼
怎么感觉这个BFS是错的呢...
b*o
6 楼
So cute!
a*g
7 楼
top-grain leather 是头层皮也是真皮。透气性好。
bonded leather是碎皮压成的,便宜,真皮替代品
bycast是皮上面图了一层材料,所以看起来要光滑一些。
bonded leather是碎皮压成的,便宜,真皮替代品
bycast是皮上面图了一层材料,所以看起来要光滑一些。
t*f
9 楼
谢谢LS两位。不知道这种leather耐不耐用?
t*d
11 楼
optimal bst ?那个词汇频率来建造bst的那个题目么
相关阅读
puppy太好动怎么办?乌龙了多图:DUKE港湾游 (下)英雄拖狗今儿见了一nb的dog-walker奔哥俩好有没有petfooddirect的coupon or FS code?谢谢北京警察博物馆把退役警犬活体解剖,制成警犬标本(ZZ)这个很好玩[奔]猪小黛不为人知的一面小狗吞食异物需要手术妖姬终于遇到了邻居的棉裤哇哈哈,今天碰到艾迪的playmate了狗脚垫儿裂了...Pluto懂事了...给我的一双宝贝猫猫找一个充满爱的新家[夏日活动]桑心的Callie有San Antonio的husky么~~想交流下教育方针是不是周五去kc比较好?后知后觉,Mello原来是豹纹猫!有图有真相哦谁把我发的“方舟子谈弓形虫”删掉了,请给个合理理由