`
阿尔萨斯
  • 浏览: 4206284 次
社区版块
存档分类
最新评论

迅雷的一道面试题。。。看谁做出来

 
阅读更多
写一个方法:
publicStringdelString(StringA,StringB){
.....方法体......
}
用高效的方式从A中剔除包含B的字符,例如StringA="hiareyouok";
StringB="io";delString方法返回"hareyuk"
注意:不能使用String的instandof,splid,charat等等库函数
请问怎么做?




用String的toCharArray做的,如果这个都不能用,那就无能为力了。

Java code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->public class Test {

public static void main(String[] args) {
String a
= "hi are you ok";
String b
= "io";
long t0, t1;
String s
= null;
t0
= System.nanoTime();
s
= delString(a, b);
t1
= System.nanoTime();
System.out.println(t1
- t0 + " ns");
System.out.println(s);
}

private static String delString(String a, String b) {
char[] ca = a.toCharArray();
char[] cb = b.toCharArray();
char[] c = new char[ca.length];
int len = 0;
for(int i = 0; i < ca.length; i++) {
boolean isExist = false;
for(int j = 0; j < cb.length; j++) {
if(ca[i] == cb[j]) {
isExist
= true;
break;
}
}
if(!isExist) {
c[len
++] = ca[i];
}
}
return new String(c, 0, len);
}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics