我正在尝试UVa问题编号10324 我编写了以下解决方案,这得到了一个接受,但给出了一个非常糟糕的运行时间2.670秒 我有以下两个代码 这是我的代码
public static void main(String[] args) throws Exception{
// write your code here
StringBuilder op = new StringBuilder();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(isr);
String input, tmp[];
int n, l, u, j, i = 0;
boolean dec;
char thi;
while ((input = reader.readLine()) != null) {
op.append("Case " + (++i) + ":\n");
n = Integer.parseInt(reader.readLine());
while (n-- > 0) {
tmp = reader.readLine().split(" ");
l = Integer.parseInt(tmp[0]);
u = Integer.parseInt(tmp[1]);
if (l > u) {
l ^= u;
u ^= l;
l ^= u;
}
//System.out.println(l + "|" + u);
dec = true;
thi = input.charAt(l++);
for (; l <= u; l++) {
if (thi != input.charAt(l)||(thi != input.charAt(u--))) {
dec = false;
break;
}
}
op.append((dec ? "Yes\n" : "No\n"));
}
}
System.out.print(op.toString());
return;
}
}
,其他代码是我在Mr Gorgon's Solution找到的代码 这个运行时间为0.84秒
public static void main(String[] args) throws Exception {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder("");
int testCase = 1;
String line;
while ((line = br.readLine()) != null) {
sb.append("Case ").append(testCase).append(":\n");
testCase++;
int noOfCases = Integer.parseInt(br.readLine());
for (int j = 0; j < noOfCases; j++) {
String[] str = br.readLine().split(" ");
int val1 = Integer.parseInt(str[0]);
int val2 = Integer.parseInt(str[1]);
if (val1 > val2) {
val1 ^= val2;
val2 ^= val1;
val1 ^= val2;
}
boolean isValid = true;
if (val1 != val2) {
for (int i = val1; i < val2; i++) {
if (line.charAt(i) != line.charAt(i + 1)) {
isValid = false;
break;
}
}
}
if (isValid)
sb.append("Yes\n");
else
sb.append("No\n");
}
}
System.out.print(sb);
}
我发现很难理解当所有任务基本相同时,为什么这段代码运行得更快。我的代码声明比Gorgons代码更少
答案 0 :(得分:2)
op.append("Case " + (++i) + ":\n");
这否定了StringBuilder的好处
if (val1 != val2) {
在第二个版本中,快捷方式是循环答案 1 :(得分:2)
它的
TreeContainer = Backbone.Model.extend({
elementCount: 0,
file: '',
object: {jj: "kk"},
type: "input",
parent: d3.select("#canvas"),
initialize: function () {
var result = this.readFile();
for (var i = 0; i < 200; i++) {
console.log(i); //this is resulted before the readFile content
}
},
readFile: function () {
var model = this;
// display text
if (this.get('file').name.endsWith(".json") || this.get('file').type === "application/json") {
var reader = new FileReader();
reader.onload = function (e) {
//parseJSON
var text = e.target.result;
var data = JSON.parse(text);
model.set('object', data);
console.log(data);
return data;
};
reader.readAsText(this.get('file'));
}
}
});
这有助于加快程序,我认为它与缓存
有关我研究并发现了一些名为 JCS 的内容 这可能允许他们的apache服务器修复内存数据并帮助更快地访问
答案 2 :(得分:-1)
您的解决方案中缺少下面的代码。你正在盲目地循环,好像条件限制循环。
if (val1 != val2) {
}
}