1
我爲兩個和問題構建了一個數據結構。在這個數據結構中,我建立了添加和查找方法。
add - 將數字添加到內部數據結構。
查找 - 查找是否存在任何一對數字,其總和等於該值。 例如:兩個數據結構問題
add(1); add(3); add(5);
find(4) // return true
find(7) // return false
下面是我的代碼,那麼什麼是錯的代碼?
http://www.lintcode.com/en/problem/two-sum-data-structure-design/
這是測試網站,某些情況下無法通過
public class TwoSum {
private List<Integer> sets;
TwoSum() {
this.sets = new ArrayList<Integer>();
}
// Add the number to an internal data structure.
public void add(int number) {
// Write your code here
this.sets.add(number);
}
// Find if there exists any pair of numbers which sum is equal to the value.
public boolean find(int value) {
// Write your code here
Collections.sort(sets);
for (int i = 0; i < sets.size(); i++) {
if (sets.get(i) > value) break;
for (int j = i + 1; j < sets.size(); j++) {
if (sets.get(i) + sets.get(j) == value) {
return true;
}
}
}
return false;
}
}
你的代碼不工作?如果是的話,哪裏或什麼似乎是問題。 – Imprfectluck
http://www.lintcode.com/en/problem/two-sum-data-structure-design/。這是測試網站。我無法通過一些測試。 –
@WBLee不會鏈接到需要登錄的網站。如果你不能說在問題本身中哪些測試用例不通過*,那麼你可能不應該在StackOverflow上進行詢問。 –