w***o 发帖数: 109 | 1 拿这个练练DP:
public int longestValidParentheses(String s) {
int n = s.length();
int[] dp = new int[n+1];
int max = 0;
for(int i = n-2; i >= 0; i--) {
if(s.charAt(i) == '(') {
int j = i+1+dp[i+1];
if(j < n && s.charAt(j) == ')') {
dp[i] = 2+dp[i+1]+dp[j+1];
max = Math.max(max, dp[i]);
}
}
}
return max;
} |
|
p*****2 发帖数: 21240 | 2 我来写一个标准的吧。也练练手。
public class test
{
public static void main(String[] args)
{
new test().run();
}
PrintWriter out = null;
int score(String str, int start, int len)
{
if (len == 2)
{
if (str.charAt(start) == str.charAt(start + 1))
return 2;
}
else if (len == 3)
{
char[] a = str.substring(start, start + 3).toCharArray();
if (a[0] == a[1] && a[1] == a[2])
return... 阅读全帖 |
|
p*****2 发帖数: 21240 | 3
不好意思。刚才写的太滥了。没有优化。这次这个应该才是标准的。
import java.io.*;
import java.util.*;
public class test
{
public static void main(String[] args)
{
new test().run();
}
PrintWriter out = null;
int score(String str, int start, int len)
{
if (len == 2)
{
if (str.charAt(start) == str.charAt(start + 1))
return 2;
}
else if (len == 3)
{
char[] a = str.substring(start, start + 3).toCharArray();
if... 阅读全帖 |
|
p*****2 发帖数: 21240 | 4 白芷,我写了一个,你看看有没有问题。空间可以优化到2*m, 后边找最短的也可以一
并做了。不过这个主要是实现这个思路,没有优化。
String min(String str, String word)
{
int n = str.length();
int m = word.length();
int[][] dp = new int[n + 1][m + 1];
for (int i = 0; i <= n; i++)
dp[i][m] = 0;
for (int j = 0; j < m; j++)
dp[n][j] = n + 1;
for (int i = n - 1; i >= 0; i--)
for (int j = m - 1; j >= 0; j--)
{
if (str.charAt(i) == word.charAt(j))
{
dp[i][j] = 1 + dp[i + 1][j + 1];
}
else
{
dp[i][j] = 1 + dp[i + 1][j];
}
}
String ans = "none";
int min = n + 1;
for (in... 阅读全帖 |
|
Z*****Z 发帖数: 723 | 5 二爷,看不太懂 :(
写了两个testcase您看看?
public class SearchSequence {
static String min(String str, String word) {
int n = str.length();
int m = word.length();
int[][] dp = new int[n + 1][m + 1];
for (int i = 0; i <= n; i++)
dp[i][m] = 0;
for (int j = 0; j < m; j++)
dp[n][j] = n + 1;
for (int i = n - 1; i >= 0; i--)
for (int j = m - 1... 阅读全帖 |
|
T**********r 发帖数: 52 | 6 public boolean exist(char[][] board, String word) {
if(word == null || word.length() == 0){
return true;
}
char c = word.charAt(0);
for(int i = 0; i < board.length; i ++){
for(int j = 0; j < board[i].length; j ++){
if(board[i][j] ==c){
board[i][j] = 0;
if(dfs(board, i, j, word, 1)){
return true;
}
board[i][j] = c;
}
... 阅读全帖 |
|
p****n 发帖数: 294 | 7 来自主题: JobHunting版 - G电面一题 随便写了一下, 用递归
String maps = "abcdefghijklmnopqrstuvwxyz";
public ConvertNumberToString(int num) {
String numStr = String.valueOf(num);
if (false == getCombination(numStr, "")) {
System.out.println("NO");
}
}
private boolean getCombination(String remain, String result) {
if (remain.length() == 0) {
System.out.println(result);
return true;
}
int n = Integer.parseInt(remain.substring(0, 1)) - 1;
if (n < 0){
return false;
}
char... 阅读全帖 |
|
h**********9 发帖数: 3252 | 8 Don't understand question #1.
Question #2 seems too simple. Any trap???
String input = "house";
String order = "soup";
LinkedHashMap map = new LinkedHashMap
>();
for(int i = 0; i < order.length(); i++)
{
map.put(order.charAt(i), 0);
}
for(int i = 0; i < input.length(); i++)
{
char c = input.charAt(i);
if(map.contains(c))
map.put(c, map.get(c)++);
else
map.put(c, 1);
}
for(Map.Entry entry : map)
{
for(int i = 0; i < entr... 阅读全帖 |
|
w***o 发帖数: 109 | 9 有点罗嗦,这样应该简洁一点:
public static int RomanToInt(String input) {
// null check ...
int pre = map.get(input.charAt(0));
int ret = pre;
for(int i = 1; i < input.length; i++ {
int cur = map.get(input.charAt(i));
ret += cur;
if(cur > pre)
{ret -= pre * 2;pre = 1001;}
else
pre = cur;
}
return ret;
} |
|
b*******y 发帖数: 2048 | 10 public int Compare(String s1, String s2)
{
String[] a1 = s1.Split('.');
String[] a2 = s2.Split('.');
int i=0;
while(i
{
ss1=a1[i];
ss2 = a2[i];
int num1 =integer.parseInt(ss1);
int num2 = integer.parseInt(ss2);
if(num1==0 || num2 ==0)
return num1-num2;
while(ss1.charAt(0)=='0')
{
ss1 = ss1.subString(1);
num2 *=10;
}
while(ss2.charAt(0)=='0')
{
ss2 = ss2.sub... 阅读全帖 |
|
z******e 发帖数: 82 | 11 多谢大牛的test case
private static String uncomment(String str) {
boolean slash2 = false;
boolean inStr = false;
boolean slashstar = false;
StringBuilder sb = new StringBuilder();
char lastc = ' ';
char c = ' ';
int deleteStart = -1;
for (int i = 0; i < str.length(); i++) {
lastc = c;
c = str.charAt(i);
sb.append(c);
// ""
if (c == '"' && !slash2 && !slashstar) {
... 阅读全帖 |
|
z******e 发帖数: 82 | 12 -_-!!, what bad test skill I have.
for this test case, change
if (c == '"' && !slash2 && !slashstar) {
to
if (c == '"' && lastc != '\' && !slash2 && !slashstar) {
complete code:
private static String uncomment(String str) {
boolean slash2 = false;
boolean inStr = false;
boolean slashstar = false;
StringBuilder sb = new StringBuilder();
char lastc = ' ';
char c = ' ';
int deleteStart = -1;
for (int i = 0; i < str.length(); i++) {
... 阅读全帖 |
|
z******e 发帖数: 82 | 13 new test cases:
--------------------
char str[] = "\\"; // test \ inside "
char str[] = "\\";
char str[] = '\"'; // test \ inside "
char str[] = '\"';
code:
---------------------------
private static String uncomment(String str) {
boolean slash2 = false;
boolean inStr = false;
boolean slashstar = false;
boolean escape = false;
StringBuilder sb = new StringBuilder();
char lastc = ' ';
char c = ' ';
int deleteStart = -1;
... 阅读全帖 |
|
p********2 发帖数: 123 | 14 OJ过了,字符串太长也可以用bigint
public class Solution {
public int numDistinct(String S, String T) {
// Start typing your Java solution below
// DO NOT write main() function
if(S.length()==0) return 0;
if(T.length()==0) return 1;
int[][] DP=new int[T.length()+1][S.length()+1];
for(int i=0;i<=T.length();i++){
DP[i][0]=0;
}
for(int i=0;i<=S.length();i++){
DP[0][i]=1;
}
for(int i=1;i<=T.length()... 阅读全帖 |
|
f*********m 发帖数: 726 | 15 这步怎么理解?
if(S.charAt(j-1)==T.charAt(i-1)){
DP[i][j]=DP[i][j-1]+DP[i-1][j-1];
}
else
DP[i][j]=DP[i][j-1];
谢谢 |
|
p********2 发帖数: 123 | 16 OJ过了,字符串太长也可以用bigint
public class Solution {
public int numDistinct(String S, String T) {
// Start typing your Java solution below
// DO NOT write main() function
if(S.length()==0) return 0;
if(T.length()==0) return 1;
int[][] DP=new int[T.length()+1][S.length()+1];
for(int i=0;i<=T.length();i++){
DP[i][0]=0;
}
for(int i=0;i<=S.length();i++){
DP[0][i]=1;
}
for(int i=1;i<=T.length()... 阅读全帖 |
|
f*********m 发帖数: 726 | 17 这步怎么理解?
if(S.charAt(j-1)==T.charAt(i-1)){
DP[i][j]=DP[i][j-1]+DP[i-1][j-1];
}
else
DP[i][j]=DP[i][j-1];
谢谢 |
|
e****e 发帖数: 418 | 18 I'm with you. I don't think prefix tree(trie) is needed. Here is my code.
String[] pad = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs
", "tuv", "wxyz"};
List words = new ArrayList();
Set dict = // Preloaded.
public void determineWord(String word, String remaining) {
if ( "".equals( remaining ) ) {
if(dict.contains( word )
words.add( word );
return;
}
int digit = remaining.... 阅读全帖 |
|
b*****6 发帖数: 179 | 19 2 public String addBinary(String a, String b) {
3 int i = a.length() - 1;
4 int j = b.length() - 1;
5 int carry = 0;
6
7 ArrayList result = new ArrayList();
8
9 while (i >= 0 || j >= 0 || carry == 1)
10 {
11 int cur = carry;
12
13 if (i >= 0)
14 cur += a.charAt(i--) - '0';
15
16 if (j >= 0)
17 cur += ... 阅读全帖 |
|
p******9 发帖数: 47 | 20 public String findMostBeautifulString(String s){
int M = 256;
int[] count = new int[M];
boolean[] used = new boolean[M];
int num = 0;
for(int i = 0 ; i < s.length() ; i ++){
int index = s.charAt(i);
if(count[index] == 0)
num ++;
count[index] ++;
used[index] = false;
}
char[] output = new char[num];
int k = 0;
for(int i = 0 ; i < s.length(); i ++){
ch... 阅读全帖 |
|
p******9 发帖数: 47 | 21 public String findMostBeautifulString(String s){
int M = 256;
int[] count = new int[M];
boolean[] used = new boolean[M];
int num = 0;
for(int i = 0 ; i < s.length() ; i ++){
int index = s.charAt(i);
if(count[index] == 0)
num ++;
count[index] ++;
used[index] = false;
}
char[] output = new char[num];
int k = 0;
for(int i = 0 ; i < s.length(); i ++){
ch... 阅读全帖 |
|
h*********o 发帖数: 230 | 22 代码如下:
public boolean exist(char[][] board, String word) {
// Start typing your Java solution below
// DO NOT write main() function
for(int i=0;i
for(int j=0;j
if(board[i][j]==word.charAt(0)){
return find(board, word, i,j,0);
}
}
}
return false;
}
public boolean... 阅读全帖 |
|
e****e 发帖数: 418 | 23 IMHO, the limitation of this question is b shouldn't have dups.
public String sort(String input, String order) {
Map map = new LinkedHashMap(
);
for ( int i = 0; i < order.length(); i++ )
map.put( order.charAt( i ), 0 );
for ( int i = 0; i < input.length(); i++ ) {
char c = input.charAt( i );
if ( map.containsKey( c ) )
map.put( c , map.get(c) + 1 );
... 阅读全帖 |
|
c*****a 发帖数: 808 | 24 import java.util.Hashtable;
public class Solution {
Hashtable Dict = new Hashtable ()
;
public ArrayList letterCombinations(String digits) {
ArrayList res = new ArrayList();
if(digits.length()==0){
res.add("");
return res;
}
Dict.put(2, new String[]{"a", "b", "c"});
Dict.put(3, new String[]{"d", "e", "f"});
Dict.put(4, new String[]{"g", "h", "i"});
... 阅读全帖 |
|
w***o 发帖数: 109 | 25 DP version:
String s = "aabbabbaab";
int n = s.length();
int min = n, last = n;
int[] dp = new int[n+1];
for(int i = 1; i <= n; i++) {
int max = 1;
for(int j = n; j >= 1; j--) {
if (s.charAt(i-1) == s.charAt(j-1))
dp[j] = dp[j-1] + 1;
else
dp[j] = 0;
if(i != j && dp[j] >= max)
max = dp[j]+1;
}
if(max <=... 阅读全帖 |
|
l**b 发帖数: 457 | 26 然后想了一下,DP的空间好像可以优化到O(|Si|):
private boolean helper3(String s, String t) {
boolean[] dp = new boolean[s.length() + 1];
for (int i = 0; i <= s.length(); i++) {
dp[i] = true;
}
for (int i = 1; i <= t.length(); i++) {
boolean[] newDp = new boolean[s.length() + 1];
for (int j = 1; j <= s.length(); j++) {
if (t.charAt(i - 1) == s.charAt(j - 1)) {
newDp[j] = dp[j - 1];
} else {
... 阅读全帖 |
|
l**b 发帖数: 457 | 27 然后想了一下,DP的空间好像可以优化到O(|Si|):
private boolean helper3(String s, String t) {
boolean[] dp = new boolean[s.length() + 1];
for (int i = 0; i <= s.length(); i++) {
dp[i] = true;
}
for (int i = 1; i <= t.length(); i++) {
boolean[] newDp = new boolean[s.length() + 1];
for (int j = 1; j <= s.length(); j++) {
if (t.charAt(i - 1) == s.charAt(j - 1)) {
newDp[j] = dp[j - 1];
} else {
... 阅读全帖 |
|
b*******h 发帖数: 53 | 28 求各位大牛指导, 这个题目我扫描整个string。用boolean表示当前的状态, 每个
char可以看之前的状态进行判断,逻辑上是不是比较清楚一点。贴上代码:
public boolean isNumber(String s) {
s = s.trim();
if(s.length() == 0) return false;
if(s.charAt(0) == '+' || s.charAt(0)=='-') return isNum(s.substring(
1,s.length()));
else return isNum(s);
}
public boolean isNum(String s){
boolean hasInteger = false, hasDot = false, hasDecimal = false,
hasPow = false, hasSign = false, hasExponent = false;
for (ch... 阅读全帖 |
|
b*******h 发帖数: 53 | 29 求各位大牛指导, 这个题目我扫描整个string。用boolean表示当前的状态, 每个
char可以看之前的状态进行判断,逻辑上是不是比较清楚一点。贴上代码:
public boolean isNumber(String s) {
s = s.trim();
if(s.length() == 0) return false;
if(s.charAt(0) == '+' || s.charAt(0)=='-') return isNum(s.substring(
1,s.length()));
else return isNum(s);
}
public boolean isNum(String s){
boolean hasInteger = false, hasDot = false, hasDecimal = false,
hasPow = false, hasSign = false, hasExponent = false;
for (ch... 阅读全帖 |
|
c***w 发帖数: 134 | 30 请问一道题,我的程序就是不能全过large test case。
代码如下:
public class Solution {
public boolean isPalindrome(String s) {
// Start typing your Java solution below
// DO NOT write main() function
if (s == null) {
return false;
}
if (s.length() == 0) {
return true;
}
s = s.toLowerCase();
int start = 0;
int end = s.length() - 1;
while (start <= end) {
char first = s.c... 阅读全帖 |
|
c*u 发帖数: 22 | 31 一、用 HashSet 和 LinkedHashSet
String str = "cbacbbbfa";
HashSet set = new HashSet();
LinkedHashSet linkSet = new LinkedHashSet();
int len = str.length();
for (int i=0;i
char ch = str.charAt( i );
if (set.contains( ch ))
linkSet.remove( ch );
else{
set.add( ch );
linkSet.add( ch );
}
}
System.out.println( linkSet.iterator().next() );
二、用 HashSet 和 LinkedList
String str = "cbacbbbfa";
HashSet set = n... 阅读全帖 |
|
b*****6 发帖数: 179 | 32 前些天我也看到这个题,顺手写了一下:思路是找到s.length()的所有prime factor,
然后看根据这个factor移位是不是满足题的条件,如果是,结果加length/factor - 1
public static int cyclic(String s)
{
int len = s.length();
if (len <= 1)
return 1;
int result = 1;
ArrayList factors = get_factors(len);
for (int fctr : factors)
{
if (valid_cyclic(s, fctr))
result += len / fctr - 1;
}
return result;
}
sta... 阅读全帖 |
|
c********t 发帖数: 5706 | 33 嗯,就是你这个算法,O(n*sqrt(n)),改了改ben5736的codes 不知过不过的了大case;
public static int cyclic(String s) {
int len = s.length();
if (len <= 1)
return 1;
ArrayList factors = get_factors(len);
for (int i = factors.size() - 1; i >= 0; i--) {
int fctr = factors.get(i);
if (valid_cyclic(s, fctr))
return fctr;
}
return len;
}
static boolean valid_cyclic(String s, int v) {
for (int i = 0, j = v; i < s.length(); i++, j++) {
if (j == s.... 阅读全帖 |
|
b*****6 发帖数: 179 | 34 前些天我也看到这个题,顺手写了一下:思路是找到s.length()的所有prime factor,
然后看根据这个factor移位是不是满足题的条件,如果是,结果加length/factor - 1
public static int cyclic(String s)
{
int len = s.length();
if (len <= 1)
return 1;
int result = 1;
ArrayList factors = get_factors(len);
for (int fctr : factors)
{
if (valid_cyclic(s, fctr))
result += len / fctr - 1;
}
return result;
}
sta... 阅读全帖 |
|
c********t 发帖数: 5706 | 35 嗯,就是你这个算法,O(n*sqrt(n)),改了改ben5736的codes 不知过不过的了大case;
public static int cyclic(String s) {
int len = s.length();
if (len <= 1)
return 1;
ArrayList factors = get_factors(len);
for (int i = factors.size() - 1; i >= 0; i--) {
int fctr = factors.get(i);
if (valid_cyclic(s, fctr))
return fctr;
}
return len;
}
static boolean valid_cyclic(String s, int v) {
for (int i = 0, j = v; i < s.length(); i++, j++) {
if (j == s.... 阅读全帖 |
|
f*******t 发帖数: 7549 | 36 看不懂前面第二题的解法,自己写了个O(n^3)的
public class MinPalindromeSplits {
private int[][] dp;
private boolean isPalindrome(String s) {
if (s.length() < 2)
return true;
for (int i = 0; i <= s.length() / 2; i++) {
if (s.charAt(i) != s.charAt(s.length() - i - 1))
return false;
}
return true;
}
public int minSplits(String s) {
dp = new int[s.length() + 1][s.length() + 1];
for (int len = 1; l... 阅读全帖 |
|
j**7 发帖数: 143 | 37 第三题的答案:
public class InfixToPostfix {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String infix="4*((5+6)*7";
infix="3+(1*2)";
String postfix=toPostfix(infix);
System.out.println(postfix);
System.out.println(evaluate(postfix));
}
//evaluate a postfix equation
static int evaluate(String postfix)
{
Stack st=new Stack阅读全帖 |
|
c********t 发帖数: 5706 | 38 好吧
int wordCount(String s) {
int count = 0, n = s.length();
for (int i = 0; i < n; i++) {
if (s.charAt(i) != ' ') {
count++;
while (i < n && s.charAt(i++) != ' ');
i--;
}
}
return count;
} |
|
r*****a 发帖数: 421 | 39 写了一个向两边扫的。
public int minCut(String s) {
int len = s.length();
int[] dp = new int[len];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[0] = 0;
for (int i = 1; i< len; ++i) {
for (int j = i-1; j <= i; j++) {
int start = i-1;
int end = j;
while (start >= 0 && end <= len-1 && s.charAt(start) == s.charAt(end))
{
if (start == 0) dp[end] = 0;
else
dp[end] = Math.min(dp[end], dp[start-1] + 1);
start--;
end++;
}
}
if (d... 阅读全帖 |
|
H*****l 发帖数: 1257 | 40 原题是Leetcode上的Palindrome Partitioning
如下:
Given a string s, partition s such that every substring of the partition is
a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab",
Return
[
["aa","b"],
["a","a","b"]
]
--------------------------------------------------------
请问,dfs函数里,这一小段是什么用处啊?特别是最后的remove..
for(int i=start+1;i<=s.length();i++){
if(isPalin(s,start,i-1)){
al.add(s.substring(start,i));
d... 阅读全帖 |
|
x****8 发帖数: 127 | 41 stack?
public static void main(String[] args) {
XmlParser p = new XmlParser("ABDC");
String s;
Stack> stk = new Stack>();
TreeNode root = null;// = new TreeNode();
while((s = p.getNextTag()) != null){
if(p.isStartTag()){
String str = p.getNextTag();//assert is string tag
TreeNode node = new TreeNode(str);
... 阅读全帖 |
|
x****8 发帖数: 127 | 42 a small bug below: temp = new ArrayList(output);
public static List BinaryOutput(String input) {
if (input == null || input.isEmpty())
return null;
List output = new ArrayList();
List temp = new ArrayList();
temp.add("");
for (int i = 0; i < input.length(); i++) {
output.clear();
for (String s : temp) {
if (input.charAt(i) == ... 阅读全帖 |
|
L*******e 发帖数: 114 | 43 试着用Java写了一个。还是不够简练。Have to use String array, otherwise cannot
push the result back to the original array.
public static int evaluate(String[] array, int from, int to){
// get first operator, backward
int j = to;
String s = array[j];
while (j >= from && !Operator.isOperator(s.charAt(0))){
s = array[--j];
}
// get operands
Operator op = Operator.getOperator(s.charAt(0));
int opIndex = j;
int a = Integer.valueOf(array[++j]);
int b = Integer.valueOf(ar... 阅读全帖 |
|
l*n 发帖数: 529 | 44 你这个int [] A = {2, 1, 0, 2, 2, 0}; 用得匪夷所思啊。
给你个正确的。
public class Solution {
public String countAndSay(int n) {
// Start typing your Java solution below
// DO NOT write main() function
if (n<=0) return "";
if (n==1) return "1";
String s = countAndSay(n-1);
StringBuilder sb = new StringBuilder();
int i=0;
int start=0;
while (start
char prev = s.charAt(start);
while (i阅读全帖 |
|
b**m 发帖数: 1466 | 45 public class Solution {
public String countAndSay(int n) {
if(n==1){
return "1";
}
StringBuilder r = new StringBuilder();
String work = countAndSay(n-1);
int count=1;
int c=work.charAt(0)-'0';
for(int i=1;i
int num = work.charAt(i) -'0';
if(num ==c){
count ++;
}else{
r.append(count);
... 阅读全帖 |
|
y*****3 发帖数: 451 | 46 最近看leetcode和很多牛人的blog,发现都很追求code写的短,写的紧凑,比如下边这
条:
int p = (i < m) ? a.charAt(m - 1 - i) - '0' : 0;
可是,有实际经验的码工们都知道,code的readibility非常重要,没有必要用太多的
syntax sugar来压缩代码。比如上边这条如果改写成下边这样,可读性大大提高,但是
performance其实是一样的。
int p = 0; //always initiate a member field
if (i < m)
{
p = a.charAt(m - 1 - i) - '0';
}
else
{
p = 0;
}
我比较迷惑,在面试的时候,那些面试官们难道不懂在实际工作中code的readibility
很重要吗?为什么大家都那么追求code写的短和紧凑?面试的时候到底该怎么写?请大
牛们指点迷津,谢谢! |
|
j*******t 发帖数: 223 | 47 sedgewick的书里有讲过很简单的trie。
public class TrieST {
private static final int R = 256; // extended ASCII
private Node root = new Node();
private static class Node {
private Object val;
private Node[] next = new Node[R];
}
public boolean contains(String key) {
return get(key) != null;
}
public Value get(String key) {
Node x = get(root, key, 0);
if (x == null) {
return null;
}
return (Value) x.val;
}
... 阅读全帖 |
|
j*******t 发帖数: 223 | 48 sedgewick的书里有讲过很简单的trie。
public class TrieST {
private static final int R = 256; // extended ASCII
private Node root = new Node();
private static class Node {
private Object val;
private Node[] next = new Node[R];
}
public boolean contains(String key) {
return get(key) != null;
}
public Value get(String key) {
Node x = get(root, key, 0);
if (x == null) {
return null;
}
return (Value) x.val;
}
... 阅读全帖 |
|
l*n 发帖数: 529 | 49 有递推关系的不是直接的min值,而是i左右的b字符和a字符个数,所以好像没有办法针
对min值做dp。还是直接scan比较直观。
int minChange(String s) {
int[] countABackword = new int[s.length() + 1];
countABackword[s.length()] = 0;
for (int i = s.length() - 1; i >= 0; i--) {
char c = s.charAt(i);
countABackword[i] = c == 'a' ? countABackword[i + 1] + 1
: countABackword[i + 1];
}
int countBForward = 0;
int min = countABackword[0]; // case: all b
for (int i = 0; i < s.length(); i++) {
char c = s.char... 阅读全帖 |
|
m********l 发帖数: 791 | 50 了解这题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 c... 阅读全帖 | | |
|