0
問題減少時間複雜度
我想在火力5個隨機節點,其中值「狀態」等於「ACEPTED」,這兩種解決方案我已經實現需要大量的時間
解決方案1:
該解決方案由獲得隨機值的,如果他們的「狀態」等於「ACEPTED」將其添加到我的清單。
private DatabaseReference mdatabas;
private List<String> keys;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_makequestions);
keys=new ArrayList<>();
mdatabas=FirebaseDatabase.getInstance().getReference()
.child("QUESTIONS").child("SPANISH");
getNofQuestions();
}
private void getNofQuestions(){
mdatabas.child("QUESTIONID").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//QUESTION ID IS EQUAL TO THE NUMBER OF QUESTIONS
nOfQuestions=dataSnapshot.getValue(Integer.class);
getRandom();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void getRandom(){
getQuestion(random.nextInt(nOfQuestions-11)+11);
}
private void getQuestion(int random){
mdatabas.child(String.valueOf(random)).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(keys.size()==4){
Toast.makeText(getApplicationContext(),"FINISHED",Toast.LENGTH_SHORT).show();
}
else{
if(dataSnapshot.child("STATE").getValue(String.class)!=null){
if(dataSnapshot.child("STATE").getValue(String.class).equals("ACEPTED")){
keys.add(dataSnapshot.getKey());
getRandom();
}
else{
getRandom();
}
}
else{
getRandom();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
解決方案2 基本上我得到所有他們的狀態值等於接受了問題,洗牌他們,使子列表。
private void getQuestions(){
mdatabas.orderByChild("STATE").equalTo("ACEPTED").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot:dataSnapshot.getChildren()){
keys.add(snapshot.child("ID").getValue(String.class));
}
Collections.shuffle(keys);
List<String> finalKeys = keys.subList(0, 4);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
JSON結構(這是下面的問題/西班牙)
{
"0" : {
"ID" : "0",
"MISPELLED" : 1,
"SPAM" : 1,
"STATE" : "ACEPTED",
"WRONGLANGUAGE" : 1,
"WRONGREGION" : 1
},
"1" : {
"ID" : "1",
"OPTION1" : "Elementos químicos ",
"OPTION2" : "Huesos ",
"OPTION3" : "Órganos ",
"OPTION4" : "Células ",
"QUESTION" : "La estequiologia se encarga de estudiar "
},
"2" : {
"CORRECT" : 1,
"ID" : "11",
"LIKES" : 15,
"OPTION1" : "Elementos químicos ",
"OPTION2" : "Huesos ",
"OPTION3" : "Órganos ",
"OPTION4" : "Células ",
"QUESTION" : "La estequiologia se encarga de estudiar ",
"STATE" : "ACEPTED",
"WRONG" : 1
},
"3" : {
"CORRECT" : 2,
"ID" : "12",
"LIKES" : 6,
"MISPELLED" : 2,
"OPTION1" : "Ingestión de cecaria enquistada en \r\nplanta de agua dulce.\r\n ",
"OPTION2" : "Ingestión de metacecaria enquistada en \r\nplanta de agua dulce.\r\n",
"OPTION3" : "Ingestión de metacecaria enquistada en \r\nplanta de agua salada",
"OPTION4" : "Ingestión de esporocito enquistada en \r\nplanta de agua dulce.\r\n",
"QUESTION" : "Mecanismos de transmisión de faciolopsis buski ",
"WRONG" : 3
},
"QUESTIONID" : 3
}
所以我我的問題是,如果有一些獲取數據或任何改進的另一種方式,我可以使地雷減少時間複雜性?
感謝,關於它使用隨機的númerical索引,我現在將移動到推送ID。 –