avatar
sonny玩妈妈的头绳# pets - 心有所宠
t*f
1
万能的EBIZ啊,有么有朋友知道什么是Blended Leather?和普通的Leather有什么不同
,谢谢。
avatar
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;
}
}

}
avatar
b*a
3
这家伙对镜头太敏感了。每次他明明玩的欢,一看到我在照相/录像,立刻就停了。。。
avatar
c*2
5
怎么感觉这个BFS是错的呢...
avatar
b*o
6
So cute!
avatar
a*g
7
top-grain leather 是头层皮也是真皮。透气性好。
bonded leather是碎皮压成的,便宜,真皮替代品
bycast是皮上面图了一层材料,所以看起来要光滑一些。
avatar
m*l
8
代码在OJ上跑过,就是在大测试的时候显示Time Limit Exceeded
我觉得BFS 和 DFS 的time complexity应该是一样,不应该一个通过另一个没通过吧。
觉得代码里哪里应该有点小问题。

【在 c*******2 的大作中提到】
: 怎么感觉这个BFS是错的呢...
avatar
t*f
9
谢谢LS两位。不知道这种leather耐不耐用?
avatar
m*l
10
我看了一下,原来的代码是有错。我加了一行break;应该可以了。
但是还是无法通过大测试,不知道到底是哪里太慢了。我的理解是BFS和DFS 在时间复
杂度上应该是差不多的呀

【在 c*******2 的大作中提到】
: 怎么感觉这个BFS是错的呢...
avatar
t*d
11
optimal bst ?那个词汇频率来建造bst的那个题目么
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。