我的評論
re: 新浪一道面試題:寫一個(gè)函數(shù),計(jì)算兩個(gè)文件的相對路徑的遞歸算法 AthrunWang 2012-09-01 01:38
String aPath = "/P/y/z/a/b/a/g/e.php";
String bPath = "/P/y/z/a/b/a/g/c.php";
情況的時(shí)候貌似不對。
代碼可改成:
public String pathARelativePathB(String pathA, String pathB, int i) {
// A相對于B ../g/e.php
if (pathA.contains(pathB)) {
if (i == 1) {
return pathA.replaceAll(pathB + "/", "");
} else {
StringBuffer sb = new StringBuffer();
for (int j = 1; j < i; j++)
sb.append("../");
return sb.append(pathA.replaceAll(pathB + "/", "")).toString();
}
} else {
return pathARelativePathB(pathA, pathB.substring(0, pathB.lastIndexOf("/")), ++i);
}
}
String bPath = "/P/y/z/a/b/a/g/c.php";
情況的時(shí)候貌似不對。
代碼可改成:
public String pathARelativePathB(String pathA, String pathB, int i) {
// A相對于B ../g/e.php
if (pathA.contains(pathB)) {
if (i == 1) {
return pathA.replaceAll(pathB + "/", "");
} else {
StringBuffer sb = new StringBuffer();
for (int j = 1; j < i; j++)
sb.append("../");
return sb.append(pathA.replaceAll(pathB + "/", "")).toString();
}
} else {
return pathARelativePathB(pathA, pathB.substring(0, pathB.lastIndexOf("/")), ++i);
}
}
re: [導(dǎo)入][轉(zhuǎn)載]java中用URLConnection 類post方式提交表單 AthrunWang 2011-09-28 13:06
非常好,幫了大忙了,十分感謝!