Redian新闻
>
向各位朋友请教一道题目
avatar
向各位朋友请教一道题目# Java - 爪哇娇娃
m*2
1
Question:
Return true if the given string contains an appearance of "xyz" where the
xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz"
does not.
xyzThere("abcxyz") → true
xyzThere("abc.xyz") → false
xyzThere("xyz.abc") → true
answer:
public boolean xyzThere(String str) {
for(int i = 0; i < str.length() - 2; i++){
if(str.substring(i, i+3).equals("xyz")){
if(i != 0 && str.substring(i-1, i).equals("."))
{}else{
return true;}
}
}
return false;
}
我的问题是,为什么第二个if之后可以有空的statement{},之后应该是与其对应的else
为什么第一个if之后没有statement, 也没有else;
谢谢大家。
avatar
z*3
2
第二个if和else你可以改成
if(i== 0 || !str.substring(i-1, i).equals("."))
{
return true;
}
它这么写只是为了更贴近人的思维习惯罢了
if之后可以跟else也可以不跟
if(condition is true) { }
else{ do sth.}
相当于
if(condition is false){ do sth.}
avatar
Y*G
3
This ia not a good implemen, a DFA solution without back trace is more
efficient.

"

【在 m******2 的大作中提到】
: Question:
: Return true if the given string contains an appearance of "xyz" where the
: xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz"
: does not.
: xyzThere("abcxyz") → true
: xyzThere("abc.xyz") → false
: xyzThere("xyz.abc") → true
: answer:
: public boolean xyzThere(String str) {
: for(int i = 0; i < str.length() - 2; i++){

avatar
S*C
4
Why bother, just do
public boolean xyzThere(String str) {
return str.contains("xyz") && !str.contains(".xyz");
}
unless you really really care about the "Performance".

"

【在 m******2 的大作中提到】
: Question:
: Return true if the given string contains an appearance of "xyz" where the
: xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz"
: does not.
: xyzThere("abcxyz") → true
: xyzThere("abc.xyz") → false
: xyzThere("xyz.abc") → true
: answer:
: public boolean xyzThere(String str) {
: for(int i = 0; i < str.length() - 2; i++){

avatar
m*2
5
谢谢大家的回复和指点,受益匪浅。请问YHFG, a DFA solution是什么意思?
avatar
Y*G
6
DFA = http://en.wikipedia.org/wiki/Deterministic_finite_automaton
The code below is the most efficient: (better than calling contains and
equals):
boolean xyzThere(String str) {
char next[] = new char[] {'x', 'y', 'z'};
int state = 0;
for (int i = 0; i < str.length(); i ++) {
char ch = str.charAt(i);
switch (status) {
case 0:
if (ch == '.') {
state = -1;
} else if (ch == 'x') {
state = 1;
}
break;
case -1:
if (ch != '.') {
ch = 0;
}
break;
case 1:
case 2:
if (ch == '.') {
state = -1;
} else if (ch != next[status]) {
state = (ch == 'x')?1:0;
} else {
state++;
}
break;
}
if (status == 3) {
return true;
}
}
return false;
}
avatar
Y*G
7
correct some typo:
boolean xyzThere(String str) {
char next[] = new char[] {'x', 'y', 'z'};
int state = 0;
for (int i = 0; i < str.length(); i ++) {
char ch = str.charAt(i);
switch (state) {
case 0:
if (ch == '.') {
state = -1;
} else if (ch == 'x') {
state = 1;
}
break;
case -1:
if (ch != '.') {
state = 0;
}
break;
case 1:
case 2:
if (ch == '.') {
state = -1;
} else if (ch != next[status]) {
state = (ch == 'x')?1:0;
} else {
state++;
}
break;
}
if (state == 3) {
return true;
}
}
return false;
}
avatar
m*2
8
非常感谢详细的codes:) 很专业。

【在 Y**G 的大作中提到】
: correct some typo:
: boolean xyzThere(String str) {
: char next[] = new char[] {'x', 'y', 'z'};
: int state = 0;
: for (int i = 0; i < str.length(); i ++) {
: char ch = str.charAt(i);
: switch (state) {
: case 0:
: if (ch == '.') {
: state = -1;

avatar
S*C
9
你这个是面试用的解法,感觉像用写C的方式写Java,但一不小心就有bug.
我的是工作用的解法,看上去一定要简单,容易维护,

【在 Y**G 的大作中提到】
: correct some typo:
: boolean xyzThere(String str) {
: char next[] = new char[] {'x', 'y', 'z'};
: int state = 0;
: for (int i = 0; i < str.length(); i ++) {
: char ch = str.charAt(i);
: switch (state) {
: case 0:
: if (ch == '.') {
: state = -1;

avatar
o*i
10
实际上,你的解法效率也更高

【在 S**********C 的大作中提到】
: 你这个是面试用的解法,感觉像用写C的方式写Java,但一不小心就有bug.
: 我的是工作用的解法,看上去一定要简单,容易维护,

avatar
B*g
11
aglee

【在 o***i 的大作中提到】
: 实际上,你的解法效率也更高
avatar
o*i
12
:)

【在 B*****g 的大作中提到】
: aglee
avatar
F*n
13
面试问这种题多半是Expect regex 的答案

【在 B*****g 的大作中提到】
: aglee
avatar
m*2
14
弱弱地问,什么是regex?
谢谢!
avatar
m*a
15
Regular expression

【在 m******2 的大作中提到】
: 弱弱地问,什么是regex?
: 谢谢!

avatar
a*m
16
同样受教了!感觉C++和java在string这块儿小差别还是非常多的
avatar
g*s
17
赞!
我还是觉得这个最简约易懂。

【在 S**********C 的大作中提到】
: Why bother, just do
: public boolean xyzThere(String str) {
: return str.contains("xyz") && !str.contains(".xyz");
: }
: unless you really really care about the "Performance".
:
: "

avatar
x*o
18

面试人直接说了,我知道JAVA很多方法,不能用,哈哈

【在 S**********C 的大作中提到】
: 你这个是面试用的解法,感觉像用写C的方式写Java,但一不小心就有bug.
: 我的是工作用的解法,看上去一定要简单,容易维护,

avatar
o*2
19
"xyz.xyz"

【在 S**********C 的大作中提到】
: Why bother, just do
: public boolean xyzThere(String str) {
: return str.contains("xyz") && !str.contains(".xyz");
: }
: unless you really really care about the "Performance".
:
: "

avatar
s*o
20
regex lookbehind
avatar
S*C
21
So? it will return false, that matched the definition.

【在 o**2 的大作中提到】
: "xyz.xyz"
avatar
d*g
22
should return true ba..

【在 S**********C 的大作中提到】
: So? it will return false, that matched the definition.
avatar
T*e
23
Second this.

【在 s***o 的大作中提到】
: regex lookbehind
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。